1

I have a json string , and i want it to be converted into name value pair json string.

My actual string:

 {"payload":[{"YearlylCostPercentage":"4.6",
 "TotalRepayment":"120000","PropertyMarketValue":"100000","productname":"Standard","PaymentFrequency":"Monthly","NumberOfTerms":"60","NotaryName":"Notary","NotaryEmail":"raj234eu222222222.sen@yahoo.com","NotaryContactNumber":"0234241729","NotaryAdressLine2":"1112 CM The Hag","NotaryAdressLine1":"Valkenboslaan 110","nominalInterestRate":"4.5","MonthlyPayment":"600","MonthlyInterestAmount":"300","loanUtilizationDate":"12-1-2019","loanPurpose":"Loan","loanMaturityDate":"12-2-2019","LoanDuration":"5","loanAmount":"100000","KadasterLocation":"Amsterdam","InterestFixationProvision":"1.0","HandlingCost":"900","FixedInterestyear":"2.0","firstPaymentDate":"12-2-2019","FirstMonthlyPayment":"700","effectiveInterestRate":"4.7","DepartmentName":"Dev","CustomerDetails":[{"CustomerSalutation":"Geen titulatuur gewenst","CustomerName":"Eqaavq","AddressLine2":"1221 NH MDFMGDAKSV","AddressLine1":"Wdr Veuniztapbmr 345"},{ "CustomerSalutation":"Mevrouw","CustomerName":"Eqaavq-col Fpcysfptp","AddressLine2":"1221 NH MDFMGDAKSV","AddressLine1":"Wdr Veuniztapbmr 345"}],"city":"Amsterdam","BorrowedRatio":"1.4","AdvisorName":"Raju Sen","AdvisorContact":"2123123312","AdviseCost":"1000"}]}';

expected output :

"payload":{
        {"name" : "YearlylCostPercentage",
        "value":"4.6"
         },
       {"name":"TotalRepayment",
       "value":"120000"
    }
  • 1
    That's basically JSON abuse. Why do you want/need it in this format? – sfdcfox Nov 14 '18 at 13:53
  • 3
    Seems like this might be an X-Y problem. What will performing this transformation do for you? What ends up using this JSON? Is there a reason why you can't simply use JSON.deserialize() or JSON.deserializeUntyped()? – Derek F Nov 14 '18 at 13:55
  • yeah, its an abuse, we are trying to connect with one of the services , they want the request body to have this format , so we can't use it as it is, we cant ask them to handle this from their end too. – Raju Sen Nov 14 '18 at 14:11
  • Can we use json.createParser to form this output, iterating every attribute? – Raju Sen Nov 14 '18 at 14:16
  • 1
    Okay, just realized your JSON expected output is invalid JSON. Are you sure that's correct? – sfdcfox Nov 14 '18 at 14:20
4

JSON.deserializeUntyped is your friend here:

Map<String, Object> jsonParseSource = JSON.deserializeUntyped(jsonBody);
List<Map<String, String>> payload = new List<Map<String, String>>();
Map<String, Object> payloadBody = (Map<String, Object>)jsonParseSource.get('payload');
for(String key: payloadBody.keyset()) {
  payload.add(new Map<String, String> { 'name' => key, 'value' => (String)payloadBody.get(key) });
}
String jsonResult = JSON.serialize(new Map<String, List<Map<String, String>>> {
  'payload' => payload
});

This results in something like:

{ "payload": [
  { "name": "fieldname1", "value": "fieldvalue1" },
  { "name": "fieldname2", "value": "fieldvalue2" } ] }

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.