How to replace JSON values double quotes with single quotes with Regex

Viewed 28

I use an old legacy framework that connects to a non-relational DB that can only receive a JSON string like this:

'{
"id":'123',
"name":'myZoo',
"ticketPrice":66.5,
"animals":['lion','zebra','wolf']
}'

I have this JSON string:

'{
"id":"123",
"name":"myZoo",
"ticketPrice":66.5,
"animals":["lion","zebra","wolf"]
}'

Using regex, I want to replace double quotes " with single quotes ' of the JSON values only ( the keys must have double quotes ).

the reason I want to use regex is because my framework cant handle JSON so I need to use it as string.

For a JSON without arrays I have this code:

/(?<=:)"((?:\\.|[^\\])*?)"/g

But, I cant make it work with arrays.

1 Answers

So I think what you mean by saying it doesn't work with arrays is the case of the "animals" key. Could you just check if the JSON value is an array then use apply your regex to each individual element in the array then build a new array with your new elements?

Related