Jump to content

JSON trouble


Fmdpa

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.

Link to comment
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?

Link to comment
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/

Link to comment
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?

Link to comment
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.

Link to comment
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.).

Link to comment
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.

Link to comment
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)"]

Link to comment
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.

Link to comment
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.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...