Skip to content
Log in

deserialize - Liquid Filter Reference

On this page

Description

Deserializes a JSON or XML string into a map or list. You can then read values with dot notation and pass the result to filters such as map, pluck and where.

Property Value
Return Type map or list
Category text

The format is detected from the input. A string that starts with < is parsed as XML, and anything else is parsed as JSON. Leading and trailing whitespace is ignored.

The filter returns nothing (nil) when:

  • the input is not valid JSON or XML
  • the input is larger than 1 MB

No error is shown in either case, so check the result before you use it.

:::note POS templates parse JSON only. XML input returns nothing on the POS. :::

Examples

A JSON object:

```liquid

{% assign data = ‘{“name”:”Dog & Cat”}’ | deserialize %} {{ data.name }} ```

Output: Dog & Cat

A JSON array, read with pluck:

```liquid

{% assign pets = ‘[{“id”:1,”name”:”Dog”},{“id”:2,”name”:”Cat”}]’ | deserialize %} {{ pets | pluck: “name” | join: “ & “ }} ```

Output: Dog & Cat

XML

Each XML element becomes a key, starting from the root element. Read nested elements with dot notation:

```liquid

{% assign data = ‘RubyMatz’ | deserialize %} {{ data.catalog.book.title }} by {{ data.catalog.book.author }} ```

Output: Ruby by Matz

Check that parsing succeeded before you read from the result. This XML is cut off partway through a tag, so deserialize returns nothing:

```liquid

{% assign feed = ‘<book' | deserialize %} {% if feed == blank %}

The feed could not be read.

{% else %}

{{ feed.catalog.book.title }}

{% endif %} ```

Output: <p>The feed could not be read.</p>

:::tip The api tag already parses a JSON or XML response body when you set accept to json or xml, so you do not need deserialize on response.body in those cases. :::

Was this article helpful?

Was this article helpful?