-1

I am trying to create a JSON and populate it with some data. The data is a bit complex so I would like to have its "title", "name" and "value".

My issue is that I am not able to get the content from the JSON I created and getting "Uncaught SyntaxError: Unexpected token o" error message. However, if I just pass the json variable to console.log() I can see all the objects contained in the variable.

Please see the code below:

JSON

var json = [
    {"title":"rice",
        "value":{
            "carb": 44.5,
            "fat": 0.1,
            "cal": 205,
            "prot": 4.3
        }
    },
    {"title":"buckwheat",
        "value":{
            "carb": 20,
            "fat": 1,
            "cal": 92,
            "prot": 3
        }
    },
    {"title":"potato",
        "value":{
            "carb": 50.5,
            "fat": 0.5,
            "cal": 225,
            "prot": 5.9
        },
    }
]  

JS

var obj = JSON.parse(json);
console.log(obj[0].title);
egurb
  • 1,176
  • 2
  • 14
  • 40
  • 1
    `JSON.parse` should be called to turn a string representation of a json object into an actual javascript object. The `json` variable is already a javascript object. – Will P. Mar 22 '16 at 21:38
  • 1
    You don't have any JSON in your question. Just use `console.log( json[0].title );` without trying to parse it first (although I recommend also renaming your variable since the name `json` is misleading).. – Paul Mar 22 '16 at 21:39
  • You also have an extraneous comma after the value object inside the object with title "potato". – LinuxDisciple Mar 22 '16 at 21:41
  • 1
    @LinuxDisciple Note that a trailing comma in an object is valid in Javascript (but not JSON), so it should still work find if he doesn't try parsing it. – Paul Mar 22 '16 at 21:43
  • As suggested the json variable name is misleading. It's a javascript object (an Array of Objets) but not a JSON. Try to stringify instead of parsing your object HTTPS://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify – Ludovic Frérot Mar 22 '16 at 21:53
  • Possible duplicate of duplicate: [parse json Object in javascript getting undefined](http://stackoverflow.com/questions/22706833/parse-json-object-in-javascript-getting-undefined) – Yogi Mar 22 '16 at 22:01

3 Answers3

1

Maybe I don't understand your question but

JSON.parse()

As first parameter takes some String, text value, converts and returns it as JSON object. Since you have one - you can populate it with your data.

lomboboo
  • 1,221
  • 1
  • 12
  • 27
0

Your json data is not valid.

Valid Example

You can press f12 in Chrome or Mozilla and look console. You can find what is wrong with your JS code.

[
  {
    "title": "rice",
    "value": {
      "carb": 20,
      "fat": 1,
      "cal": 92,
      "prot": 3
    }
  },
  {
    "title": "buckwheat",
    "value": {
      "carb": 20,
      "fat": 1,
      "cal": 92,
      "prot": 3
    }
  },
  {
    "title": "potato",
    "value": {
      "carb": 50.5,
      "fat": 0.5,
      "cal": 225,
      "prot": 5.9
    }
  }
]
hurricane
  • 6,521
  • 2
  • 34
  • 44
  • Thanks for the response. I just didn't understand how do I get any data without a variable. – egurb Mar 22 '16 at 21:47
0

Your var "json" is already a javascript object. Just add a semicolon to it and fix the one error (comma after "prot": 5.9}) :

var obj = [
    {"title":"rice",
        "value":{
            "carb": 44.5,
            "fat": 0.1,
            "cal": 205,
            "prot": 4.3
        }
    },
    {"title":"buckwheat",
        "value":{
            "carb": 20,
            "fat": 1,
            "cal": 92,
            "prot": 3
        }
    },
    {"title":"potato",
        "value":{
            "carb": 50.5,
            "fat": 0.5,
            "cal": 225,
            "prot": 5.9
        }
    } ];

You can simply get the values with:

console.log(obj[0].title);

If you want to parse json, save your data in string format.