取消嵌套嵌套的 JSON 对象

Unnest a nested JSON object

我下面有一个嵌套的 JSON 对象。

    var payload = {
      "name" : {
        "first" : "I am a",
        "last" : "Customer"
      },
      "product_info": {
        "name" : "Product 1",
        "category" : {
          "id" : "123456789",
          "name" : "C1",
          "sub_category" : {
            "id" : "321654987",
            "name" : "SC1"
          },
        }
      }};

    var param = JSON.stringify(payload);

我怎样才能取消 JSON 对象的嵌套,这样我就可以得到下面的 JSON 对象?

    var result = {
      "name.first" : "I am a",
      "name.last" : "Customer",
      "product_info.name" : "Product 1",
      "product_info.category.id" : "123456789",
      "product_info.category.name" : "C1",
      "product_info.category.sub_category.id" : "321654987",
      "product_info.category.sub_category.name" : "SC1"
    }

我试图使用 Object.keys(param).length 检查有多少嵌套对象,但如果没有嵌套对象,它将 return 值的长度。

如有任何建议,我们将不胜感激。

尝试使用下面的代码在任何嵌套级别展平您的 JSON 对象:

function myFunction(){
  var payload = {
      "name" : {
        "first" : "I am a",
        "last" : "Customer"
      },
      "product_info": {
        "name" : "Product 1",
        "category" : {
          "id" : "123456789",
          "name" : "C1",
          "sub_category" : {
            "id" : "321654987",
            "name" : "SC1"
          },
        }
      }};
      
  const flattenJSON = (payload = {}, res = {}, extraKey = '') => {
   for(key in payload){
      if(typeof payload[key] !== 'object'){
         res[extraKey + key] = payload[key];
      }else{
         flattenJSON(payload[key], res, `${extraKey}${key}.`);
      };
   };
   return res;
};
console.log(flattenJSON(payload));
};

基本上它正在重建您的 JSON 对象并将其分配给一个新变量,该变量是扁平化对象的第一层。该过程通过遍历对象来工作,如果它是对象的最后一个参数,它将添加一个具有值的新键。密钥是它经过的所有密钥的附加密钥。

这应该return未嵌套的JSON对象

参考link:https://www.tutorialspoint.com/flattening-a-json-object-in-javascript

遍历对象

function myFunction() {
  const ss = SpreadsheetApp.getActive();
  const sh = ss.getSheetByName('Sheet1');
  const s = '{"name":{"first":"Iama","last":"Customer"},"product_info":{"name":"Product1","category":{"id":"123456789","name":"C1","sub_category":{"id":"321654987","name":"SC1"}}}}';
  const obj = JSON.parse(s);
  traverse1(obj, process1);
}

function traverse1(o, func) {
  for (var i in o) {
    func.apply(this, [i, o[i]]);
    if (o[i] !== null && typeof (o[i]) == "object") {
      traverse(o[i], func);
    }
  }
}

function process1(key, value) {
  if (typeof (value) == "object") {
    Logger.log(key);
  } else {
    Logger.log("%s: %s ", key, value);
  }
}

Execution log
9:08:32 PM  Notice  Execution started
9:08:32 PM  Info    name
9:08:32 PM  Info    first: Iama 
9:08:32 PM  Info    last: Customer 
9:08:32 PM  Info    product_info
9:08:32 PM  Info    name: Product1 
9:08:32 PM  Info    category
9:08:32 PM  Info    id: 123456789 
9:08:32 PM  Info    name: C1 
9:08:32 PM  Info    sub_category
9:08:32 PM  Info    id: 321654987 
9:08:32 PM  Info    name: SC1 
9:08:33 PM  Notice  Execution completed