Sathiya Narayanan
Sathiya Narayanan

Reputation: 641

Want to replace the string a number using regex

I have a json string which has the below value

"appId": 434832826

I want to add double quotes around the number so that the json becomes valid.

I tried replaceAll(":\\\s\\\d+", ":\"$0\"");

But it is replacing the value as

"appId":": 434832826"

I am not sure if this isthe correct regex. An help is much appreciated. Thanks in advance

Upvotes: 2

Views: 640

Answers (2)

Jan
Jan

Reputation: 43169

Put the number in a capturing group and use the following regex:

replaceAll(":\\\s*(\\\d+)", ":\"$1\"");

Upvotes: 2

hek2mgl
hek2mgl

Reputation: 157967

You can use jq:

jq '.appId|=tostring' input.json

Imagine you have the following json:

{
    "appId": 434832826,
    "foo": "bar"
}

The above command would produce:

{
  "appId": "434832826",
  "foo": "bar"
}

Upvotes: 2

Related Questions