Editor: JSON Data
Creating JSON Data
JSON (JavaScript Object Notation) is a lightweight data interchange format commonly used to store object data or for use with transmitting via web protocols. It consists of key-value pairs organized in a human-readable and easily parseable format. JSON is language-agnostic and the same structure can be read by many applications. Unreal natively does not provide any Blueprint functions for handling JSON data. However, Nominal has provided some helper functions for constructing and using JSON objects.
To create a new JSON object, use the Create JSON
function to construct a new JSON object with some key-value pairs. This can be modified later for more complicated key-value pairs.
This function above will construct the following JSON packet:
{
"name": "Nominal",
"founded": "2020"
}
Note
By default, when using the creating JSON function, the values will be assumed to be strings. If they are numeric numbers, they should be written using the set functions for integers or floating-point numbers.
Writing Data
JSON data can be written to a JSON object that has been constructed, or an empty string provided it has been configured to be in the right format. This is done using the Set JSON Value
functions. There are a few provided types:
- Bool: For writing true or false JSON values (with the option for a bool array)
- Float: For writing numerical decimal numbers to the JSON parameter (with an option for a float array)
- Int: For writing whole integer numbers to the JSON parameter (with an option for an integer array)
- String: For writing standard string data to a JSON parameter, or for writing additional JSON objects to the parameter (with an option for a string array)
- Message: For decomposing a message to its core data payload and serializing the data to JSON, provided that the message is valid.
In each case, data can be written to JSON by passing in the name of the key to write to. If the key does not exist, it will attempt to create a new key to write to. If the key does exist, it will overwrite the value within the key parameter.
This function above will result in the following JSON packet:
{
"name": "Nominal",
"founded": 2020,
"alive": true,
"size": 2.25
}
Reading Data
Data can be read from the JSON packet and deserialized into the appropriate data type based on the function call. As with the setting functions, floats, integers, strings, booleans and messages can all be read from the packet. Using the above example, the following code could be used to read all the values from the JSON packet. The parameter that is parsed in as the key is also case-insensitive, meaning the capitalization of the key does not matter.
Object Recursion
When reading and writing objects, if there are sub-objects within the parameters of keys, these can be accessed more easily with the full stop in the key entry. This will attempt to read or write data within an object by key. If the sub-keys do not exist, they will be created when attempting to write data. As an example, the following screenshot shows how to construct sub-objects within JSON and how to read the data.
This function above will result in the following JSON packet:
{
"name": "Nominal",
"numbers": {
"favorite": 8.0,
"options": [
1.0, 2.0, 4.0, 8.0
]
}
}
In the same way, when reading JSON values, using the full-stop will be able to read into sub-objects. In this example, to read the options as a float array, the following function could be used:
Note
There is no limit to the amount of recursion that can occur within the object. For additional recursion, add more full stops to the parameter key.
Deleting Parameters
Parameters can be deleted from the JSON packet using the Delete JSON Key
function. This will remove the entire object within that key. Additionally, recursion of deletion can be used with the full stops if required. If the name is invalid, no object will be deleted.