Search code examples
javascriptjsonnested-loops

Nested array in JSON object, add key if not exists and value as empty


Currently, I have a nested array in JSON object. I would like to define the key and value for the following:

  • contact as "phone": "", "email" : "", "fax": "", "website": ""
  • address as "city": "", "country": "", "house_number": "", "street name": ""

If one of these keys is missing, it needs to be filled as a default key and value as shown in the expected input.

Could anyone help how to do it in JavaScript?

Current Input:

{
  "data": [
    {
      "name": "John",
      "contact": {
        "phone": "987-654-3210",
        "email": "[email protected]"
      },
      "address": {
        "city": "Berlin",
        "country": "Germany"
      }
    }
  ]
}

Expected Input:

{
  "data": [
    {
      "name": "John",
      "contact": {
        "phone": "987-654-3210",
        "email": "[email protected]",
        "fax": "",
        "website": ""
      },
      "address": {
        "city": "Berlin",
        "country": "Germany",
        "house_number": "",
        "street name": ""
      }
    }
  ]
}

Solution

  • If you don't mind to mutate your original array, here's one solution:

    const templates = {
      contact: {
        "phone": "",
        "email": "",
        "fax": "",
        "website": ""
      },
      address: {
        "city": "",
        "country": "",
        "house_number": "",
        "street name": ""
      }
    }
    
    source.data.forEach(entry => Object.entries(templates).forEach(([key, template]) => {
      entry[key] = entry[key] || {};
      Object.entries(template).forEach(([k, v]) => {
        entry[key][k] = entry[key][k] || v;
      });
    }));
    

    const source = {
      "data": [{
        "name": "John",
        "contact": {
          "phone": "987-654-3210",
          "email": "[email protected]"
        },
        "address": {
          "city": "Berlin",
          "country": "Germany"
        }
      }, {
        "name": "Bob",
        "contact": {
          "phone": "123-456-7890",
          "fax": "9876-5432",
          "website": "bob.example.com"
        },
        "address": {
          "city": "NYC",
          "country": "USA",
          "house_number": "701A"
        }
      }]
    };
    
    const templates = {
      contact: {
        "phone": "",
        "email": "",
        "fax": "",
        "website": ""
      },
      address: {
        "city": "",
        "country": "",
        "house_number": "",
        "street name": ""
      }
    }
    
    source.data.forEach(entry => Object.entries(templates).forEach(([key, template]) => {
      entry[key] = entry[key] || {};
      Object.entries(template).forEach(([k, v]) => {
        entry[key][k] = entry[key][k] || v;
      });
    }));
    
    console.log(source);