0

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.

1

3 Answers 3

1

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);

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

1 Comment

typeof obj.key should be typeof obj[key]
1

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.

1 Comment

@pilchard Nice! Updated.
1

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);

Comments

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.