1

I have a invalid json string like this:

[{
        "itemID": "1",
        "itemTitle": "Mango",
        "itemText": "some text here"again text",
        "ThumbUrl": "http://someurl.com/mango.jpg",
        "itemContent": null
    }, {
        "itemID": "2",
        "itemTitle": "orange",
        "itemText": "someother text " again another texther",
        "ThumbUrl": "http://someurl.com/orange.jpg",
        "itemContent": null
    }

]

javascript:

$.get("http://www.someapiurl.com/getdata.php", function(data, status){ 

//here i want to replace json key value to empty before parsing the json  
     var json = $.parseJSON(data);

}

I want to change the value of itemText to word empty using regular expression. Could any one help me how to achieve this using regular expression ?Thanks

Note:

-JSON is invalid (That is the way i recive it so i have to correct it before parsing it

-The Json response some time got double quotes in itemText)

-the itemText key value is across multiple lines(mix of unicode and non unicode)and long(not on online line)

Edit: I have used this php regular expression to achive same thing. Could you guys help me convert it to javascript regular expresion?

print_r(preg_replace('/\"itemText\"\:\".*?\"\,/s', '"textTitle$1":"empty",',$json));

Edit2: Finally this replaced itemText with word empty for me in all cases:

 data.replace(/("itemText"\s*:\s*")[\s\S]*?ThumbUrl/g, '$1empty","ThumbUrl')
7
  • 1
    If your JSON is formatted like this, .replace(/("itemText"\s*:\s*").*",/, '$1"') Commented Oct 3, 2016 at 19:42
  • Thanks for reply . It only made the itemText empty for first one without quote at the end! How to make "itemText": "empty", for all the json items not just the first one Commented Oct 3, 2016 at 19:52
  • 2
    I feel the question needs to be asked... why can't the system that outputs invalid data be fixed? Commented Oct 3, 2016 at 19:53
  • You may add /g: .replace(/("itemText"\s*:\s*").*",/g, '$1"') - does it help? Commented Oct 3, 2016 at 20:02
  • 3
    Why don't you fix the server instead of a hack on the client? Commented Oct 3, 2016 at 20:15

2 Answers 2

2

The right approach is to ask your data provider to fix the issue on their side.

As a temporary workaround, you may use

.replace(/("itemText[12]"\s*:\s*")[\s\S]*?",/g, '$1empty"')

See the regex demo

var regex = /("itemText[12]"\s*:\s*")[\s\S]*?",/g;
var str = `[{
        "itemID": "1",
        "itemTitle": "Mango",
        "itemText1": "some text here"again text",
        "ThumbUrl": "http://someurl.com/mango.jpg",
        "itemContent": null
    }, {
        "itemID": "2",
        "itemTitle": "orange",
        "itemText2": "someother text " again another texther",
        "ThumbUrl": "http://someurl.com/orange.jpg",
        "itemContent": null
    }

]`;
var subst = '$1empty"';
var result = str.replace(regex, subst);
console.log(result);

Details:

  • ("itemText[12]"\s*:\s*") - Group 1 capturing
    • "itemText - literal text "itemText,
    • [12] - 1 or 2
    • " - a double quote
    • \s*:\s* - 0+ whitespaces, :, and again 0+ whitespaces
  • [\s\S]*? - any 0+ chars as few as possible up to the first
  • ", - a double quote and a comma.
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for helping me. your code works until there is no ",(quote and comma after it) in the itemText but when there is ",(quote and comma after it) in the itemText it fails. Is it possible to change the regular expression to remove anything between itemText": "......."ThumbUrl" with itemText:"Empty", "ThumbUrl"? So that way the extra(",) will not create problem ?
Is "ThumbUrl" optional? If not, just add it - ("itemText[12]"\s*:\s*")[\s\S]*?"ThumbUrl",. Feel free to edit the regex fiddle. If it is optional, wrap with an optional group like (?:ThumbUrl")?
0

Finally this replaced itemText with word empty for me in all cases:

data.replace(/("itemText"\s*:\s*")[\s\S]*?ThumbUrl/g, '$1empty","ThumbUrl')

1 Comment

It seems my answer works for you. Please consider accepting it.

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.