-1

I am trying to parse the following JSON in Javascript but haven't been successful

{  
   "outer":"{'inner':{'key':'value'}}"
}

Using JSON.parse on the above JSON gives an invalid syntax error. I have verified that its a valid JSON. what am I missing?

var json = '{"outer":"{'inner':{'key':'value'}}"}';
JSON.parse(json);

This gives an unexpected identifier at the 'inner' json

user_mda
  • 18,148
  • 27
  • 82
  • 145
  • 1
    Show us how you're parsing your JSON and what you expect. `JSON.parse("{\"outer\":\"{'inner':{'key':'value'}}\"}");` works although it probably doesn't give the format you were hoping for. – Mike Cluck Dec 07 '16 at 20:57
  • http://jsbin.com/jibecet/1/edit?js,console — That parses without error when I test it. – Quentin Dec 07 '16 at 21:02
  • 1
    Re to edit: Notice how there's a `'` before `inner`? That means you're closing your string. You need to escape it. `\'`. – Mike Cluck Dec 07 '16 at 21:03

1 Answers1

2

The problem isn't your JSON (although it has it's own problems).

You've tried to convert it to a JavaScript string by wrapping it with ' characters but it already contains ' characters so you have to escape them.

var json = '{"outer":"{\'inner\':{\'key\':\'value\'}}"}';

You'd probably be better off just treating it as an object literal:

var not_json = {"outer":"{'inner':{'key':'value'}}"};
console.log(not_json);
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • I'm not even sure he wants the inner JSON to be a string. Probably a mistake. – Henry A. Dec 07 '16 at 21:07
  • Thanks guys thats helpful, I think I need to first replace the single quotes with double quotes and then parse it – user_mda Dec 07 '16 at 21:11
  • @user_mda You could but there's really no point in writing it as JSON then parsing it. You may as well do what Quentin said and just make it an object. – Mike Cluck Dec 07 '16 at 21:19
  • Thanks Mike, I am attempting to parse and the 'value' value of the 'key' thats why I want it to be a json and accessible – user_mda Dec 07 '16 at 21:21
  • 2
    @user_mda: *"thats why I want it to be a json"* If you are working in JavaScript, you almost always want objects and arrays, not JSON. E.g. if you want to create and access the data, then use `var data = {outer: {inner: {key: 'value'}}};`. No need to parse anything. You only need to use JSON if you persist data on disk or transfer data between two systems. JSON is *textual* data representation. Another example: If you want an array of numbers, you create just that: `var data = [1,2,3];`. You likely *won't* create a CSV and "parse" that: `var data = '1,2,3'.split(',').map(x => Number(x))`. – Felix Kling Dec 07 '16 at 21:42
  • I guess thats where I am confused, in the above case how can I access the 'value' ? Is the answer string parsing? – user_mda Dec 07 '16 at 22:11
  • @user_mda — In which above case? In Felix's last comment? No parsing needed. Just `data.outer.inner.key`. When you've got it in your original format where it is a string of JSON-like data as the value of the `outer` property? First you write a custom parser because it isn't JSON. – Quentin Dec 07 '16 at 22:17
  • data.outer.inner gives me undefined. I understand that its not a JSON so my options are either to write a custom parser or convert it into json to be easily accessible using data.outer.inner.key as you said – user_mda Dec 07 '16 at 23:47
  • @user_mda: *"so my options are either to write a custom parser or convert it into json to be easily accessible using data.outer.inner.key as you said "* You still seem to confuse JSON with a JavaScript object. JSON is *text* / a *string*. JSON has to be parsed into a JavaScript object or array. This is a string literal containing JSON: `'{"foo": 42}'`. This is a JavaScript object literal: `{foo: 42}` (or `{"foo": 42}`). Maybe this helps: [Access / process (nested) objects, arrays or JSON](http://stackoverflow.com/q/11922383/218196), and http://eloquentjavascript.net/04_data.html . – Felix Kling Dec 08 '16 at 07:52