0
votes

I want to remove extra quote from the below Json

{""id"":""1"", ""name"":""john"",""address"":"",""timestamp"":""2018/01/01 12:43:42 -700"",""dept"":""}

I am using the sed command for this:

sed -i -e 's/""/"/g' file.json

However it's not working as expected because the values for address and dept are just the empty string "" and I don't want to replace those by just a single " which would be a new JSON syntax error. I want to replace only the doubled double quotes around fields which contain an actual value. So I tried

sed -i -e 's/\""[a-z]+[0-9]+[.-]+\""/"[a-z]+[0-9]+[.-]+\"/g' file.json 

but it's not working either.

How can I achieve this?

1

1 Answers

2
votes

Assuming keys and values don't contain escaped commas or double-quotes:

sed -i 's/""\([^",]\+\)""/"\1"/g' file

[^",] matches any character but " and ,, \+ means one or more. enclosing it in escaped parentheses (\(\)) populates capturing group 1 (\1 in replacement string expands to it). So, ""\([^",]\+\)"" matches two double-quotes, followed by one or more characters which are not commas or double-quotes, followed by two double-quotes.