Battery
Battery

Reputation: 397

Convert JSON key value using RegExp

I receive a JSON string from a server call, in this form:

{"0":{"jpgN":"2","spread_value":"392.22","relevant_new":"text"},"1":{"jpgN":"1","spread_value":"395.28","relevant_new":"text"},"count":2}

Using RegExp, is there any way to replace the value of any key jpgN with the string "http://mydomain.com/keyValue.jpg" (eg "http://mydomain.com/2.jpg")?

Upvotes: 1

Views: 1201

Answers (2)

Pavel Strakhov
Pavel Strakhov

Reputation: 40492

Replace "jpgN":"([^"]+)" to "jpgN":"http://mydomain.com/$1.jpg".

But it's better to parse json and change values using your programming language.

Upvotes: 2

Romain Meresse
Romain Meresse

Reputation: 3044

Something like that ?

var json = {"0":{"jpgN":"2","spread_value":"392.22","relevant_new":"text"},"1":{"jpgN":"1","spread_value":"395.28","relevant_new":"text"},"count":2};
var s = JSON.stringify(json);

s.replace(/\"jpgN\":\"(\w+)\"/g, "\"jpgN\":\"http://mydomain.com/$1.jpg\"");

Upvotes: 1

Related Questions