2

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

7
  • Better use appropiate JSON functions, depending on the language used. Commented Feb 1, 2016 at 14:00
  • I am unable to parse this String to JSON due to errors in the json. So actually speaking I am doing these operations on a String Commented Feb 1, 2016 at 14:01
  • so you want to surround 434832826 with " marks ? Commented Feb 1, 2016 at 14:03
  • Put the number in a capturing group and use this - e.g. replaceAll(":\\\s*(\\\d+)", ":\"$1\""); Commented Feb 1, 2016 at 14:03
  • Yes exactly I want to do the same Commented Feb 1, 2016 at 14:04

2 Answers 2

2

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

replaceAll(":\\\s*(\\\d+)", ":\"$1\"");
Sign up to request clarification or add additional context in comments.

Comments

2

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"
}

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.