5

I wrote a sample code just to explain what I was after so...

Here is a working example:

var json='{"hezi":"firstName","gangina":"lastName"}',
    obj=JSON.parse(json);

alert(obj.gangina);

And here is the same exact code with line breaks and tabs (just to make the code more readable since in my real code the JSON array string is HUGE):

var json=
'
    {
        "hezi":"firstName",
        "gangina":"lastName"
    }
',
    obj=JSON.parse(json);

alert(obj.gangina);

I even tried to compensate with :

    obj=JSON.parse(json.replace(/(\r\n|\n|\r|\t)/gm,""));

So... Technically I can solve this issue by compressing my line (removing all \r\n|\n|\r|\t manually) but I'm quite sure there is a quick fix for that regardless beautifying my code.

Small tweak needed here...

6
  • 1
    Your second json is invalid string because strings can't have newlines in them in javascript. Commented Apr 18, 2015 at 12:53
  • You seem to be working under the assumption that line breaks are allowed in JavaScript strings. They are not, unless escaped with a \. Commented Apr 18, 2015 at 12:53
  • @jcubic So the only way to solve it is to compress it manually ? Commented Apr 18, 2015 at 12:56
  • It's nothing about compression, The problem is in your string definition. See jehna1's anwer below. Commented Apr 18, 2015 at 12:58
  • It has nothing to do with the line breaks in the JSON. It's because you're writing the JSON string in a JavaScript file. Your replace call won't even be evaluated because the problem occurs earlier. Commented Apr 18, 2015 at 12:58

5 Answers 5

7

JavaScript does not accept line breaks without escaping. You can fix this by escaping the line breaks:

var json=
'\
    {\
        "hezi":"firstName",\
        "gangina":"lastName"\
    }\
',
obj=JSON.parse(json);

alert(obj.gangina);
Sign up to request clarification or add additional context in comments.

4 Comments

I always used to terminate and concatenate strings, f me right?
@php_nub_qq Then me too :)
@php_nub_qq Well, results aren't equivalent. You do not want line breaks in there.
@php_nub_qq you can also make an array and join with empty string ;)
3

I suppose you want to 'pretty print' long JSON-strings?

In that case: you can use the space parameter in JSON.stringify to display a long JSON-string formatted. See MDN. For example:

var json = '{"hezi":"firstName","gangina":"lastName"}'
    obj = JSON.parse(json);

document.querySelector('#result').innerHTML = JSON.stringify(obj, null, ' ');
<pre id="result"></pre>

Comments

3

You need to use line breaks to achieve this but escaping is not recommended due to it being easy to escape a space by mistake which would throw a formatting error.

Use..

var json=
'{'+
 '"hezi":"firstName",'+
 '"gangina":"lastName"'+
'}',
    obj=JSON.parse(json);

alert(obj.gangina);

But my question would be why use json in the 1st place, if hand coding use an object then this wouldn't be an issue, json data should only be loaded from external source.

Comments

2

ES6 template strings are now supported in Chrome 41+ and Firefox 34+. It's time to know about it.

JSON.parse(`
   {
        "hezi":"firstName",
        "gangina":"lastName"
    }
`);

1 Comment

Well... As I state on my last sentence on this current post : "Small tweak needed here... " I guess that you are DE man that I was looking for! Thanks bro! :}
2

Just to clear up the confusion. The error is here:

var json=
'
    {

On line 2, there's a quote: the beginning of a string. Then an unescaped line break. That causes a SyntaxError and none of your code is ever executed. In particular, JSON.parse isn't executed.

Comments

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.