Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

How do I parse this JSON?

[
    {
        "name": "Emma de Milliano",
        "sex": "f"
    },
    {
        "name": "Carolus Haverbeke",
        "sex": "m"
    }
]

I know above is valid JSON. Now, if I want to parse this, how do I do it? I tried making the whole thing a string by surrounding it in quotation marks, but when I pass the result in as an argument to JSON.parse(), I get an error.

I figured it out. I was trying to parse the JSON in the console without the code all on a single line. It was formatted just like it was above, and for some reason the console did not read it as a string. When I deleted the new lines and condensed the code to one line, it worked.

1 Answer

Not sure what you were trying to do exactly, but would something like this work?

var employees = [
    {"Name":"Emma de Milliano", "Sex":"F"},
    {"Name":"Carolus Haverbeke", "Sex":"M"}
];
console.log(JSON.stringify(employees))