Invalid JSON

Tags: webdevelopment, programming.
By lucb1e on 2012-06-28 17:16:50 +0100

I've written about XML versus JSON before, praising JSON far above XML. I still stand by this, but I must say that there is an incredible lot of invalid JSON out there.

Most applications using JSON are client-server setups where the client downloads data from the server. Since JSON is native Javascript, you can just throw it in there along with any sort of Javscript instructions, and it'll work. Yeah, for you application only that is.

For example for my website playbylyrics.com I wanted to enable users to download the MP3 of what they were listening. To do this, I needed some service which converts the video... Since this is not a trivial thing that I can build in an evening or two, I decided to just use one of the many available websites for this. Certainly it must be possible to bypass all ads on one and get the mp3 directly?

And it was. I did it for two websites in the end to get a lot of pre-cached files. One of them was youtube-mp3.org, which has a rather cluttered website. Also they are in read-only mode (if I remember correctly Youtube threatened to sue them last week), but they do have the largest cache database by far. Request almost any song, and they'll have it.

Luckily for me, they got a Chrome extension. This extension doesn't redirect users from youtube to their website, it uses asynchronous Javascript for this. Looking in Chromium's developer tools, they requested data in a JSON-like looking format. It turns out it wasn't really JSON but simply a Javascript array, but still I think PHP should have been able to parse that.

It's not the first time that PHP gives trouble parsing JSON input. It always gives me trouble, both when parsing and when encoding JSON. Try this:
<script>
    var json = eval(GET("json.php"));
    alert(json.user.age);
</script>

json.php:
<?php $array = array("user" => array("name" => "Luc", "age" => 19));
echo json_encode($array);


It will not work.

What you should have done is this:
var json = eval("(" + GET("json.php") + ")");

Don't ask me why, but working with PHP and JSON is always trouble. Don't start about XML by the way, that's absolutely impossible to parse. You'll first need to spend 10 minutes checking out documentation before even starting on XML... Anyhow.

So I got the following code from youtube-mp3.org:
info = { "title" : "videotitle", "image" : "http://i.ytimg.com/vi/videoid/default.jpg", "length" : "3", "status" : "serving",  "progress_speed" : "",  "progress" : "",  "ads" : "",  "pf" : "",  "h" : "ffa36452c376c5d6f7e5abcdefc95616"  };

And of course, it doesn't parse. Also not if you get rid of the "info =". Now I paste it here I notice the semi-colon behind it, that turns out to be the problem.

But still, PHP never parses JSON just like that. What do I do about that, being a developer? Write my own parser!
http://lucb1e.com/rp/php/json-freaking-decode.php
It sucks, but at least you can almost always parse the JSON in a way that it's usable. Here are some use-cases:

Valid JSON
{"variable": "value"}

Invalid JSON
"variable": "value"
variable = "value";
({[)}[variable = value]

As you see, it doesn't even have to look like JSON anymore and it will still parse it to something. That's what I wanted: it just always works.

The source for this tool is obtainable by pasting ?source behind the URL:
http://lucb1e.com/rp/php/json-freaking-decode.php?source
Before looking though, I suggest you sit down. It's not pretty, but it gets the job done :)
lucb1e.com
Another post tagged 'programming': Searching 10GB of data As A Service - lessons learned

Look for more posts tagged programming or webdevelopment.

Previous post - Next post