-1

Ugh. So noobish, but I can't seem to parse this string into valid JSON within Javascript. How should I? This data is coming from a Foursquare checkin.

EDIT: Here is an image of what I'm trying to get at, the "checkin" property enter image description here

"{"id":"52e2ac6511d28d5cd63ee41f","createdAt":1390586981,"type":"checkin","shout":"Vd","timeZone":"America\/Denver","timeZoneOffset":-420,"user":{"id":"76097213","firstName":"SpencerTesting","lastName":"GardnerTesting","gender":"none","relationship":"self","photo":"https:\/\/foursquare.com\/img\/blank_boy.png","tips":{"count":0},"lists":{"groups":[{"type":"created","count":1,"items":[]}]},"homeCity":"Pleasant Grove, UT","bio":"","contact":{"email":"email@email.com"}},"venue":{"id":"4b83ec97f964a520d81631e3","name":"Joseph Smith Building","contact":{},"location":{"address":"770 E University Pkwy","crossStreet":"at BYU","lat":40.245901424248,"lng":-111.65174603462219,"postalCode":"84604","cc":"US","city":"Provo","state":"UT","country":"United States"},"categories":[{"id":"4bf58dd8d48988d198941735","name":"College Academic Building","pluralName":"College Academic Buildings","shortName":"Academic Building","icon":"https:\/\/ss1.4sqi.net\/img\/categories\/education\/default.png","parents":["College & University"],"primary":true}],"verified":false,"stats":{"checkinsCount":1695,"usersCount":281,"tipCount":1},"likes":{"count":0,"groups":[]},"beenHere":{"count":0}}}"
freedomflyer
  • 2,431
  • 3
  • 26
  • 38
  • You shouldn't need to do that in the first place. What are you trying to write? – SLaks Jan 24 '14 at 18:21
  • 4
    are you sure that's the string, or did you copy that from chrome's console? if it really is like that, just JSON.parse(str.slice(1,-1)) – dandavis Jan 24 '14 at 18:21
  • as i suspected, it is a valid string, the wrapping quotes are cosmetic in chrome so you can spot JSON vs a deep object... if you alert() the string you won't see the quotes... – dandavis Jan 24 '14 at 18:26
  • Edited to show more information about where it's coming from. – freedomflyer Jan 24 '14 at 18:27

2 Answers2

2

Use single quotes around the string rather than double quotes:

JSON.parse('{"id":"52e2ac6511d28d5cd63ee41f","createdAt":1390586981,"type":"checkin","shout":"Vd","timeZone":"America\/Denver","timeZoneOffset":-420,"user":{"id":"76097213","firstName":"SpencerTesting","lastName":"GardnerTesting","gender":"none","relationship":"self","photo":"https:\/\/foursquare.com\/img\/blank_boy.png","tips":{"count":0},"lists":{"groups":[{"type":"created","count":1,"items":[]}]},"homeCity":"Pleasant Grove, UT","bio":"","contact":{"email":"email@email.com"}},"venue":{"id":"4b83ec97f964a520d81631e3","name":"Joseph Smith Building","contact":{},"location":{"address":"770 E University Pkwy","crossStreet":"at BYU","lat":40.245901424248,"lng":-111.65174603462219,"postalCode":"84604","cc":"US","city":"Provo","state":"UT","country":"United States"},"categories":[{"id":"4bf58dd8d48988d198941735","name":"College Academic Building","pluralName":"College Academic Buildings","shortName":"Academic Building","icon":"https:\/\/ss1.4sqi.net\/img\/categories\/education\/default.png","parents":["College & University"],"primary":true}],"verified":false,"stats":{"checkinsCount":1695,"usersCount":281,"tipCount":1},"likes":{"count":0,"groups":[]},"beenHere":{"count":0}}}')

works for me.

In the case that the string is dynamically generated and actually contains the " character, follow dandavis's suggestion of removing the " using JSON.parse(str.slice(1,-1)) where str contains the JSON response to your Foursquare API call.

However, this shouldn't be necessary since the surrounding quotes are a side effect of printing to the console. In reality, the value of the checkin key is perfectly valid JSON that can be parsed regularly with JSON.parse. However, if copied off the console, the extra " need to be replaced since all the inner " aren't displayed as escaped in the console.

If you're using a jQuery and not vanilla JS to send your AJAX queries, specifying dataType: 'json' during the query will automatically result in parsed data passed to the success callback. I believe this is the default behavior for $.ajax() if the response type is application/json or equivalent.

sushain97
  • 2,752
  • 1
  • 25
  • 36
1

I can't seem to parse this string into valid JSON within Javascript

You don't parse a string into JSON. The string contains JSON, and you parse it to convert the JSON in JavaScript arrays and objects.

In your case, you are looking for

 var data = JSON.parse(obj.checkin);

because the value of the checking property is a string containing JSON. Then you can access the properties of the object.

Community
  • 1
  • 1
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143