Not able to get specified value from JSON

apexjsonparsing

I am having JSON structure like this

   "line_items":[
        {
            "id":385,
            "name":"AD737",
            "product_id":743,
            "variation_id":0,
            "quantity":2,
            "sku":"TH99533",
            "price":0,
            "parent_name":null,
            "composite_parent":"",
            "composite_children":[
                386
            ],
            "bundled_by":"",
            "bundled_item_title":"",
            "bundled_items":[
                
            ]
        },
        {
            "id":386,
            "name":"Arisun AD737 (Set of 8)",
            "product_id":742,
            "variation_id":0,
            "quantity":2,
            "sku":"TH99533-8",
            "price":2400,
            "parent_name":null,
            "composite_parent":385,
            "composite_children":[
                
            ],
            "bundled_by":"",
            "bundled_item_title":"",
            "bundled_items":[
                387
            ]
        },
        {
            "id":387,
            "name":"Set of 8",
            "product_id":738,
            "variation_id":0,
            "quantity":16,
            "sku":"TH99533-1",
            "price":0,
            "parent_name":null,
            "composite_parent":"",
            "composite_children":[
                
            ],
            "bundled_by":386,
            "bundled_item_title":"Set of 8",
            "bundled_items":[
                
            ]
        }
    ]

the tree nodes are interlinked

I want to pickout the quantity from third block but for that i have to traverse from 1 to 2 and with the help of 2 i have to get quantity in third block.

  1. From First i have to get the composite_children //386
  2. With this i have to get the bundled_items from second //387
  3. with the id got from 2 I have to get the quantity // 16

I tried in this way

Set<Object> compositeChildernId= new Set<Object>();

for(Object LineItem:lstMetaData){
     Map<String,Object> MapLineItems= (Map<String, Object>)LineItem;
      for(String MapLineItem :  MapLineItems.keyset()){
             if( MapLineItem == 'composite_children'){
                  List<Object> tempObjMap = (List<Object>) MapLineItems.get('composite_children');                  
                   if( tempObjMap.size()>0){
                       compositeChildernId.addAll(tempObjMap);
                   }
             }
      }   

}

with the above i can get only the composite_children id like parentComponentID >>>{386, 389, 392, 400}
but not able to traverse through the next loop

Best Answer

You don't have a Map structure there, you actually have a JSON array.

I recommend defining a LineItem class deserializing into that:

class LineItem{
  Integer id;
  //add other attributes as you need
  Integer[] composite_children;
  Integer[] bundled_items;
}

Deserialize:

LineItem[] lineItems = (LineItem[])JSON.deserialize(jsonItems,LineItem[].class);

Put into a map:

Map<Integer,LineItem> lineItemsById = new Map<Integer,LineItem>();
for (LineItem l : lineItems){
  lineItemsById.put(l.id,l);
}

Now you can really get somewhere:

for (LineItem l : lineItems){
  for (Integer id : l.composite_children){
    LineItem childLineItem = lineItemsById.get(id);
    //etc
  }
}

Note, I'm not sure quite what you are going to do with that number, but this should get you one your way.

Don't forget null checks!!

Related Topic