0

I have JSON to which i have done JSON.stringify() to pass the JSON into apex. Now i have one key:value such that value should be passed as array but since i am doing JSON.stringify()it is converting that array as string. So what can be done to get that "name":"value" from converted JSON string and then send the value as Array.

Format i am excepting.

//Removed some values so that code should not look big.

{
  "receivableaccount": "122",
  "taxItem": "230",
  "shippingitem": "12073",
  **"customer_currency":[2,3,4,5,6,8],
   **//(This is what i want [2,3,4,5,6,8] should be passed as array instead of string)**
  "addressbook": [
    {
      "addr1": "50 Corriveau Ave.",
      "zip": "T8N 3T5",
      "city": "St. Albert,",
      "country": "CA",
      "state": "Alberta"
    }
  ]
}

Code in JS-

finalArr['recordtype'] = recordType;
finalArr['subsidiary'] = subsidary;
finalArr['custentity_customer_type'] = entityCusType;
finalArr['receivableaccount'] = receivableAcc; 
finalArr['taxItem'] = taxItem;
finalArr['shippingitem'] = shippingitem;
finalArr['customer_currency'] = customerCurrency;
finalArr['addressbook'] = addrFieldAttr;
jsonBody = JSON.stringify(finalArr);                    
component.set("v.jsonStr",jsonBody);
console.log('jsonBody of new customer '+jsonBody);

This jsonBody i am directly sending to Apex.

JSON body i am getting through above code.

{
  "companyname": "Uk test account",
  "custentity_salesforce_id": "153602",
  "subsidiary": "1",
  "terms": "Net 15",
  "recordtype": "customer",
  "custentity_customer_type": "2",
  "receivableaccount": "122",
  "taxItem": "230",
  "shippingitem": "12073",
  "customer_currency": "[2,3,4,5,6,8]",
  "addressbook": [
    {
      "addr1": "32 Princes Street",
      "zip": "EX19 2HD",
      "city": "Roborough",
      "country": "GB",
      "state": ""
    }
  ]
}
              
8
  • How did you create the JS variable customerCurrency? Without that, we have no idea why it would come in incorrectly. Also, You don't necessarily need to use JSON.stringify+JSON.deserialize, that's usually wasteful programming.
    – sfdcfox
    Commented Sep 15, 2020 at 13:01
  • i am passing customerCurrency value form custom label. And since i am sending the request using HTTP i need to stringify them.Like below- Http http = new Http(); HttpRequest request = new HttpRequest(); request.setEndPoint('callout:Netsuite_Salesforce_Integration'); //external URL request.setBody(jsonBody); request.setMethod('POST'); Commented Sep 15, 2020 at 13:08
  • that label is having value as [2,3,4,5,6,8]. Commented Sep 15, 2020 at 13:09
  • You'd want to JSON.parse the label, then, as in JSON.parse(customerCurrency).
    – sfdcfox
    Commented Sep 15, 2020 at 13:11
  • 1
    finalArr['customer_currency'] = JSON.parse(customerCurrency); -- Sorry, had to edit this comment.
    – sfdcfox
    Commented Sep 15, 2020 at 13:52

1 Answer 1

1

You need to deserialize the label so that it becomes an object. It will then be serialized correctly later.

finalArr['customer_currency'] = JSON.parse(customerCurrency);
2
  • But since i am again doing JSON,stringify() to entire finalArr won't it convert again customer_currency value into string again,thinking that only i didn't go for parse. Commented Sep 15, 2020 at 13:58
  • 1
    @DeepakAgarwal Parsing makes it in to a real object, so when you stringify it later, it will be a JSON array with number values. Again, you don't necessarily need to stringify, it depends on how you wrote the Apex method.
    – sfdcfox
    Commented Sep 15, 2020 at 14:07

You must log in to answer this question.

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