1

I'm facing some issues with JSON deserializing. I have a JSON format data from my existing webservice and I have collected all that data and made a .txt file and uploaded as a static resource. So my question is, how can I utilize that static resource file in apex class ? Here is my JSON format Data :

array(10) {
  [0]=>
  array(19) {
    ["cso_no"]=>
    string(8) "13095524"
    ["product_id"]=>
    string(10) "5000115236"
    ["vin_no"]=>
    string(17) "SAJBB4AG0HCY38725"
    ["dlrid"]=>
    string(5) "20067"
    ["current_allocation_dlr"]=>
    string(14) "Aditya Suzuki"
    ["brand"]=>
    string(6) "SUzuki"
    ["model"]=>
    string(10) "Baleno"
    ["submodel"]=>
    string(8) "TDI"
    ["engine"]=>
    string(8) "2.0 GTDi"
    ["ext_colour_code"]=>
    string(3) "1AA"
    ["ext_colour"]=>
    string(13) "Polaris White"
    ["int_colour_code"]=>
    string(3) "RNR"
    ["int_colour"]=>
    string(5) "Latte"
    ["model_yr"]=>
    string(4) "2017"
    ["mfg_yr"]=>
    string(9) "18-Jul-16"
    ["order_status"]=>
    string(12) "Dealer Stock"
    ["stock_status"]=>
    string(6) "closed"
    ["specification"]=>
    string(0) ""
    ["show_fly"]=>
    string(1) "Y"
  }
    }

There are many more records like this. So, how can I parse this and get it to deserialize in a list or arrays?

I have tried to create a class for this as well. I'll show what I have done until now :

public class JSON2Apex {
    public static void consumeObject(JSONParser parser) {
        Integer depth = 0;
        do {
            JSONToken curr = parser.getCurrentToken();
            if (curr == JSONToken.START_OBJECT || 
                curr == JSONToken.START_ARRAY) {
                depth++;
            } else if (curr == JSONToken.END_OBJECT ||
                curr == JSONToken.END_ARRAY) {
                depth--;
            }
        } while (depth > 0 && parser.nextToken() != null);
    }   

    public class JsonDeserialize {
        public String cso_no {get;set;} 
        public String product_id {get;set;} 
        public String vin_no {get;set;} 
        public String dlrid {get;set;} 
        public String current_allocation_dlr {get;set;} 
        public String brand {get;set;}  
        public String model {get;set;} 
        public String submodel {get;set;} 
        public String engine {get;set;} 
        public String ext_colour_code {get;set;} 
        public String ext_colour {get;set;} 
        public String int_colour_code {get;set;} 
        public String int_colour {get;set;} 
        public String model_yr {get;set;} 
        public String mfg_yr {get;set;} 
        public String order_status {get;set;} 
        public String stock_status {get;set;} 
        public String specification {get;set;} 
        public String show_udaan {get;set;} 

        public JsonDeserialize(JSONParser parser) {
            while (parser.nextToken() != JSONToken.END_OBJECT) {
                if (parser.getCurrentToken() == JSONToken.FIELD_NAME) {
                    String text = parser.getText();
                    if (parser.nextToken() != JSONToken.VALUE_NULL) {
                        if (text == 'cso_no') {
                            cso_no = parser.getText();
                        } else if (text == 'product_id') {
                            product_id = parser.getText();
                        } else if (text == 'vin_no') {
                            vin_no = parser.getText();
                        } else if (text == 'dlrid') {
                            dlrid = parser.getText();
                        } else if (text == 'current_allocation_dlr') {
                            current_allocation_dlr = parser.getText();
                        } else if (text == 'brand') {
                            brand = parser.getText();
                        } else if (text == 'model') {
                            model = parser.getText();
                        } else if (text == 'submodel') {
                            submodel = parser.getText();
                        } else if (text == 'engine') {
                            engine = parser.getText();
                        } else if (text == 'ext_colour_code') {
                            ext_colour_code = parser.getText();
                        } else if (text == 'ext_colour') {
                            ext_colour = parser.getText();
                        } else if (text == 'int_colour_code') {
                            int_colour_code = parser.getText();
                        } else if (text == 'int_colour') {
                            int_colour = parser.getText();
                        } else if (text == 'model_yr') {
                            model_yr = parser.getText();
                        } else if (text == 'mfg_yr') {
                            mfg_yr = parser.getText();
                        } else if (text == 'order_status') {
                            order_status = parser.getText();
                        } else if (text == 'stock_status') {
                            stock_status = parser.getText();
                        } else if (text == 'specification') {
                            specification = parser.getText();
                        } else if (text == 'show_udaan') {
                            show_udaan = parser.getText();
                        }
                    }
                }
            }
        }
    }


    public static JSON2Apex parse(String json) {
        return (JSON2Apex)System.JSON.deserialize(json, JSON2Apex.class);
    }   
}

I'm new into this JSON parsing so dont know much about it. Can someone guide me through it.

Thanks in advance to all.

  • You can refer this answer – Rohit Mourya Nov 11 '17 at 6:59
  • Does not look like json to my. Looks more like the output of a Javascript object??? – Eric Nov 11 '17 at 7:31
  • Well, it is an array of data but i have similar to it in the json format. So any more suggestions on it ? – Hardik B Nov 11 '17 at 9:48
4

Do ensure that your static resource is in JSON format; you can check that using a JSON validator e.g. https://jsonformatter.curiousconcept.com/.

The JSON.deserialize(jsonString, apexType) method is the first thing to try as it does all the deserializing work for you. That work is controlled by the second argument, and the simplest way to generate that class is to give JSON2Apex an example of your JSON and let it work out the corresponding class definitions.

So the deserialization becomes this simple:

public static JSON2Apex parse(String json) {
    return (JSON2Apex) System.JSON.deserialize(json, JSON2Apex.class);
}

(JSONParser requires you to do a lot of coding that embodies the expected structure - very error prone and hardly ever necessary to do.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.