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

Saurabh Sharma
  • 804
  • 6
  • 16
  • 42
  • It's so easy to do... What did you try? – laurent Jun 20 '17 at 09: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 come up with yourself to show us. – CBroe Jun 20 '17 at 09:13
  • Try https://www.npmjs.com/package/flat – Igor Popov Jun 20 '17 at 09: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 Jun 20 '17 at 09: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?"_ – CBroe Jun 20 '17 at 09:18
  • main level `abc` can have multiple objects in array, then `c` have multiple json objects – Saurabh Sharma Jun 20 '17 at 09:20

2 Answers2

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.

Nuwan.Niroshana
  • 407
  • 4
  • 15
0

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;
}
Hans Felix Ramos
  • 4,264
  • 3
  • 16
  • 40