JSON.parse Issues - javascript

so, I am working on a website for a client, a friend of mine.
He sells geckos, and he has made a website for himself, and his sales partner, and I am doing a lot of javascript work for him, IE AJAX, etc... Well, I got to the available lizard page for him, and I am making a sort of dynamic gecko selection system. The way this script is supposed to work is, fetch a JSON file (here) which is perfectly good JSON, and then parse the values in to multiselects. I'm using the jQuery.get function to do this. All goes well until I try parsing the JSON data, and the browser, firefox, throws the error "Syntax Error: JSON.parse", and chromium throws the error "Unexpected Token", the problem also occurred with
The error is thrown on line 219 of js.js,
jQuery.parseJSON().
the issues are in the function drawCat(data), the page this code is in use on is Here
I hope that this is a quality question, I'm really quite tired to be coding right now, it pretty late.

Actually that is not valid JSON. It's easy to check at http://jsonlint.com/. The error is that the root should be either one JSON object or an array. Now you have several JSON objects as roots.
Update: Danjah is also correct. After you fix this the problem he highlights will also cause invalid JSON. so you need to fix both.

I don't think its valid, there's a bunch of missing commas part way down the file, screenshot attached as no line numbering.

Related

js files are loading in unreadable format

I was working on a Web App. I used a "✗" UTF-8 character as a delete button. Eclipse asked me to save file in UTF-8 format i told yes. Everything worked fine. But next day when i ran the app again it is throwing exception "Uncaught SyntaxError: Unexpected token ILLEGAL". When i checked it, all javascript file is loading in unreadable format. See image below.
I tried to replace that character with its UNICODE "✗" and saved all js files in default encoding, but didn't help.
Do anyone know why is this happening?
It sounds like you saved in a different text format than you loaded it and/or loaded it in a different text format that you saved it. Without recommending a tool, I would see if you can use something to change the code-page/text-format and then try stuff.
However, seeing as what you are showing on teh screen is 3rd part stuff, you could just more easily re-download it where you got it the first time, or perhaps unpack it if you saved the installer/zip-file.

What is the maximum length that $.parseJSON() can handle?

I have a long json array that needs to be sent to an html5 mobile app and parsed. The whole array has around 700kb (gziped to 150kb) and it's 554976 characters long at the moment. But it will increase on time.
Using jquery to parse the json, my app crashes while trying to parse it. And so does jsonlint, json parser.fr and any other online json validator I try, so I'm guessing eval() is not an option either.
Might be a broad question but what is the maximum "acceptable" length for a json array?
I have already removed as much data as I can from the array, the only option I can think of is to split the array in 3-4 server calls and parse it separately in the app. Is there any other option?
EDIT
Thanks to #fabien for pointing that if jsonlint crashes there is a problem on the json. There was a hidden "space" character in one of the nodes. It was parsed correctly on the server but not on the client.
I've parsed way bigger arrays with jquery.
My first guess is that there's an error in your json.
You have many ways to find it (sublime text could highlight the error but some time, it's a bit long). Try to paste it in a web tool like http://www.jsoneditoronline.org/. and use any of the buttons (to format or to send to the right view). It'll tell you where the error is.

Javascript syntax checker for Text Area text?

Im working on a project and one of the requirements is that users can write their own javascript code, on a simple text area component.
This is easy, but I have to validate the syntax, something like 'error: missing ; at the end'... like most syntax checkers do.
I dont want to develop it, cause it would take a lot of time.
Does anybody know if a plugin exists for that?
I found one called Javascript Lint but it is a .exe file and it doesnt have native integration with Java (its a java ee project, jsp files, etc)
Thanks for the help!
Here is a simple top-down parser: https://github.com/douglascrockford/TDOP/blob/master/parse.js.
A more complicated parser is JSLint: https://github.com/douglascrockford/JSLint. JSLint is half parser, have C-style "lint" tool (for checking for common mistakes), but you can just make use of the parser half by not reporting 'lint' results to your users. You can also turn off all of the "lint" checks.
The major difference is that JSLint will do things like checking that variables are defined and in scope and checking for other mistakes/common bad practices.
If the data is not critical, you can ask users to paste their data in http://www.jslint.com/ (Doug Crockford's site) and it displays the errors within the JSON.
We had used YUI in our JS application, so we used YAHOO.lang.JSON.parse(your textarea's json content) to validate the user's Json. However, we were never able to give them an exact list of errors within the Json, we could only tell them if it was valid Json.
Thanks

Parsing very large JSON strings in IE causing problems

I'm parsing a 2MB JSON string in IE8. The JSON.Parse line is taking a little while to return and IE8 shows a message asking the user if they want to abort the script.
Is there any way I can suppress this message? (or somehow speed up JSON.Parse)
I know about Microsoft KB175500, however this is not suitable as my target users will not have administrator access to make the registry modifications on their SOE machines.
I had this same question. Apparently there is no way to suppress the message, but there are tricks to make IE think it's still working by using an asynchronous iteration pattern (dead link, view comments below).
This comes from an answer to one of my questions:
loop is too slow for IE7/8
If the browser is unhappy with how long the JSON parser is taking, there are only four choices here I know of:
Get a faster JSON parser that doesn't take so long.
Break up your JSON data into smaller pieces so you are only parsing smaller pieces at once.
Modify a JSON parser to work in chunks so it can parse part of the data in one chunk, then on a short timeout, parse the next chunk, etc... This will prevent the browser prompt, but is probably a lot of work to write/modify a JSON parser that works this way.
If you can be sure the content is safe, then you could see if using eval instead of a JSON parser works around the issue.

Where to start with JSON? (Closed)

I'm trying to get started with JSON. I've set the link to the JSON.js file using a script link, and I've set my objects using JSON, but it returns no results at all when I try to refer to the object. The JSON doesn't seem to be working at all. If anyone could point me in the right direction it'd be appreciated. I've looked all over the internet and haven't found much to help me.
Thanks guys. The JSON website helped me figure it out
Get Firefox, get Firebug, learn to use it to see what javascript is being loaded, and where the errors are.
Oh, and post some code.
Take a look-see at this
http://json.org/
basically you need to understand that json is a way to stream javascript object literals and arrays from a server to the client (and vice-versa). Open up firebug/webkit and in the console try
var obj = JSON.parse('{"test": 1}')
and you will see that obj is an object literal with a test property.
edit -- note that the link I provided mentions that json is a "is a lightweight data-interchange format" -- so its does not need to be javascript specific. But I think in practice you will get the most mileage using json in conjunction with javascript.
Here's a couple of links that might help:
http://secretgeek.net/json_3mins.asp
http://www.hunlock.com/blogs/Mastering_JSON_(_JavaScript_Object_Notation_)

Categories

Resources