# JSON Handling

To work with HTTP / WebSockets effectively, you'll often need to manipulate JSON data from Blueprint.

To facilitate this, we have enabled Epic's `JsonBlueprintUtilities` plugin which offers a collection of nodes for manipulating Json Objects. In addition, we have provided an extended set of nodes to augment the out of the box features that Epic provide.

## JSON Blueprint Utilities

Epic's `JsonBlueprintUtilities` plugin offers several useful features:

### To / From String

#### Load Json From String

This can take a JSON string and convert it to a JSON Object

<figure><img src="/files/8TZlk0OZtcu88jihsN0w" alt=""><figcaption></figcaption></figure>

#### Get Json String

Converts a JSON Object to a JSON string

<figure><img src="/files/WoixO2hfczY9GNcxf22F" alt=""><figcaption></figcaption></figure>

#### Convert Struct to Json String

Converts a UStruct directly to a JSON string

<figure><img src="/files/m2HheUG86GCW612gUI01" alt=""><figcaption></figcaption></figure>

### Get / Set Fields

<figure><img src="/files/cMfBeEUPs1eaRsjl50t1" alt=""><figcaption></figcaption></figure>

The `Get Field` node allows you to retrieve a specific field from a JSON Object. The return value is a wildcard and so requires explicitly casting or connecting to an input pin.

The `Set Field` node allows you to set a specific field on a JSON Object with a wildcard typed field.

{% hint style="info" %}
Internally this relies on Unreal's Json Serializer, so if a wildcard type is not explicitly serializable to/from Json you may end up with unexpected failures or results.
{% endhint %}

### Field Inspection

<figure><img src="/files/nLyRXT4bTwVqfiFCi23H" alt=""><figcaption></figcaption></figure>

#### Has Field

Allows you to inspect if a Json Object has a specific field.

#### Get Field Names

allows you to list all the field names on an object; which can then be used to enumerate them in a loop.

## M2 JSON Utilities

We have augmented Epic's nodes with some of our own to further facilitate working with JSON data.

### To / From UStruct

<figure><img src="/files/xpFvD8gBWvzQaSs4SNhT" alt=""><figcaption></figcaption></figure>

#### Make Json Object from Struct

Converts any UStruct to a JSON object.

#### Make Struct from Json Object

Converts a JSON object into a struct of a specific, known type

#### Read Json Object into struct

Uses an existing struct (eg: in a variable) to read a JSON object

### Nested Objects

#### Get Json Object at Location

Allows you to specifiy a `dot.delimited.path` to a field containing a JSON object.

For example; given this JSON object

```json
{
    "person": {
        "forename": "alice",
        "surname": "smith"
    },
    "contact": {
        "address": {
            "street": "park avenue",
            "town": "exampleville"
        }
    }
}
```

The path of `contact.address` would return a Json Object containing:

<pre class="language-json"><code class="lang-json">{
<strong>    "street": "park avenue",
</strong>    "town": "exampleville"
}
</code></pre>

### To Map

<figure><img src="/files/laRA1PFL24qUeEapypzg" alt=""><figcaption></figcaption></figure>

This node allows you to read all fields into a specified (pre-made) map of either strings or integers.

## JSON Value Handling

We have added a range of improvements that allow working with JSON at a deeper level than Objects. These are exposed as a set of JSON Value functions.

### JSON Values

There are a series of functions available to create the main JSON Value primiities of `Null`, `Number`, `String`, `Array` and `Object`.

<figure><img src="/files/KG5GGkWhIIA70U6IbROk" alt=""><figcaption></figcaption></figure>

Each of these creates a JSON Value that wraps the provided data. You can extract this data from a JSON Value by using the `Get Json Value XXX` functions, which will return the wrapped data if the type matches.

The `Is Json Value XXX` functions can be used to test if a JSON Value is the type you want.

Additionally the functions Get Json Value Type and Is Json Value Type are available to query based on the type.

<figure><img src="/files/KqIapoB3WXqBmjTgoHYW" alt=""><figcaption></figcaption></figure>

As you would expect; there are a couple of To/From JSON String helpers available.

<figure><img src="/files/dzV86TuurQIAeExp8ulr" alt=""><figcaption></figcaption></figure>

The `Value to Json String` function can only work on JSON Values that wrap Arrays or Objects; this is due to a limitation in the built-in Unreal JSON Serializer.

### JSON Arrays

There are several ways to create a JSON Array. If you have an array of *homogeneous* types (eg: all String or all Number, etc) then the `Create Json Value XX Array` functions will help you.

If you have a *heterogeneous* list of value types, then the Create Json Value Array function will be best for you. This allows you to add *any* JSON Value type to an array.

<figure><img src="/files/wPqJYHiyUUXtkKJmk3m9" alt=""><figcaption></figcaption></figure>

You can also create an Array by using the Json String to Value function, using a Json representation of the array.

<figure><img src="/files/0Qe8xPUNJgzHRGwzihHp" alt=""><figcaption></figcaption></figure>

In order to access the content of the JSON Array you have a couple of options at present.

<figure><img src="/files/9vi4ybpUBheDRFiwvsmD" alt=""><figcaption></figcaption></figure>

`Get Json Value Array Items` will give you access to an array of the content; essentially unwrapping the array for you to use. You can then iterate on the contents as you would any normal array.

Individual items can be accessed by using the `Get Json Array Item Count` and `Get Json Array Item` functions. These return the length of the array and the item at a specific location.

### JSON Objects

JSON Objects can be wrapped in JSON Values. In addition to the JSON Object functionality discussed elsewhere, the JSON Value library gives you the ablity to Get or Set fields from an object using the `Get Json Object Field Value` and S`et Json Object Field Value` functions.

<figure><img src="/files/WqMMKbIGlagb1EiVmkHO" alt=""><figcaption></figcaption></figure>

You can also use the `Get Json Value at Location` to access a specific value in a JSON Object hierarchy.\\

For example; given this JSON object

```json
{
    "person": {
        "forename": "alice",
        "surname": "smith"
    },
    "contact": {
        "address": {
            "street": "park avenue",
            "town": "exampleville"
        }
    }
}
```

The path of `contact.address.street` would return a JSON Value of type `String` that contains the string value `park avenue`. This is useful for accessing individual fields from a nested hierarchy.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.msquared.io/creation/unreal-development/features-and-tutorials/web-services/working-with-json.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
