简体   繁体   中英

Change the key name to be the key value structure of a nested JSON

I'm trying to clean a JSON that i request from an API, because the DB insert a key name, key value structure and mess everything, unfortunate i don't have access to the back, so i have to clean the JSON in the front, one of the problems is that the data not always is the same structure, so, i need a recursive solution to iterate in all the nested JSON, the real JSON have more nested childs, but for demonstration purpose i show only a part of the JSON.

This is an example of the structure, but sometimes the structure is not the same, sometimes i don't get the population or one of the values of the population.

[
  { "Name": "code", "Value": "18187" },
  { "Name": "date", "Value": "2020-05-10" },
  {
    "Name": "countries",
    "Value": [
      [
        { "Name": "id", "Value": "1" },
        { "Name": "name", "Value": "Canada" },
        {
          "Name": "population",
          "Value": [
            [
              { "Name": "id", "Value": "1" },
              { "Name": "male", "Value": "1000" }
            ],
            [
              { "Name": "id", "Value": "2" },
              { "Name": "female", "Value": "1000" }
            ]
          ]
        }
      ],
      [
        { "Name": "id", "Value": "2" },
        { "Name": "name", "Value": "Italy" },
        {
          "Name": "population",
          "Value": [
            [
              { "Name": "id", "Value": "1" },
              { "Name": "male", "Value": "1000" }
            ],
            [
              { "Name": "id", "Value": "2" },
              { "Name": "female", "Value": "1000" }
            ]
          ]
        }
      ]
    ]
  }
]

And i'm trying to clean all the key name, key value, to get something like this:

[
  { "code": "18187" },
  { "date": "2020-05-10" },
  {
    "countries": [
      [
        { "id": "1" },
        { "name": "Canada" },
        {
          "population": [
            { "male": "1000" },
            { "female": "1000" }
          ]
        }
      ],
      [
        { "id": "2" },
        { "name": "Italy" },
        {
          "population": [
            { "male": "1000" },
            { "female": "1000" }
          ]
        }
      ]
    ]
  }
]

You could do with recursive function that uses map method.

 const data = [{"Name":"code","Value":"18187"},{"Name":"date","Value":"2020-05-10"},{"Name":"countries","Value":[[{"Name":"id","Value":"1"},{"Name":"name","Value":"Canada"},{"Name":"population","Value":[[{"Name":"id","Value":"1"},{"Name":"male","Value":"1000"}],[{"Name":"id","Value":"2"},{"Name":"female","Value":"1000"}]]}],[{"Name":"id","Value":"2"},{"Name":"name","Value":"Italy"},{"Name":"population","Value":[[{"Name":"id","Value":"1"},{"Name":"male","Value":"1000"}],[{"Name":"id","Value":"2"},{"Name":"female","Value":"1000"}]]}]]}] function modify(data) { return data.map(e => { if (Array.isArray(e)) return modify(e) else { const { Name, Value } = e; return ({ [Name]: Array.isArray(Value)? modify(Value): Value }) } }) } const result = modify(data); console.log(result)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM