Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing malformed JSON with Javascript

I want to parse this content using Javascript. The data looks like this:

{"ss":[["Thu","7:00","Final",,"BAL","19","ATL","20",,,"56808",,"PRE4","2015"],["Thu","7:00","Final",,"NO","10","GB","38",,,"56809",,"PRE4","2015"]]}

Every single tutorial online teaches you how to parse JSON using Twitter, but I am not quite sure how parsing with JSON works.

I would like to set this up on a website to view the NFL team scores for a fun project and a good learning experience about parsing JSON, as I could care less about Twitter stuff.

Is this possible? Any good tutorials to start with? Even some starting code?

like image 399
allencoded Avatar asked Mar 12 '26 11:03

allencoded


1 Answers

Generally speaking, you can use JSON.parse to do this. However, that snippet that you have does not appear to be strictly valid JSON (as seen here: http://jsfiddle.net/yK3Gf/ and also by validating the source JSON here: http://jsonlint.com/).

So you will either need to parse it by hand, or get nfl.com to fix up their JSON.

As an alternative, their JSON does parse successfully when using eval(), so you could parse it with something like:

var parsedData = eval('(' + jsonData + ')');

...as shown here: http://jsfiddle.net/yK3Gf/1/

Though be aware that parsing JSON in this way is generally frowned upon (particularly when the data being parsed is being delivered by a third-party source), as it leaves you open to XSS attacks should the data happen to include any executable code inside of it.

like image 95
aroth Avatar answered Mar 14 '26 01:03

aroth



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!