2

I am puzzled by this. I read many posts on SO, but strangely I cannot find one solution for my situation.

I have the following very simple VALID json string. Note that it contains a double quote in attribute value.

{"text":"\"xxxxx"}

You can verify that it is a valid Json string at jsonlint

Now I hope to parse the above string with jQuery.parseJSON into an object the following way:

jQuery.parseJSON('{"text":"\"xxxxx"}');

I get error. Can someone let me know what I miss?

Thanks a lot!!!

Background

I am doing a web application. In the backend (Java+Spring), I use Google's Gson to parse a complex object into a Json string. This string is then passed to the fontend (Javascript) as a string. Now I need to convert the string back into a Json object. I cannot change this technical approach.

Update 1 The following is actual code generated as part of the page when the page is load (I can see it in View Source in Firefox)

<script>
var data='{"defaultLocale":"en","answers":[{"text":"\"xxxx"}.......
</script>
7
  • @MarcosPérezGude already answered that. Before passing json to front end, escape backslashes: json.replace('"', '\\"').replace('\\', '\\\\'); Commented Sep 12, 2015 at 20:17
  • @ankhzet, thanks for chiming in! Please see my response to MarcosPérezGude's answer. Commented Sep 12, 2015 at 20:23
  • You, probably, wrong. If you builded JSON in backend as '{"text":"\"xxxxx"}', then to front-end it is delivered already broken: '{"text":""xxxxx"}'. It's can't be fixed at front-end side. show us code, where front-end receives the json, maybe we can debug it there to find out if it's true Commented Sep 12, 2015 at 20:36
  • 1
    No, it isn't. {"text":"\"xxxx" will be translated by JS engine as {"text":""xxxx". See, broken. Commented Sep 12, 2015 at 20:59
  • 1
    try to remove surrounding singlequote, so data='{...}'; becomes data={...}; Commented Sep 12, 2015 at 21:00

2 Answers 2

1

The problem was with JSON-encoded object, injected into script as string and then JSON-decoded.

<script>
var data='{"defaultLocale":"en","answers":[{"text":"\"xxxx"} ... }';
</script>

So, as long as JSON-encoded object string has a valid javascript syntax (as intended, obviously), it can be injected directly, without string -> JSON-decode step:

<script>
var data = {"defaultLocale":"en","answers":[{"text":"\"xxxx"} ... };
</script>
Sign up to request clarification or add additional context in comments.

Comments

1

The JSON is valid, but when you parse it the escape values are lost. This is the valid parse JSON:

jQuery.parseJSON('{"text":"\\\"xxxxx"}');

2 Comments

See my update to the post. The Json string actually is a lot more complex and I don't wish do this type of "\\" thing in the backend if there is a frontend solution . Thanks for your input!
You need to get in front end the valid json, you are receiving an invalid string. Share us your code as @ankhzet said in the post's coments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.