1

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.

2
  • Practice "recursive" functions. Add full expected output for your json sample. Commented Jul 3, 2020 at 3:24
  • Please share the expected output Commented Jul 3, 2020 at 3:36

1 Answer 1

2

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!

Sign up to request clarification or add additional context in comments.

7 Comments

Thank you for your reply! It works. How can I modify it to get new line delimited?
You are welcome! "new line"? "Delimited"? Please explain.
You could have mentioned me... I'm not yet familiar with blobs and file i/o... Again, what's this "new line"? "Delimited"?
Thank you. Not for me for the time being... Good luck!
Could you, please, up-vote, thank, and select my answer?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.