parse_json

Modified on Thu, 9 Jul at 10:50 AM

parse_json

TABLE OF CONTENTS

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

Transformation
{
  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.
}
Result
{
  "a": 1,
  "b": true,
  "c": ["apple", "banana"],
  "d": {"name": "Jane", "age": 30}
}

Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select at least one of the reasons
CAPTCHA verification is required.

Feedback sent

We appreciate your effort and will try to fix the article