When using JMESPath for transforming JSON, a basic concept that is used a lot, is the notion of a key/value pair. We use key/value pairs to order our data in both JMESPath and JSON.
Let’s take a look at a simple example of how it looks in JSON.
{
"name": "John Doe"
}
Here we have two strings being separated with a :
. The string on the left side "name"
is the key which, when referenced to it, will return the value string "John Doe"
. This is a simple example on a key/value pair in JSON, where the value is a string. It should be noted that the key should always be a string, but the value can be of any allowed JSON data type.
When transforming the JSON with JMESPath in Documotor, we follow the same structure with the key/value pair, where we define new keys based on the contents of the JSON data. However, you can also create key/value pairs in JMESPath with customized data. Let’s look at an example with both JSON and JMESPath.
Assume we have the following JSON sample data.
{
"firstName": "John",
"lastName": "Doe",
"age": 34
}
With JMESPath, we can define a new key/value pair. See below how we transform the sample data.
{
name: join(' ', [firstName, lastName]),
age: age
}
Note that we in JMESPath do not have to surround the key with "
as we have to do with the JSON key. In the value, instead of writing a string, we use the join
function to concatenate the two values from the JSON sample data. We also do not have to surround the JSON keys we are referencing to in the function with "
either. When defining a JMESPath transformation, it is only the data defined in this transformation that is passed forward. If you look at line 3 of the JMESPath transformation, you can see that we define a key/pair value that simply repeats the key from the JSON sample data in order to make sure the data is passed on to the result data. In the age: age
key/value pair, the value is referring to our JSON sample data. See below the result from the above transformation.
{
"name": "John Doe",
"age": 34
}
To learn more about Key/Value pairs, you can read about the JSON Syntax for further elaboration.