1
{
    "status": 1,
    "complete": 1,
    "list": {
        "378144457": {
            "item_id": "378144457",
            "resolved_id": "378144457",
            "given_url": "https://ngrok.com",
            "given_title": "",
            "favorite": "0",
            "status": "0",
            "time_added": "1372274736",
            "time_updated": "1372274741",
            "time_read": "0",
            "time_favorited": "0",
            "sort_id": 3,
            "resolved_title": "Introspected tunnels to localhost",
            "resolved_url": "https://ngrok.com",
            "excerpt": "ngrok creates a tunnel from the public internet (http://subdomain.ngrok.com) to a port on your local machine. You can give this URL to anyone to allow them to try out a web site you're developing without doing any deployment.  ngrok captures all traffic through the tunnel.",
            "is_article": "0",
            "is_index": "1",
            "has_video": "0",
            "has_image": "1",
            "word_count": "422"
        },
        "380099389": {
            "item_id": "380099389",
            "resolved_id": "380099389",
            "given_url": "http://physics.aps.org/articles/v6/69",
            "given_title": "",
            "favorite": "0",
            "status": "0",
            "time_added": "1371518628",
            "time_updated": "1371518632",
            "time_read": "0",
            "time_favorited": "0",
            "sort_id": 5,
            "resolved_title": "Viewpoint: New Particle Hints at Four-Quark Matter",
            "resolved_url": "http://physics.aps.org/articles/v6/69",
            "excerpt": "Particle physicists seem to have a pretty good handle on the fundamental particles of the universe, but there are some glaring holes in this understanding. Quarks are a good example of this.",
            "is_article": "1",
            "is_index": "0",
            "has_video": "0",
            "has_image": "1",
            "word_count": "1053"
        },
        "385567551": {
            "item_id": "385567551",
            "resolved_id": "385567551",
            "given_url": "http://gtrak.wordpress.com/2013/06/26/clojure-tradeoffs/",
            "given_title": "",
            "favorite": "0",
            "status": "0",
            "time_added": "1372794240",
            "time_updated": "1372794247",
            "time_read": "0",
            "time_favorited": "0",
            "sort_id": 0,
            "resolved_title": "Clojure Tradeoffs (design implications and why you should care)",
            "resolved_url": "http://gtrak.wordpress.com/2013/06/26/clojure-tradeoffs/",
            "excerpt": "Clojure as a language and community is very sensitive to the definition and design of tradeoffs. This post is an attempt to elucidate the tradeoffs chosen by the language, what they mean to interested parties, and an attempt to predict the future based on these choices.",
            "is_article": "1",
            "is_index": "0",
            "has_video": "0",
            "has_image": "0",
            "word_count": "2568"
        },
        "385823467": {
            "item_id": "385823467",
            "resolved_id": "385823467",
            "given_url": "http://devo.ps/blog/2013/06/26/goodbye-node-forever-hello-pm2.html",
            "given_title": "",
            "favorite": "0",
            "status": "0",
            "time_added": "1372275849",
            "time_updated": "1372275853",
            "time_read": "0",
            "time_favorited": "0",
            "sort_id": 2,
            "resolved_title": "Goodbye node-forever, hello PM2",
            "resolved_url": "http://devo.ps/blog/2013/06/26/goodbye-node-forever-hello-pm2.html",
            "excerpt": "It’s no secret that the devo.ps team has a crush on Javascript; node.js in the backend, AngularJS for our clients, there isn’t much of our stack that isn’t at least in part built with it. Our approach of building static clients and RESTful JSON APIs means that we run a lot of node.",
            "is_article": "1",
            "is_index": "0",
            "has_video": "0",
            "has_image": "1",
            "word_count": "800"
        },
        "386086417": {
            "item_id": "386086417",
            "resolved_id": "386086417",
            "given_url": "https://news.ycombinator.com/item?id=5946900",
            "given_title": "",
            "favorite": "0",
            "status": "0",
            "time_added": "1372274365",
            "time_updated": "1372274367",
            "time_read": "0",
            "time_favorited": "0",
            "sort_id": 4,
            "resolved_title": "Richard Stallman Inducted Into the 2013 Internet Hall of Fame",
            "resolved_url": "https://news.ycombinator.com/item?id=5946900",
            "excerpt": "The man never misses an opportunity to try and get people thinking about the issues that matter.  I'm unconvinced. The majority of people are sharing more about themselves online. Not less.",
            "is_article": "0",
            "is_index": "0",
            "has_video": "0",
            "has_image": "0",
            "word_count": "2316"
        },
        "388862014": {
            "item_id": "388862014",
            "resolved_id": "388862014",
            "given_url": "http://imgur.com/a/Op82P",
            "given_title": "",
            "favorite": "0",
            "status": "0",
            "time_added": "1372692033",
            "time_updated": "1372692040",
            "time_read": "0",
            "time_favorited": "0",
            "sort_id": 1,
            "resolved_title": "Upload images",
            "resolved_url": "http://imgur.com/a/Op82P",
            "excerpt": "",
            "is_article": "0",
            "is_index": "0",
            "has_video": "0",
            "has_image": "2",
            "word_count": "0"
        }
    },
    "since": 1372879049
}

I'm trying to parse this json in Javascript but failing miserably in node.

 data = JSON.parse(body); //body is the json response
 dat = data["list"];

I get the list from here but how do I parse this? JSON.stringify(dat) doesn't work either.

8
  • Hmm. Seems to be invalid JSON according to JSONlint (parse error on line 40) Commented Jul 3, 2013 at 19:26
  • 1
    @CrazyTrain Updated the question with valid JSON. Commented Jul 3, 2013 at 19:32
  • data["list"] returns an object. What do you want to do with that object, specifically? Commented Jul 3, 2013 at 19:37
  • @NickTomlin I want the "given_url". Commented Jul 3, 2013 at 19:37
  • It should already be parsed... What does data["list"]["380099389"]["given_url"] give you? Commented Jul 3, 2013 at 19:38

1 Answer 1

2

You need to use a for in loop like this:

for (var id in data["list"]){
    console.log(id + " has given_url " +data["list"][id]["given_url"]);
}

If you are using a library that modifies the base Object prototype you may also need a hasOwnProperty check like this:

for (var id in data["list"]){
    if (data["list"].hasOwnProperty(id)){
        console.log(id + " has given_url " +data["list"][id]["given_url"]);
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Wow! That worked. Thank you so much. This seems like Pythony solution. I tried forEach but always gave errors. Thank you again.
@Hick: Just FYI, .forEach() is for Arrays. To use it on plain objects, you can get an Array of object keys, and then iterate that Array, using each key to access the object, like: Object.keys(data.list).forEach(function(key) { console.log(key, data.list[key].given_url); });

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.