parse_json

Parse a JSON token from a string

Definition

any parse_json(string $string)

Parses a string to yield a JSON token of any data type. Inverse of to_string function.

Parameters

string $string

The string correctly formatted as a JSON token. If this is not the case, an error is raised.

Returns

any

A JSON token that was represented by $string.

Examples

{
  a: parse_json('1'),
  b: parse_json('true'),
  c: parse_json('["apple", "banana"]'),
  d: parse_json('{name: "Jane", age: 30}'),
  e: parse_json('apple')           // Error, strings need to be enclosed in double quotes.
}
{
  "a": 1,
  "b": true,
  "c": ["apple", "banana"],
  "d": {"name": "Jane", "age": 30}
}