JSON Include Directives

Standard JSON Include

.. jsoninclude::

Include a section of a JSON file, given a jsonpointer.

e.g. using this json file:

Source:

```{eval-rst}
.. jsoninclude:: _static/example.json
    :jsonpointer: /a/0/b
    :expand: e
```

Output:

{
    "c": 1,
    "d": 2,
    "e": [
        {
            "f": 3
        },
        {
            "f": 4
        }
    ],
    "g": 5
}

The expand option is needed to expand a list when the json is folded by javascript. For more info see Javascript below.

exclude option:

Source:

```{eval-rst}
.. jsoninclude:: _static/example.json
    :jsonpointer: /a/0/b
    :exclude: e,g
```

Output:

{
    "c": 1,
    "d": 2
}

include_only option:

Source:

```{eval-rst}
.. jsoninclude:: _static/example.json
    :jsonpointer: /a/0/b
    :include_only: e,g
    :expand: e
```

Output:

{
    "e": [
        {
            "f": 3
        },
        {
            "f": 4
        }
    ],
    "g": 5
}

Javascript

To fold the JSON with javascript, you need to include the following files:

You need to add the files to a _static folder within your docs, and then add the following to _templates/layout.html.

{% extends "!layout.html" %}
{% set css_files = css_files + ["_static/renderjson.css"] %}
{% set script_files = script_files + ["_static/renderjson.js", "_static/json-example-format.js"] %}

The option expand can be used to control which lists are expanded initially. The option title can be used to give the json include a title; only one of consecutive includes will be shown, with a select box to switch:

Source:

```{eval-rst}
.. jsoninclude:: _static/example.json
    :jsonpointer:
    :title: collapsed

.. jsoninclude:: _static/example.json
    :jsonpointer:
    :expand: a,b,e,h
    :title: expanded
```

Output:

{
    "a": [
        {
            "b": {
                "c": 1,
                "d": 2,
                "e": [
                    {
                        "f": 3
                    },
                    {
                        "f": 4
                    }
                ],
                "g": 5
            }
        }
    ],
    "h": [
        {
            "i": 11,
            "j": 12
        },
        {
            "k": 13,
            "l": 14
        }
    ]
}
{
    "a": [
        {
            "b": {
                "c": 1,
                "d": 2,
                "e": [
                    {
                        "f": 3
                    },
                    {
                        "f": 4
                    }
                ],
                "g": 5
            }
        }
    ],
    "h": [
        {
            "i": 11,
            "j": 12
        },
        {
            "k": 13,
            "l": 14
        }
    ]
}

Flat JSON Include

.. jsoninclude-flat::

Include a section of a JSON file, flattened into a table representation, given a jsonpointer.

Examples, using this json file:

Source:

```{eval-rst}
.. jsoninclude-flat:: _static/example.json
    :jsonpointer: /a/0/b
```

Output:

/a/0/b/c

/a/0/b/d

/a/0/b/g

1

2

5

recursive (include nested dicts and lists):

Source:

```{eval-rst}
.. jsoninclude-flat:: _static/example.json
    :jsonpointer: /a/0/b
    :recursive:
```

Output:

/a/0/b/c

/a/0/b/d

/a/0/b/e/0/f

/a/0/b/e/1/f

/a/0/b/g

1

2

3

4

5

List of items directly under the json pointer:

Source:

```{eval-rst}
.. jsoninclude-flat:: _static/example.json
    :jsonpointer: /h
```

Output:

/h/k

/h/l

11

12

13

14

Remove part of the path from the headings uwsing ignore_path:

Source:

```{eval-rst}
.. jsoninclude-flat:: _static/example.json
    :jsonpointer: /a/0/b
    :ignore_path: /a/0/b/
```

Output:

c

d

g

1

2

5

jsoninclude-flat also has the options exclude and include_only, the same as for jsoninclude (see above).