0

If I have some json that looks like this:

[
  {"y": 0.964, "x": "2013-10-17T18:23:11.244418+00:00"}, 
  {"y": 0.979, "x": "2013-10-17T18:23:11.244455+00:00"}
  ...
]

How do I create a javascript object that uses identifiers instead of quoted strings as keys, like this:

[
  { y: 0.964, x: "2013-10-17T18:23:11.244418+00:00"}, 
  { y: 0.979, x: "2013-10-17T18:23:11.244455+00:00"}
  ...
]

Not sure if this is important, but for context, I am manipulating some data with python, then serializing with json.dumps(..) and passing the result of json.dumps(..) as a context variable to a Django html template. Then I want to plot the data using Rickshaw (http://code.shutterstock.com/rickshaw/). I have not dealt with converting the time to epoch yet.

tbc
  • 1,679
  • 3
  • 21
  • 28
  • *How do I create a javascript object that uses identifiers instead of quoted strings as keys* You don't need to do that, JSON objects are valid JS objects. – bfavaretto Oct 17 '13 at 20:56
  • Your two code blocks will function identically – Explosion Pills Oct 17 '13 at 20:56
  • so when I convert to javascript object, `{x:1234}` is absolutely identical to `{"x":1234}`? – tbc Oct 17 '13 at 20:58
  • I am assuming you need to parse this on the client side so after you get the response from your Python output, you would process it with something like jquery.parse(). Simply take the result and submit it to the parse call, var jsObj = $.parse(jsonFromPython); Then the jsObj should be a valid js obj. – ntlarson Oct 17 '13 at 20:58
  • Yes, they're identical. If you're printing it directly to the HTML into a script tag, you don't even need to parse it, it's a JavaScript object literal declaration. – bfavaretto Oct 17 '13 at 20:59
  • OK, I will try this again. Looks like this was a stupid question! – tbc Oct 17 '13 at 21:00
  • @tbc To be precise, JS even requires the quotes in some cases (when the property name is not a valid identifier name, e.g., when it contains spaces). – bfavaretto Oct 17 '13 at 21:01
  • The difference is though, if the return from the server is a string vs the json object itself. If the server is returning a string, e.g. '{"y":0.964,"x","2013-10-17T18:23:11.244418+00:00"}' then that needs to be parsed before it can be accessed as an object. – ntlarson Oct 17 '13 at 21:04
  • @ntlarson ah ha, that is the part I was missing I think. That is what your first comment does? – tbc Oct 17 '13 at 21:06
  • @tbc yes that is what my first comment does. It will parse that string response into an object. If you configure your server to return content-type="application/json" then it will return an object not a string. But if you have it set to content-type="text/plain" or some similar variant then it will return it as a string. Oh and I had the function name wrong.... it would be jQuery.parseJSON(string) (or $.parseJSON() if you prefer the shortened jquery call) for a full call of var jsObj = $.parseJSON(string); this of course relies on jQuery. If you do not have that you can use JSON.parse(string) – ntlarson Oct 17 '13 at 21:17
  • Excellent, thanks. This stuff is very new to me. With all this in mind, I should be able to make it work. – tbc Oct 17 '13 at 21:19
  • @tbc no problem, glad to help. In case you dont get notified of edits. I added the native browser call to my above comment. This is not supported in all browsers so it is best to use jQuery if you can but if your browser supports it then that is just as valid an approach – ntlarson Oct 17 '13 at 21:22

0 Answers0