Home > Enterprise >  Change value of key at different level in json using node js/ javascript
Change value of key at different level in json using node js/ javascript

Time:09-14

I am trying to replace a specific value from the JSON file. let's suppose given below is my JSON data

sample.json

let sample={
  "yuiwedw":{
  "id":"yuiwedw",
  "loc": "ar",
  "body":{
    "data":"we got this",
    "loc":"ar",
    "system":{
      "fname":"admin",
      "lname":"admin",
      "details":[
       {
        "id":"Admin_01",
        "loc":"ar"
       },
       {
        "id":"Admin_02",
        "loc":"ar"
       }
       ]
      }
    }
  },
    "hbjds324":{
  "id":"hbjds324",
  "loc": "ar",
  "body":{
    "data":"testing",
    "loc":"ar",
    "system":{
      "fname":"public",
      "lname":"servent",
      "details":[
       {
        "id":"public_01",
        "loc":"ar"
       }
       ],
       "Available":true,
       "loc":"ar"
      }
    }
  }
}

for (const value of Object.values(
 sample )) {
value.loc = "en";
}

console.log(sample);

So I want to replace all the loc values which is ar with the en I can replace the value which at the first level but how to change all the loc value which is present at a different level.

CodePudding user response:

You could write a simple recursive function to handle this. This iterates over keys and if the value of a key is an object it calls itself. If the key is loc it replaces it value.

Here's jsfiddle with your sample.

Example:

function replaceLoc(obj){
    for(const key of Object.keys(obj)){
     if (typeof obj[key] === 'object'){
            replaceLoc(obj[key]);
     }
     if (key === 'loc'){
            obj[key] = 'en'
     }
  }
}

replaceLoc(sample);

let sample={
  "yuiwedw":{
  "id":"yuiwedw",
  "loc": "ar",
  "body":{
    "data":"we got this",
    "loc":"ar",
    "system":{
      "fname":"admin",
      "lname":"admin",
      "details":[
       {
        "id":"Admin_01",
        "loc":"ar"
       },
       {
        "id":"Admin_02",
        "loc":"ar"
       }
       ]
      }
    }
  },
    "hbjds324":{
  "id":"hbjds324",
  "loc": "ar",
  "body":{
    "data":"testing",
    "loc":"ar",
    "system":{
      "fname":"public",
      "lname":"servent",
      "details":[
       {
        "id":"public_01",
        "loc":"ar"
       }
       ],
       "Available":true,
       "loc":"ar"
      }
    }
  }
}


function replaceLoc(obj){
    for(const key of Object.keys(obj)){
     if (typeof obj[key] === 'object'){
            replaceLoc(obj[key]);
     }
     if (key === 'loc'){
            obj[key] = 'en'
     }
  }
}

replaceLoc(sample);

console.log(sample);

CodePudding user response:

To search and replace nested values you can use recursion (Make your for... into a function and call the function from within itself on all nested objects). Edit: the other answer shows how to do this.

You could also do it using regex. Example:


const regex = /"loc": *"ar"/g
const updatedSample = JSON.stringify(sample).replace(reg, `"loc":"ar"`);

Explanation: the regex finds "loc":"ar" with 0 to infinite spaces after the :. The JavaScript then replaces those with the value you give it. Here is an example showing how it works.

CodePudding user response:

Alternatively, You can convert a JSON object into a string, and replace all occurrences with regex and convert them back to JSON

const sampleString = JSON.stringify(sample);
const updatedSample = sampleString.replace(/"loc":"ar"/g, `"loc":"en"`);
const newSample = JSON.parse(updatedSample);
console.log(newSample);

Complete Example Below:

let sample = {
  yuiwedw: {
    id: "yuiwedw",
    loc: "ar",
    body: {
      data: "we got this",
      loc: "ar",
      system: {
        fname: "admin",
        lname: "admin",
        details: [
          {
            id: "Admin_01",
            loc: "ar"
          },
          {
            id: "Admin_02",
            loc: "ar"
          }
        ]
      }
    }
  },
  hbjds324: {
    id: "hbjds324",
    loc: "ar",
    body: {
      data: "testing",
      loc: "ar",
      system: {
        fname: "public",
        lname: "servent",
        details: [
          {
            id: "public_01",
            loc: "ar"
          }
        ],
        Available: true,
        loc: "ar"
      }
    }
  }
};

const sampleString = JSON.stringify(sample);
const updatedSample = sampleString.replace(/"loc":"ar"/g, `"loc":"en"`);
const newSample = JSON.parse(updatedSample);
console.log(newSample);

  • Related