1

please do not close my case. I have submitted the same question couple of times yesterday, but someone has posted a link. Am sorry for that, am very new to this coding. But i managed to print all the key and values of this Json, except the inner objects in th earray. I am spending hours in google searching for the same. below is th ecode i used to print all the values except inner values.

Map results = (Map) JSON.deserializeUntyped(response.getBody());

Map Gtusage = (Map)results.get('QueryResult');
Map RtInfo = (Map)Gtusage.get('ContractInfo');
System.debug(RtInfo.get('ID'));
System.debug(RtInfo.get('Status'));
System.debug(RtInfo.get('Segment'));
Map RtInfo2 = (Map)Gtusage.get('SubscriberInfo'); 
//Map RtArr = (Map)RtInfo2.get('SubscriberProducts'); 
System.debug(RtInfo2.get('Status')); 
System.debug(RtInfo2.get('BillingType')); 
System.debug(RtInfo2.get('Name')); 
System.debug(RtInfo2.get('BSType'));
List RtInfo3 = (List)Gtusage.get('SubscriberProducts'); 
if (RtInfo3 != null) { 
    for (Object item : RtInfo3) {
        Map i = (Map)item;
        system.debug((String) i.get('Code'));
        system.debug((String) i.get('Status'));
        system.debug((String) i.get('DisplayName'));
        system.debug((String) i.get('ArabicDisplayName'));
        system.debug((String) i.get('Category'));
        system.debug((String) i.get('SubCategory'));
        system.debug((String) i.get('StartDate'));
        system.debug((String) i.get('EndDate'));
    }
}

This is my json output. From that, i want to get the inner object (product fee) and inner array elements(ProductSpecs).

{
    "QueryResult": {
        "ContractInfo": {
            "ID": "1684409",
            "Status": "Active",
            "Segment": "Consumer"
        },
        "SubscriberInfo": {
            "Status": "Active",
            "BillingType": "POST",
            "Name": "VAS  A S",
            "BSType": "VOICE"
        },
        "SubscriberProducts": [
            {
                "Code": "VCAF",
                "Status": "ACTV",
                "DisplayName": "Activation Fee Video Calls",
                "ArabicDisplayName": "gjhghghjg",
                "Category": "Video",
                "SubCategory": "Local",
                "StartDate": "2010-10-20T13:47:26.001",
                "EndDate": "2020-06-30T23:59:00.001",
                "ProductFees": {
                    "ProductFee": {
                        "Name": "Installation",
                        "Amount": "0",
                        "RecurrencyType": "Single",
                        "RecurrencyValue": "1"
                    }
                },
                "Params": {
                    "Param": {
                        "Name": "AutoRenewFlag",
                        "Value": "OFF"
                    }
                }
            },
            {
                "Code": "N_VC35-SUBS",
                "Status": "ACTV",
                "DisplayName": "KD35 - 5000 Minutes Plus 4G_500GB",
                "ArabicDisplayName": "5000 Minutes",
                "Category": "Bundle",
                "SubCategory": "Voice,SMS,MMS,Data",
                "Classification": "SUBS",
                "StartDate": "2017-05-31T23:59:59.001",
                "EndDate": "2020-06-30T23:59:00.001",
                "ProductSpecs": {
                    "ProductSpec": [
                        {
                            "Name": "Voice",
                            "Value": "5000",
                            "Measure": "Min"
                        },
                        {
                            "Name": "SMS",
                            "Value": "1000",
                            "Measure": "Unit"
                        },
                        {
                            "Name": "MMS",
                            "Value": "1000",
                            "Measure": "Unit"
                        },
                        {
                            "Name": "Data",
                            "Value": "500",
                            "Measure": "GB"
                        }
                    ]
                },
                "ProductFees": {
                    "ProductFee": {
                        "Name": "Rental",
                        "Amount": "35",
                        "RecurrencyType": "Month",
                        "RecurrencyValue": "1"
                    }
                },
                "Params": {
                    "Param": {
                        "Name": "AutoRenewFlag",
                        "Value": "OFF"
                    }
                }
            },
            {
                "Code": "N_VC35-GPRS",
                "Status": "ACTV",
                "DisplayName": "KD35 - 5000 Minutes Plus 4G_500GB",
                "ArabicDisplayName": "5000 Minutes",
                "Category": "Bundle",
                "SubCategory": "Voice,SMS,MMS,Data",
                "Classification": "GPRS",
                "StartDate": "2017-05-31T23:59:59.001",
                "EndDate": "2020-06-30T23:59:00.001",
                "ProductSpecs": {
                    "ProductSpec": [
                        {
                            "Name": "Voice",
                            "Value": "5000",
                            "Measure": "Min"
                        },
                        {
                            "Name": "SMS",
                            "Value": "1000",
                            "Measure": "Unit"
                        },
                        {
                            "Name": "MMS",
                            "Value": "1000",
                            "Measure": "Unit"
                        },
                        {
                            "Name": "Data",
                            "Value": "500",
                            "Measure": "GB"
                        }
                    ]
                },
                "ProductFees": {
                    "ProductFee": {
                        "Name": "Rental",
                        "Amount": "35",
                        "RecurrencyType": "Month",
                        "RecurrencyValue": "1"
                    }
                },
                "Params": {
                    "Param": {
                        "Name": "AutoRenewFlag",
                        "Value": "OFF"
                    }
                }
            },
            {
                "Code": "1-SHEFPR002",
                "Status": "ACTV",
                "DisplayName": "KD35 ATL Mixed Promotion",
                "StartDate": "2010-10-20T13:47:26.001",
                "EndDate": "2020-06-30T23:59:00.001",
                "ProductFees": {
                    "ProductFee": {
                        "Name": "Installation",
                        "Amount": "0",
                        "RecurrencyType": "Single",
                        "RecurrencyValue": "1"
                    }
                },
                "Params": {
                    "Param": {
                        "Name": "AutoRenewFlag",
                        "Value": "OFF"
                    }
                }
            },
            {
                "Code": "MSMU",
                "Status": "ACTV",
                "DisplayName": "MissU Monthly Subscription",
                "ArabicDisplayName": "MissU - poo",
                "Category": "VAS",
                "SubCategory": "MissU",
                "Classification": "MISSU",
                "StartDate": "2017-09-27T23:59:59.001",
                "EndDate": "2020-06-30T23:59:00.001",
                "ProductFees": {
                    "ProductFee": {
                        "Name": "Rental",
                        "Amount": ".5",
                        "RecurrencyType": "Month",
                        "RecurrencyValue": "1"
                    }
                },
                "Params": {
                    "Param": {
                        "Name": "AutoRenewFlag",
                        "Value": "OFF"
                    }
                }
            },
            {
                "Code": "PK00001",
                "Status": "ACTV",
                "DisplayName": "Mobile Services",
                "StartDate": "2010-10-20T13:47:26.001",
                "EndDate": "2020-06-30T23:59:00.001",
                "ProductFees": {
                    "ProductFee": {
                        "Name": "Installation",
                        "Amount": "0",
                        "RecurrencyType": "Single",
                        "RecurrencyValue": "1"
                    }
                },
                "Params": {
                    "Param": {
                        "Name": "AutoRenewFlag",
                        "Value": "OFF"
                    }
                }
            },
            {
                "Code": "MTVARAB",
                "Status": "ACTV",
                "DisplayName": "Mobile TV Arabic Package PostPaid",
                "ArabicDisplayName": "Mobile TV Arabic Package",
                "Category": "VAS",
                "SubCategory": "Streaming",
                "StartDate": "2015-01-08T23:59:59.001",
                "EndDate": "2020-06-30T23:59:00.001",
                "ProductFees": {
                    "ProductFee": {
                        "Name": "Rental",
                        "Amount": "3",
                        "RecurrencyType": "Month",
                        "RecurrencyValue": "1"
                    }
                },
                "Params": {
                    "Param": {
                        "Name": "AutoRenewFlag",
                        "Value": "OFF"
                    }
                }
            },
            {
                "Code": "Z00101",
                "Status": "ACTV",
                "DisplayName": "Postpaid Mobile Offer",
                "StartDate": "2010-10-20T13:47:26.001",
                "EndDate": "2020-06-30T23:59:00.001",
                "ProductFees": {
                    "ProductFee": {
                        "Name": "Installation",
                        "Amount": "0",
                        "RecurrencyType": "Single",
                        "RecurrencyValue": "1"
                    }
                },
                "Params": {
                    "Param": {
                        "Name": "AutoRenewFlag",
                        "Value": "OFF"
                    }
                }
            },
            {
                "Code": "FGRBT",
                "Status": "ACTV",
                "DisplayName": "RBT-SUBSCIPTION FREE",
                "ArabicDisplayName": "manga",
                "Category": "VAS",
                "SubCategory": "RBT",
                "Classification": "RPT",
                "StartDate": "2020-05-04T00:00:00.001",
                "EndDate": "2020-06-30T23:59:00.001",
                "ProductFees": {
                    "ProductFee": {
                        "Name": "Installation",
                        "Amount": "0",
                        "RecurrencyType": "Single",
                        "RecurrencyValue": "1"
                    }
                },
                "Params": {
                    "Param": {
                        "Name": "AutoRenewFlag",
                        "Value": "OFF"
                    }
                }
            },
            {
                "Code": "1-Q205",
                "Status": "ACTV",
                "DisplayName": "SIM",
                "ArabicDisplayName": "Multi SIM 2/3/4FF USIM Prep",
                "StartDate": "2010-10-20T13:47:26.001",
                "EndDate": "2020-06-30T23:59:00.001",
                "ProductFees": {
                    "ProductFee": {
                        "Name": "Installation",
                        "Amount": "5",
                        "RecurrencyType": "Single",
                        "RecurrencyValue": "1"
                    }
                },
                "Params": {
                    "Param": {
                        "Name": "AutoRenewFlag",
                        "Value": "OFF"
                    }
                }
            },
            {
                "Code": "FG50",
                "Status": "ACTV",
                "DisplayName": "Voice Mail Free Activation",
                "ArabicDisplayName": "vrithi",
                "Category": "VAS",
                "SubCategory": "Voice Mail",
                "Classification": "VOS",
                "StartDate": "2020-05-14T00:00:00.001",
                "EndDate": "2020-06-30T23:59:00.001",
                "ProductFees": {
                    "ProductFee": {
                        "Name": "Rental",
                        "Amount": "0",
                        "RecurrencyType": "Year",
                        "RecurrencyValue": "1"
                    }
                },
                "Params": {
                    "Param": {
                        "Name": "AutoRenewFlag",
                        "Value": "OFF"
                    }
                }
            },
            {
                "Code": "DATASHARE",
                "Status": "ACTV",
                "DisplayName": "Internet Sharing service",
                "ArabicDisplayName": "arabicname",
                "Classification": "DS",
                "StartDate": "2017-05-31T23:59:59.001",
                "EndDate": "2020-06-30T23:59:00.001",
                "ProductFees": [
                    {
                        "ProductFee": {
                            "Name": "Installation",
                            "Amount": "0",
                            "RecurrencyType": "Single",
                            "RecurrencyValue": "0"
                        }
                    },
                    {
                        "ProductFee": {
                            "Name": "Rental",
                            "Amount": "0",
                            "RecurrencyType": "None",
                            "RecurrencyValue": "0"
                        }
                    }
                ],
                "Params": {
                    "Param": {
                        "Name": "AutoRenewFlag",
                        "Value": "NA"
                    }
                }
            }
        ]
    }
}
  • Follow this – rahul gawale Jun 13 at 8:27
  • Hi Rahul, Thanks for your responce. I have recieved this URL yesterday, but am comparing it with what i have done. and no clue how to get the inner object inside of an array – user84267 Jun 13 at 8:36
  • "ProductFees": { "ProductFee": { "Name": "Installation", "Amount": "0", "RecurrencyType": "Single", "RecurrencyValue": "1" } }, "Params": { "Param": { "Name": "AutoRenewFlag", "Value": "OFF" } – user84267 Jun 13 at 8:38
  • which inner object inside the array are you referring to? – rahul gawale Jun 13 at 9:05
  • The "ProductFee" object inside the array "SubscriberProducts" – user84267 Jun 13 at 9:13
3

The ProductFees returns 'inconsistent' structure, a map and list of map:

"ProductFees": {
    "ProductFee": {
        "Name": "Rental",
        "Amount": "0",
        "RecurrencyType": "Year",
        "RecurrencyValue": "1"
    }
},
"ProductFees": [
    {
        "ProductFee": {
            "Name": "Installation",
            "Amount": "0",
            "RecurrencyType": "Single",
            "RecurrencyValue": "0"
        }
    },
    {
        "ProductFee": {
            "Name": "Rental",
            "Amount": "0",
            "RecurrencyType": "None",
            "RecurrencyValue": "0"
        }
    }
],

You need to determine if the ProductFees is a list or map by using the instanceOf keyword.

if (RtInfo3 != null) { 
    for (Object item : RtInfo3) {
        Map<String, Object> i = (Map<String, Object>)item;

        if (i.get('ProductFees') instanceOf List<Object>) {
            for (Object pf : (List<Object>)i.get('ProductFees')) {
                Map<String, Object> pff = (Map<String, Object>)pf;
                System.debug(pff.get('ProductFee'));
            }
        }
        else if (i.get('ProductFees') instanceOf Map<String, Object>) {
            Map<String, Object> pf = (Map<String, Object>)i.get('ProductFees');
            System.debug(pf.get('ProductFee'));
        }
    }
}
| improve this answer | |
  • Thanks. It works. Saved my Life – user84267 Jun 13 at 13:43
0

Your logic is correct but the syntax that you have used does not work with apex. You must mention the type while declaring the collections.

this does not work in the apex code

Map myMmap = new Map();

and this does work.

Map<String, Object> myMmap = new Map<String, Object>();

Same goes with the lists, you need to mention a list of what!

Final code

Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
Map<String, Object> Gtusage = (Map<String, Object>)results.get('QueryResult');
Map<String, Object> RtInfo = (Map<String, Object>)Gtusage.get('ContractInfo');
System.debug(RtInfo.get('ID'));
System.debug(RtInfo.get('Status'));
System.debug(RtInfo.get('Segment'));
Map<String, Object> RtInfo2 = (Map<String, Object>)Gtusage.get('SubscriberInfo'); 
//Map<String, Object> RtArr = (Map<String, Object>)RtInfo2.get('SubscriberProducts'); 
System.debug(RtInfo2.get('Status')); 
System.debug(RtInfo2.get('BillingType')); 
System.debug(RtInfo2.get('Name')); 
System.debug(RtInfo2.get('BSType'));
List<Object> RtInfo3 = (List<Object>)Gtusage.get('SubscriberProducts'); 
if (RtInfo3 != null) { 
    for (Object item : RtInfo3) {
        Map<String, Object> i = (Map<String, Object>)item;
        system.debug((String) i.get('Code'));
        system.debug((String) i.get('Status'));
        system.debug((String) i.get('DisplayName'));
        system.debug((String) i.get('ArabicDisplayName'));
        system.debug((String) i.get('Category'));
        system.debug((String) i.get('SubCategory'));
        system.debug((String) i.get('StartDate'));
        system.debug((String) i.get('EndDate'));
    }
}
| improve this answer | |
  • Hi Rahul, The final code is the same code what i have now. The code prints all the objects inside the list RtInfo3. But the Array also contains an objects " ProductFee" insdie. But i do not know how to print that as well (or take the values of "Name": "Rental", "Amount": "35", "RecurrencyType": "Month", "RecurrencyValue": "1") – user84267 Jun 13 at 11:24
  • @user84267 check the answer by Hengky Ilawan for that – rahul gawale Jun 13 at 13:03
  • Sure, Thanks Rahul. – user84267 Jun 14 at 17:00

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.