最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Convert Nested JSON to Flat JSON - Stack Overflow

programmeradmin4浏览0评论

I am using javascript and I have nested json object getting from mongodb.

"abc": [
    {
      "a": "01AABCE2207R1Z5",
      "b": "Y",
      "c": [
        {
          "ca": "A",
          "cb": "AflJufPlFStqKBZ",
          "cc": "S008400"
         },
         {
          "cx": "A",
          "cy": "AflJufPlFStqKBZ",
          "cz": "S008400"
         }
        ]
     },

      {
      "a": "01AABCE2207R1Z5",
      "b": "Y",
      "c": [
        {
          "ca": "A",
          "cb": "AflJufPlFStqKBZ",
          "cc": "S008400"
         },
         {
          "cx": "A",
          "cy": "AflJufPlFStqKBZ",
          "cz": "S008400"
         }
        ]
     }
    ]

Above schema have fixed fields there will no changes in schema.

Now I want to make it as flat json array object and result should be like that. If c has multiple json object the it should create a new json object with the same a, b value

 [{
     "a": "01AABCE2207R1Z5",
     "b": "Y", 
     "ca": "A",
     "cb": "AflJufPlFStqKBZ",
     "cc": "S008400" 
    },
{
     "a": "01AABCE2207R1Z5",
     "b": "Y",  
     "cx": "A",
     "cy": "AflJufPlFStqKBZ",
     "cz": "S008400"
    },
    {
     "a": "01AABCE2207R1Z5",
     "b": "Y", 
     "ca": "A",
     "cb": "AflJufPlFStqKBZ",
     "cc": "S008400" 
    },
    {
     "a": "01AABCE2207R1Z5",
     "b": "Y",  
     "cx": "A",
     "cy": "AflJufPlFStqKBZ",
     "cz": "S008400"
    }
    ]

So, I want to know the fast and easy steps to make it flat. Please let me know the process and methods to solve this.

Thanks

I am using javascript and I have nested json object getting from mongodb.

"abc": [
    {
      "a": "01AABCE2207R1Z5",
      "b": "Y",
      "c": [
        {
          "ca": "A",
          "cb": "AflJufPlFStqKBZ",
          "cc": "S008400"
         },
         {
          "cx": "A",
          "cy": "AflJufPlFStqKBZ",
          "cz": "S008400"
         }
        ]
     },

      {
      "a": "01AABCE2207R1Z5",
      "b": "Y",
      "c": [
        {
          "ca": "A",
          "cb": "AflJufPlFStqKBZ",
          "cc": "S008400"
         },
         {
          "cx": "A",
          "cy": "AflJufPlFStqKBZ",
          "cz": "S008400"
         }
        ]
     }
    ]

Above schema have fixed fields there will no changes in schema.

Now I want to make it as flat json array object and result should be like that. If c has multiple json object the it should create a new json object with the same a, b value

 [{
     "a": "01AABCE2207R1Z5",
     "b": "Y", 
     "ca": "A",
     "cb": "AflJufPlFStqKBZ",
     "cc": "S008400" 
    },
{
     "a": "01AABCE2207R1Z5",
     "b": "Y",  
     "cx": "A",
     "cy": "AflJufPlFStqKBZ",
     "cz": "S008400"
    },
    {
     "a": "01AABCE2207R1Z5",
     "b": "Y", 
     "ca": "A",
     "cb": "AflJufPlFStqKBZ",
     "cc": "S008400" 
    },
    {
     "a": "01AABCE2207R1Z5",
     "b": "Y",  
     "cx": "A",
     "cy": "AflJufPlFStqKBZ",
     "cz": "S008400"
    }
    ]

So, I want to know the fast and easy steps to make it flat. Please let me know the process and methods to solve this.

Thanks

Share Improve this question edited Jun 20, 2017 at 9:24 Saurabh Sharma asked Jun 20, 2017 at 9:09 Saurabh SharmaSaurabh Sharma 8047 gold badges16 silver badges42 bronze badges 6
  • It's so easy to do... What did you try? – laurent Commented Jun 20, 2017 at 9:13
  • You have not even tagged this with a language you want to do this in ... And you should have at least an approach you e up with yourself to show us. – C3roe Commented Jun 20, 2017 at 9:13
  • Try npmjs./package/flat – Igor Popov Commented Jun 20, 2017 at 9:14
  • I am using javascript @CBroe. and I tried by using forloop but when I m doing it with forloop its became a lengthy process. – Saurabh Sharma Commented Jun 20, 2017 at 9:15
  • One loop over the main level items, and inside another loop that gets everything out of the c sub structure, places it on the same main level, afterwards remove the original c ... "how hard can it be?" – C3roe Commented Jun 20, 2017 at 9:18
 |  Show 1 more ment

2 Answers 2

Reset to default 5

It is so easy to do this.

var flatArray = [];
var flatObject = {};

for (var index = 0; index < data.length; index++) {
  for (var prop in data[index]) {

    var value = data[index][prop];

    if (Array.isArray(value)) {
      for (var i = 0; i < value.length; i++) {
        for (var inProp in value[i]) {
          flatObject[inProp] = value[i][inProp];
        }
      }
    }else{
        flatObject[prop] = value;
    }
  }
  flatArray.push(flatObject);
}

console.log(flatArray);

data is your array.

this will flat JSONObject even that includes JSONArray in it ...please use it ..will work

const flatJSONObject = r => {//flaten the Json in full depth
    const oc = ({}).constructor;
    const ac = ([]).constructor;
    var o={};
    if(r.constructor === oc) 
    {
          for (var k in r) {
            if ( r[k].constructor === oc) o = {...o,...flatJSONObject(value)}
            else if (  r[k].constructor === ac) r[k].forEach(e =>o = {...o,...flatJSONObject(e)});
            else o[k] =  r[k];
          } 
    }
    return o;
}
发布评论

评论列表(0)

  1. 暂无评论