Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Javascript – How to Flatten a nested JSON object

I have a Nested JSON input as shown below which I want to flatten into expected output.
Key won’t be always the same eg: u_summary, created_date, u_product i.e. there could be even more keys.

Any help?

JSON Nested input:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

var jsonUnflattenInput = 
    [
        
        {
            "u_summary": {
                "display_value": "Please check the data and set the company accordingly if everything is fine.",
                "value": "Please check the data."
            },
            "created_date": {
                "display_value": "2022-06-18 11:45:00",
                "value": "2022-05-29 21:42:01"
            },
            "u_product": {
                "display_value": "Undefined",
                "value": "2345567"
            }
        },
        {
            "u_summary": {
                "display_value": "╓ tool v14.3",
                "value": "14.3"
                },
            "created_date": {
                "display_value": "2022-03-18 11:45:00",
                "value": "2022-02-29 21:42:01"
            },
            "u_product": {
                "display_value": "test_product",
                "value": "1367"
            }
        }
    ]

Expected JSON Output:

[
    {
        "dv_u_summary": "Please check the data and set the company accordingly if everything is fine.",
        "u_summary": "Please check the data.",
        "dv_created_date": "2022-06-18 11:45:00",
        "created_date": "2022-05-29 21:42:01",
        "dv_u_product": "Undefined",
        "u_product": "2345567"
    },
    {
        "dv_u_summary": "╓ tool v14.3",
        "u_summary": "14.3",
        "dv_created_date": "2022-03-18 11:45:00",
        "created_date": "2022-02-29 21:42:01",
        "dv_u_product": "test_product",
        "u_product": "1367"
    }
]

I tried the below code after referring to some blogs, but couldn’t get the expected output.

function flatten(json, flattened, str_key) {
                for (var key in json) {
                  if (json.hasOwnProperty(key)) {
                    if (json[key] instanceof Object && json[key] != "") {
                      flatten(json[key], flattened, str_key + "." + key);
                    } else {
                      flattened[str_key + "." + key] = json[key];
                    }
                  }
                }
            }

        var flattened = {};
         
        flatten(jsonUnflattenInput, flattened, "");
        

        for (var key in flattened){
          console.log(key + ": " + flattened[key]);
        }

>Solution :

function flatten(json) {
  var newJSON = [];
  for (var i = 0; i < json.length; i++) {
    var obj = {};
    for (var key in json[i]) {
      if (json[i].hasOwnProperty(key)) {
        obj['dv_' + key] = json[i][key]['display_value'];
        obj[key] = json[i][key]['value'];
      }
    }
    newJSON.push(obj);
  }
  return newJSON;
}

var jsonUnflattenInput = [

  {
    "u_summary": {
      "display_value": "Please check the data and set the company accordingly if everything is fine.",
      "value": "Please check the data."
    },
    "created_date": {
      "display_value": "2022-06-18 11:45:00",
      "value": "2022-05-29 21:42:01"
    },
    "u_product": {
      "display_value": "Undefined",
      "value": "2345567"
    }
  },
  {
    "u_summary": {
      "display_value": "╓ tool v14.3",
      "value": "14.3"
    },
    "created_date": {
      "display_value": "2022-03-18 11:45:00",
      "value": "2022-02-29 21:42:01"
    },
    "u_product": {
      "display_value": "test_product",
      "value": "1367"
    }
  }
]

console.log(flatten(jsonUnflattenInput))

This should work for the provided example data.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading