0

I have below JSON and I want to get the name and label of all blocks in one list. How I can get it using Apex?

{
  "name":"A",
  "label":"as",
  "prefix":"mr",
  "data":"ad",
  "subject":"das"
},
{
  "name":"B",
  "label":"w1",
  "prefix":"dasd",
  "data":"asda",
  "subject":""
},
{
  "name":"C"
  "label":"ds"
  "prefix":"",
  "data":"dasd",
  "subject":"ada"
}

I tried with code below but not getting how to get only Name and column (key and values) in other list

HttpResponse httpResponse = http.send(httpRequest); 

        string jsonResponse=httpResponse.getBody();
        Map<String, Object> results = (Map<String, Object>)JSON.deserializeUntyped(jsonResponse);
        List<Object> lstCustomers = (List<Object>)results.get('sobjects');

        for (Object customer : lstCustomers) {
 // now get attributes for this customer.

My Output needs to be

Map{Name:'A',"b','c'}
4
  • Did you check if the given JSON string is valid?
    – Ashwani
    Commented Jan 30, 2020 at 9:41
  • This is just an example to understand query Commented Jan 30, 2020 at 9:51
  • Where does it fail? Are you getting an error? It might also be helpful to see the code you're using to attempt to access each customer object in the for loop.
    – pchittum
    Commented Jan 30, 2020 at 9:54
  • I stuck .how i can achive this result..Map{Name:'A',"b','c'} Commented Jan 30, 2020 at 10:04

1 Answer 1

5

Firstly, it seems that the JSON example you provided is not a valid JSON (missing ',' between elements), I know it is just an example, but if you are using an array then it should have been enclosed with [ and ].

Now coming to the requirement, assuming that it is an array, JSON's deserializeUntyped returns Object which means it depends on what kind of JSON is passed, so in your case it will return List<Object> as your input is an array of JSON

In order to get the data in the format that you have specified, you will have to follow 3 steps.

  1. Get the response in List<Object> using JSON.deserializeUntyped
  2. Convert List<Object> to List<Map<string,Object>> by typecasting each element inside the list
  3. Get the response in the format you need from the List<Map<string,Object>>

This is how your code should be which can be optimized if needed

//First step : Get the response in List<Object>
string jsonResponse ='[{"name":"A","label":"as","prefix":"mr","data":"ad","subject":"das"},{"name":"B","label":"w1","prefix":"dasd","data":"asda","subject":""},{"name":"C","label":"ds","prefix":"","data":"dasd","subject":"ada"}]';
List<Object> lstCustomers = (List<Object>) JSON.deserializeUntyped(jsonResponse);

//Second step : Convert List<Object> to List<Map<string,Object>> by typecasting each element inside the list
List<Map<String, Object>> allMaps = new List<Map<String, Object>>();
for(Object cust : lstCustomers){
    allMaps.add((Map<String, Object>)cust);
}

//Third step : Get the response in the format you need from the List<Map<string,Object>>
Map<string, List<Object>> finalMap = new Map<string, List<Object>>();
for(Map<String, Object> cus : allMaps){
    for(string key : cus.keyset()){
        if(finalMap.containsKey(key)){
            finalMap.get(key).add(cus.get(key));
        }else {
            List<Object> newObj = new List<Object>();
            newObj.add(cus.get(key));
            finalMap.put(key,newObj);
        }
    }
}
system.debug(finalMap);
system.debug(finalMap.get('name'));
system.debug(finalMap.get('label'));
//and so on for other attributes
2
  • ajay what Vijay is proposing it works fine.. change the system.debug(finalMap); for System.debug(finalMap.get('name')); and you will get what you want. Commented Jan 30, 2020 at 10:38
  • @Ajay the above code should work for you Commented Jan 30, 2020 at 10:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .