Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Decode bad 'JSON' in PHP

branch: master

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 t
Octocat-spinner-32 README.md
Octocat-spinner-32 lib_json.php
README.md

Flamework-JSON - Decode bad 'JSON' in PHP

The built-in PHP JSON decoder is very strict, and will not accept things which are fairly common in the wild:

  • unquoted keys, e.g. {foo: 1}
  • single-quoted strings, e.g. {"foo": 'bar'}
  • escaped single quoted, e.g. {"foo": "b\'ar"}
  • empty array elements, e.g. [1,,2]

This library allows you to decode JavaScript objects as if they were valid JSON.

Usage

include('lib_json.php');

$obj = json_decode_loose($str);

If you're using Flamework, just drop lib_json.php into your include folder.

Testing

If you have perl's Test::Harness installed (you almost certainly do), you can run the tests using:

prove --exec 'php' t/*.t
Something went wrong with that request. Please try again.