1

I have read the post for this an yet I still don't know what is happening.

My var "text" is valid according to most JSON online data checker but when I execute the parse it doesn't do anything.

Here is an example code:

<!DOCTYPE html>
<html>
<body>

<h2>Create Object from JSON String</h2>

<p id="demo"></p>

<script>
var text = '{
"zipcodes": [
    {
        "zip": "22312",
        "city": "Alexandria",
        "state": "VA"
    },
    {
        "zip": "22030",
        "city": "Fairfax",
        "state": "VA"
    },
    {
        "zip": "22301",
        "city": "Tyson's Corner",
        "state": "VA"
    },
    {
        "zip": "20148",
        "city": "Ashburn",
        "state": "VA"
    }
]}';

obj = JSON.parse(text);
document.getElementById("demo").innerHTML =
obj.zipcodes[1].zip + " " + obj.zipcodes[1].city;
</script>

</body>
</html>
5
  • You dont have any errors in your console (something like, Uncaught SyntaxError: Unexpected token ILLEGAL...? And why are you trying to manually create a stringy object? Just create the object! Commented Mar 11, 2015 at 15:50
  • 2
    Is that "text" variable an example or what? You don't really need to parse a json, as long as you're already writing an object inside that. jsfiddle.net/prssq57w Commented Mar 11, 2015 at 15:52
  • make Tyson's Corner become Tyson\'s Corner Commented Mar 11, 2015 at 15:52
  • Don't build json by hand. If you want a test json string, make a javascript object and stringify it. Commented Mar 11, 2015 at 16:02
  • just trying it in its string form, later I will have to put the json file in a server and call it. Was just concerned about the fact that the JSON.parse can't read it. It was the un-escaped (') that caused the commotion. UGH Commented Mar 11, 2015 at 16:04

3 Answers 3

3

You have two issues:

  1. JavaScript doesn't support multi-line strings without marking each line with a continuation (via \ at the end of the line). I've done this in my example but it's really easier to include your JSON on a single line.

  2. You're using single-quotes for your string but your string contains an un-escaped '. Escape it, and you're fine.

var text = '{ \
  "zipcodes": [\
    { \
      "zip": "22312", \
      "city": "Alexandria", \
      "state": "VA" \
    }, \
    { \
      "zip": "22030", \
      "city": "Fairfax", \
      "state": "VA" \
    }, \
    { \
      "zip": "22301", \
      "city": "Tyson\'s Corner", \
      "state": "VA" \
    }, \
    { \
      "zip": "20148", \
      "city": "Ashburn", \
      "state": "VA" \
    } \
  ]}';

obj = JSON.parse(text);
document.getElementById("demo").innerHTML =
obj.zipcodes[1].zip + " " + obj.zipcodes[1].city;
<div id='demo'></div>

Sign up to request clarification or add additional context in comments.

Comments

2

You have to put the json on one line and escape your '.

"Tyson\'s Corner"

http://jsbin.com/xumiba/1/edit?html,js,console,output

var text = '{ "zipcodes": [ { "zip": "22312", "city": "Alexandria", "state": "VA" }, { "zip": "22030", "city": "Fairfax", "state": "VA" }, { "zip": "22301", "city": "Tyson\'s Corner", "state": "VA" }, { "zip": "20148", "city":"Ashburn", "state": "VA" }]}';

5 Comments

Or leave it on multiple lines and escape the line returns.
It does not have to be in one line. ES6 supports multi-line template strings - that isn't necessarily an issue.
@TIMINeutron Just saying its not "you HAVE to". It's you probably SHOULD since ES6 isn't yet supported everywhere but you can still get around that with other libraries "have to"
@peterdotjs Sorry, what I really meant is "It's not supported today and it should never ever be supported". That is screaming "go ahead and be a lousy programmer", not really best-practices material.
@TIMINeutron LOL. Well per the React mindset "Rethinking best practices" :p
0

You need to add \ at the end of every string new line and escape the ' character. Something like this will do:

var text = '{\
"zipcodes": [\
    {\
        "zip": "22312",\
        "city": "Alexandria",\
        "state": "VA"\
    },\
    {\
        "zip": "22030",\
        "city": "Fairfax",\
        "state": "VA"\
    },\
    {\
        "zip": "22301",\
        "city": "Tyson\'s Corner",\
        "state": "VA"\
    },\
    {\
        "zip": "20148",\
        "city": "Ashburn",\
        "state": "VA"\
    }\
]}';

obj = JSON.parse(text);

console.log(obj.zipcodes[1].zip + " " + obj.zipcodes[1].city);
document.getElementById("demo").innerHTML =
obj.zipcodes[1].zip + " " + obj.zipcodes[1].city;

Here is a jsfiddle link:

http://jsfiddle.net/0retway7/

1 Comment

This will work but will not be a valid json object later

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.