skip to Main Content

I have particular field – "dataQuery". I want to move the entire content inside this field to one level up and finally delete this key itself. The original JSON is nested and can be upto 3-4MB in size. What are some possible ways to do this ?
As an example the following JSON:

{
  "name": "John",
  "age": 25,
  "value": {
        "dataQuery": {
             "nestedField1": "value1",
             "duplicateKey": "value1",
             "dataQuery": {
                 "nestedField2": "value3",
                 "duplicateKey": "value2"
              }
         },
         "child": [{
                "nestedField3": {
                    "dataQuery": { 
                         "nestedField4": "value4"
                     }
                }
         }] 
    }
}

should be transformed to the following:

{
  "name": "John",
  "age": 25,
  "value": {
             "nestedField1": "value1",
             "nestedField2": "value3",
             "duplicateKey": "value2"
         ,
         "child": [{
                "nestedField3": { 
                     "nestedField4": "value4"
                }
         }] 
    }
}

2

Answers


  1. How about something like:

    let myObj = {
      name: 'John',
      age: 25,
      value: {
    dataQuery: {
      nestedField1: 'value1',
      duplicateKey: 'value1',
      dataQuery: {
        nestedField2: 'value3',
        duplicateKey: 'value2',
      },
    },
    child: [
      {
        nestedField3: {
          dataQuery: {
            nestedField4: 'value4',
          },
        },
      },
    ],
      },
    };
    
    const collapseDataQuery = (obj) => {
      let retVal = {};
      Object.keys(obj).forEach((key) => {
    if (key === 'dataQuery' && typeof obj[key] === 'object') {
        retVal = { ...retVal, ...collapseDataQuery(obj[key]) };
    } else if (Array.isArray(obj[key])) {
      retVal[key] = obj[key].map((item) => collapseDataQuery(item));
    } else if (typeof obj[key] === 'object') {
      retVal[key] = collapseDataQuery(obj[key]);
    } else {
      retVal[key] = obj[key];
    }
      });
      return retVal;
    };
    
    console.log('Before:');
    console.log(JSON.stringify(myObj, null, 2));
    
    myObj.value = collapseDataQuery(myObj.value);
    
    console.log('-------------------');
    console.log('After:');
    console.log(JSON.stringify(myObj, null, 2));
    .as-console-wrapper { min-height: 100% !important; }
    Login or Signup to reply.
  2. You could use recursion to iterate through every object/array (obj) and compare the reference key (refKey) to the target key (targetKey).

    If your reference key is the target key (targetKey), assign the current object (obj) to the parent (parent) and delete the target key (targetKey) off of the parent (parent).

    The recursion is done before the removal occurs (post-order traversal).

    const data = {
      "name": "John",
      "age": 25,
      "value": {
        "dataQuery": {
          "nestedField1": "value1",
          "duplicateKey": "value1",
          "dataQuery": {
            "nestedField2": "value3",
            "duplicateKey": "value2"
          }
        },
        "child": [{
          "nestedField3": {
            "dataQuery": {
              "nestedField4": "value4"
            }
          }
        }]
      }
    };
    
    const pruneObject = (obj, targetKey, refKey = null, parent = null) => {
      if (typeof obj === 'object') {
        for (let k in obj) { pruneObject(obj[k], targetKey, k, obj); }
      }
      if (refKey === targetKey) {
        Object.assign(parent, obj); // Assign the object to its parent
        delete parent[targetKey]; // Remove the target association
      }
    };
    
    pruneObject(data, 'dataQuery'); // Modifies the object in-place
    console.log(data); // Display the modified object
    .as-console-wrapper { top: 0; max-height: 100% !important; }

    Output

    {
      "name": "John",
      "age": 25,
      "value": {
        "child": [
          {
            "nestedField3": {
              "nestedField4": "value4"
            }
          }
        ],
        "nestedField1": "value1",
        "duplicateKey": "value2",
        "nestedField2": "value3"
      }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search