Unable to get proper JSON from JS Object

Hi All,

Any thoughts on how to get a proper JSON object out from below? I am getting JS object just fine but for some reason using JSON.stringify is encoding the double qoutes.

lv_json = {"profile.login":"Valian@mail.com", "profile.OrgManPositionLevel":"PRODUCT", "profile.OrgManPositionCode":"ED100000R", "profile.email":"Valian@mail.com", "profile.OrgManSubteamName":"PRODUCT"}
  //Object is ready, now pass it to unflatten FM
  //lr_result = unflatten(lv_flattend_json);
  lr_result = unflatten(lv_json);
  //lr_json1 = JSON.parse(lr_result);
  //lr_string = JSON.parse(JSON.stringify(lr_result));
  lr_string = JSON.stringify(lr_result);
  lr_string = JSON.stringify(lr_string).replace(/\"/gi, "");

function unflatten(data) {
  //console.log("inside the unflatten........");
  "use strict";
  if (Object(data) !== data || Array.isArray(data))
    return data;
  var regex = /\.?([^.\[\]]+)|\[(\d+)\]/g,
    resultholder = {};
  for (var p in data) {
    var cur = resultholder,
      prop = "",
    while (m = regex.exec(p)) {
      cur = cur[prop] || (cur[prop] = (m[2] ? [] : {}));
      prop = m[2] || m[1];
    cur[prop] = data[p];
  //console.log("Exiting the unflatten........");
  return resultholder[""] || resultholder;

Current Output :

  profile: {
    email: "Valian@mail.com",
    login: "Valian@mail.com",
    OrgManPositionCode: "ED100000R",
    OrgManPositionLevel: "PRODUCT",
    OrgManSubteamName: "PRODUCT"

Output needed:

  "profile": {
    "email": "Valian@mail.com",
    "login": "Valian@mail.com",
    "OrgManPositionCode": "ED100000R",
    "OrgManPositionLevel": "PRODUCT",
    "OrgManSubteamName": "PRODUCT"

with this:

let lr_string = JSON.stringify(lr_result, null, 2);

the output is:

  "profile": {
    "login": "Valian@mail.com",
    "OrgManPositionLevel": "PRODUCT",
    "OrgManPositionCode": "ED100000R",
    "email": "Valian@mail.com",
    "OrgManSubteamName": "PRODUCT"

The JSON.stringify takes more arguments than the data to stringify, and those arguments influence the output

1 Like

Thanks so much for your help. Looks like the output is below in my case. I can now play with it on my own and make it work. But one question I do have is around the best practice of it - whats special in my scenario that I have to use these techniques to massage the data , as in like why JSON.stringify cant just convert this to JSON properly in this case. I have used in the past and never faced this issue.

Current output after using JSON.stringify(lr_result, null, 2);

  \"profile\": {
    \"login\": \"Valian@mail.com\",
    \"OrgManPositionLevel\": \"PRODUCT\",
    \"OrgManPositionCode\": \"ED100000R\",
    \"email\": \"Valian@mail.com\",
    \"OrgManSubteamName\": \"PRODUCT\"

Edit - Should have mentioned, this is where I am seeing the output - https://jsfiddle.net/jainjank/y7jtad1v/108/

depends on what accepts the data - some things expect strict rules from the data they get

Thanks again for your cooperation.