parse json response into name value pair










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"










share|improve this question



















  • 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















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"










share|improve this question



















  • 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













1












1








1


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"










share|improve this question
















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"







apex json






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 13:14







Raju Sen

















asked Nov 14 '18 at 13:45









Raju SenRaju Sen

969




969







  • 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












  • 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







1




1





That's basically JSON abuse. Why do you want/need it in this format?

– sfdcfox
Nov 14 '18 at 13:53





That's basically JSON abuse. Why do you want/need it in this format?

– sfdcfox
Nov 14 '18 at 13:53




3




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





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





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





Can we use json.createParser to form this output, iterating every attribute?

– Raju Sen
Nov 14 '18 at 14:16




1




1





Okay, just realized your JSON expected output is invalid JSON. Are you sure that's correct?

– sfdcfox
Nov 14 '18 at 14:20





Okay, just realized your JSON expected output is invalid JSON. Are you sure that's correct?

– sfdcfox
Nov 14 '18 at 14:20










1 Answer
1






active

oldest

votes


















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" ]





share|improve this answer






















    Your Answer








    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "459"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f239338%2fparse-json-response-into-name-value-pair%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    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" ]





    share|improve this answer



























      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" ]





      share|improve this answer

























        4












        4








        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" ]





        share|improve this answer













        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" ]






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 14 '18 at 14:22









        sfdcfoxsfdcfox

        256k11201441




        256k11201441



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Salesforce Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f239338%2fparse-json-response-into-name-value-pair%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            How to read a connectionString WITH PROVIDER in .NET Core?

            In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

            Museum of Modern and Contemporary Art of Trento and Rovereto