简体   繁体   中英

How to convert flat JSON array into nested JSON with Google Apps Script?

I have a standalone google script project. I'm getting a flat JSON answer from fetch via some API.Actually it should be nested but not. My JSON has level number instead nests. For example:

 [ { level: 1, first_name: 'Sammy', last_name: 'Snow', cloth_num: 8, cloth: null, color: null, day: null, sales: 1000}, { level: 2, first_name: null, last_name: null, cloth_num: 3, cloth: 'shirt', color: 'red', day: null, sales: 300}, { level: 3, first_name: null, last_name: null, cloth_num: 1, cloth: null, color: null, day: 1, sales: 100}, { level: 3, first_name: null, last_name: null, cloth_num: 2, cloth: null, color: null, day: 2, sales: 200}, { level: 2, first_name: null, last_name: null, cloth_num: 5, cloth: 'jeans', color: 'blue', day: null, sales: 700}, { level: 3, first_name: null, last_name: null, cloth_num: 2, cloth: null, color: null, day: 1, sales: 300}, { level: 3, first_name: null, last_name: null, cloth_num: 3, cloth: null, color: null, day: 2, sales: 400}, { level: 1, first_name: 'Danny', last_name: 'Crow', cloth_num: 15, cloth: null, color: null, day: null, sales: 2000}, { level: 2, first_name: null, last_name: null, cloth_num: 5, cloth: 't-shirt', color: 'red', day: null, sales: 800}, { level: 3, first_name: null, last_name: null, cloth_num: 3, cloth: null, color: null, day: 1, sales: 500}, { level: 3, first_name: null, last_name: null, cloth_num: 2, cloth: null, color: null, day: 2, sales: 300}, { level: 2, first_name: null, last_name: null, cloth_num: 5, cloth: 'hat', color: 'blue', day: null, sales: 700}, { level: 3, first_name: null, last_name: null, cloth_num: 2, cloth: null, color: null, day: 1, sales: 300}, { level: 3, first_name: null, last_name: null, cloth_num: 3, cloth: null, color: null, day: 2, sales: 400}, { level: 2, first_name: null, last_name: null, cloth_num: 5, cloth: 'socks', color: 'blue', day: null, sales: 500}, { level: 3, first_name: null, last_name: null, cloth_num: 2, cloth: null, color: null, day: 1, sales: 300}, { level: 3, first_name: null, last_name: null, cloth_num: 3, cloth: null, color: null, day: 2, sales: 200} ]

I wrote a script to nest second level into first level:

 function convertFlat(dataq) { let map = new Map() let x = dataq.forEach(x => {if (x.level === 1) x.next_level=[], map.set(x.first_name,x)}) for(let i = 0; i < dataq.length; i++){ if(dataq[i].level === 1) { l = map.get(dataq[i].first_name); continue; } else l.next_level.push(dataq[i]) } console.log([...map.values()]) }

How can I nest third level into second? I'm very new in Google Apps Script and have no idea how to repeat this loop for second and third level.

Couldn't resist myself... Here's my suggested code:

function unFlat(fj) {
  var arr=[];
  var objL1, objL2, objL3;
  var prev_level;
  for(var i=0; i<fj.length;i++) {
    var obj=fj[i];
    switch(obj.level) {
      case 3:
        objL3={};
        objL3.cloth_num=obj.cloth_num;
        objL3.day=obj.day;
        objL3.sales=obj.sales;
        objL2.items.push(objL3);
        prev_level=3;
        break;
      case 2:
        objL2={};
        objL2.cloth_num=obj.cloth_num;
        objL2.cloth=obj.cloth;
        objL2.color=obj.color;
        objL2.sales=obj.sales;
        objL2.items=[];
        objL1.cloths.push(objL2);
        prev_level=2;
        break;
      case 1:
        if(prev_level==3) arr.push(objL1);
        objL1={};
        objL1.first_name=obj.first_name;
        objL1.last_name=obj.last_name;
        objL1.cloth_num=obj.cloth_num;
        objL1.sales=obj.sales;
        objL1.cloths=[];
        prev_level=1;
        break;
    }
  }
  if(fj.length) arr.push(objL1);
  return arr;
}

arr will hold objects at the customer level,
objL1, objL2, objL3 hold info about custome, cloths, and items, respectively.
prev_level starts as undefined which is not 3 , and is for to know when to push cusotmer/ ObjL1 into arr .
Note: when we're done with the flat json, we need to push the un-pushed-yet-constructed last cusotmer/ ObjL1 - " if " there was any information at all.
In each case we collect the relevant properties, and push up one level into it's array.
Hope this makes sense!

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