Jump to content
Sign in to follow this  
Fmdpa

JSON trouble

Recommended Posts

I'm working on a project that involves fetching a JSON file from Github. I make an AJAX GET request to that page and store the responseText data in localStorage. Later, I need to retrieve the stringified JSON object from localStorage, parse it, and scan it. My trouble right now is the parsing, though. Whenever I use JSON.parse, I get some sort of error...unexpected this, unescaped that. I just can't successfully parse the JSON object. What do I need to know about the JSON syntax so that I can write a parseable JSON file? I don't want my project continually messing up because of these parsing issues.

Share this post


Link to post
Share on other sites

Maybe the problem I'm having with parsing this file is that it has several unquoted regular expressions. Is it permissible to have unquoted values in a JSON object?

Share this post


Link to post
Share on other sites

I believe only numbers can go unquoted in JSON.

Share this post


Link to post
Share on other sites

OK, thanks. Here's the problem I'm having. When I fetch the file from Github with AJAX, it automatically returns the contents of the JSON file in the responseText property....as a string. The problem is, when JSON objects are converted to a string with a method such as JSON.stringify(), it is not merely "stringified". Certain characters are escaped so that it will be parseable. Unfortunately, the reponse text is already a string and since it was not encoded with JSON.stringify, it is likely not parseable. Is there a solution to this predicament?

Share this post


Link to post
Share on other sites

well, according JSLint, what you are getting is valid JSON, at least by running it through JSLint. Have you confirmed what does in and out of localStorage are the same thing, and that they both validate?http://www.jslint.com/

Share this post


Link to post
Share on other sites

Yes, I modified the JSON last night after I found some the jsonlint validation tool. But if you look at the regular expressions, you'll see that one of the things I had to do to make it validate was unescape the periods. Consequently, they are no longer literal periods. They are metacharacters now. I don't want them to be the "." metacharacter, though. Is there any way to work around this?

Share this post


Link to post
Share on other sites

JSON will stringify this: ['/site\\.com/'] As this: "["/site\\.com/"]" That's a literal backslash in regex.

Edited by Fmdpa

Share this post


Link to post
Share on other sites

I need to find some way to escape metacharacters in the regular expression and in JSON.

Share this post


Link to post
Share on other sites

I'm confused about what the problem is, this is valid JSON: ["/site\\.com/"] A string is not a regexp, so you would need to convert that to a regular expression object, but the syntax is valid. The slash inside that string is not 2 slashes and then a dot, it's one slash and a dot (an escaped dot). Alert the string to verify that.

Share this post


Link to post
Share on other sites

Good, I didn't realize that it actually came out parsed with a single slash escaping the period. I know that the RegExp is in string form right now, but I'm trying to figure out how to parse the JSON file fetched from Github so that the string is ready to be used to create a regular expression. Here's the problem. Try running this in your Javascript console:

JSON.parse('["(site\\.com)"]')

I get this error: Unhandled Error: JSON.parse: Invalid escape char: "(site\ This, however, works:

var stringifiedObj = JSON.stringify(["(site\\.com)"]),      r = JSON.parse(stringifiedObj);

Once again, however, since the JSON file is returned in the responseText string in XMLHttpRequest object, I can't stringify it. I need to write the file as if it were already run through JSON.stringify (which escapes everything that needs to be escaped, etc.).

Share this post


Link to post
Share on other sites

That seems like it might be an issue with JSON, but I'm not sure what. Running this: JSON.stringify(["(site\\.com)"]) gives this string: ["(site\\.com)"] Trying to parse that string fails, so that seems like a bug in JSON. It should parse anything that is the output of stringify. These two both output the same thing: JSON.parse('["(site.com)"]')JSON.parse('["(site\.com)"]') This is necessary: JSON.parse('["(site\\\\.com)"]') to get this: ["(site\.com)"] It almost seems like it treats the dot as a special character which you can optionally escape. It's strange that this: JSON.parse('["(site\.com)"]') results in this: ["(site.com)"] It's strange because when you stringify that it does not escape the dot, but it will unescape it when you parse it. It requires 4 slashes to end up with one slash escaping the dot in the output.

Share this post


Link to post
Share on other sites

Hmm, very strange. This inconsistency seems serious. Dragonfly parses this as you described: JSON.parse('["(site\\\\.com)"]') => ["(site.com)"] But the Firefox console does something still different. If you run this command: JSON.parse('["(site\\\\.com)"]') You will get an bad escaped char error. ...I just checked, and it doesn't do this anymore in Firefox Aurora. It will parse it as this now: ["(site\\.com)"]

Share this post


Link to post
Share on other sites

And in Chrome you get this: ["(site\.com)"] I'm not sure what to make of this, I haven't researched this issue, but it's troubling that the stringify method can return something that is not able to be parsed. I would think that would be a violation of the requirements for the stringify method. One of the requirements should be that the output is always parseable.

Share this post


Link to post
Share on other sites

Absolutely; it makes no logical sense that the results of stringify would be unparseable, or, that the results of running an object through stringify and parse would create an object having contents different than the original object.

Share this post


Link to post
Share on other sites

Done. I wonder if I should file bug reports for the different browsers that stringify an object in such a way that it is unparseable.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...