Roi Sadika
Roi Sadika

Reputation: 237

What is the best way to replace text in json?

So I have a bunch of JSON data and it contains a few fields. for example:

[{
    "id": "XXX",
    "version": 1,
    "head": {
        "text": "Main title",
        "sub": {
            "value": "next"
        },
        "place": "secondary"
    },
    "body": [{
            "id": "XXX1",
            "info": "three little birds",
            "extended": {
                "spl": {
                    "text": "song",
                    "type": {
                        "value": "a"
                    }
                }
            }
        },
        {
            "id": "XXX2",
            "info": [
                "how are you?"
            ],
            "extended": {
                "spl": {
                    "text": "just",
                    "non-type": {
                        "value": "abc"
                    }
                }
            }
        }
    ]
}]
  1. what I'm trying to do is kind of conversion table (from a different JSON file) if a field has the value 'a' replace it with 'some other text..' etc.

  2. I have a service for the JSON pipeline, so I guess this is the right place to do the replacement.

so for this example, I have the JSON above and in my conversion table I have the following terms:

next: forward, song: music, a: option1, just: from etc...

Upvotes: 0

Views: 5509

Answers (2)

Steven Spungin
Steven Spungin

Reputation: 29071

The best way is to parse it, replace the text in the object, and then stringify it.

The next best way is to use a regular expression.

In this example, I catch exceptions if path cannot be indexed, and use ['type'] instead of .type so it will scale to indexing 'non-type' if you wish.

const data = `[{
    "id": "XXX",
    "version": 1,
    "head": {
        "text": "Main title",
        "sub": {
            "value": "next"
        },
        "place": "secondary"
    },
    "body": [{
            "id": "XXX1",
            "info": "three little birds",
            "extended": {
                "spl": {
                    "text": "song",
                    "type": {
                        "value": "a"
                    }
                }
            }
        },
        {
            "id": "XXX2",
            "info": [
                "how are you?"
            ],
            "extended": {
                "spl": {
                    "text": "just",
                    "non-type": {
                        "value": "abc"
                    }
                }
            }
        }
    ]
}]
`

const o = JSON.parse(data)
o[0].body.forEach(b => {
  try {
    if (b.extended.spl['type'].value === 'a') {
      b.extended.spl['type'].value = 'CHANGED'
    }
  } catch (e) {}
})

const newData = JSON.stringify(o, null, 2)
console.log(newData)

A string replace approach will work if you know and can rely on your source conforming, such as the only "value" is inside "type"

const data = `[{
    "id": "XXX",
    "version": 1,
    "head": {
        "text": "Main title",
        "sub": {
            "value": "next"
        },
        "place": "secondary"
    },
    "body": [{
            "id": "XXX1",
            "info": "three little birds",
            "extended": {
                "spl": {
                    "text": "song",
                    "type": {
                        "value": "a"
                    }
                }
            }
        },
        {
            "id": "XXX2",
            "info": [
                "how are you?"
            ],
            "extended": {
                "spl": {
                    "text": "just",
                    "non-type": {
                        "value": "abc"
                    }
                }
            }
        }
    ]
}]
`

const newData = data.replace(/"value": "a"/g, '"value": "NEWVALUE"')

console.log(newData)

Upvotes: 1

sidcha
sidcha

Reputation: 659

What you are looking for can be achieved with templates. Replace the variable sections with some specific markers that you can find and replace from some external tools such as perl or sed.

For example, you could have a template.json with something like this:

...
"type": {
  "value": "@@VALUE@@"
}
...

Then when you need the actual JSON, you could pass this though an intermediate script that replaces these templates with actual data.

cat template.json | sed -e 's/@@VALUE@@/my_value/' > target.json

Alternatively, with Perl:

cat template.json | perl -pi -e 's:\@\@VALUE\@\@:my_value:' > target.json

Upvotes: 3

Related Questions