...
The [*] means that we want to select every element of the “data” array. Then we specify the nodes in each array object we want to translate.
Example 4 - Arrays and translate all properties
When your JSON is an array of objects, then leaving the JSON path empty will not extract all the properties of all the objects.
Example:
Code Block |
---|
[
{
"text": "Hello",
"description": "Whatever"
},
{
"text": "Good morning",
"description": "Whatever again"
}
] |
To extract all the properties and texts, use this JSON path:
Code Block |
---|
$[*] |
To extract just the “text” property, use:
Code Block |
---|
$[*].text |
Example 5 - Extract meta-information with texts
Let’s look at a JSON file that contains product descriptions. With each product there is a title, a description, an identifier and some more meta-information.
...
You can also pull data and save to each segment’s custom fields like this:
...
Example
...
6 - Handling plain text and HTML content
JSON files may contain HTML formatted content. Our file contains “text” nodes that are not html and “html” nodes that are, well, HTML.
...
We achieve this by simply defining two JSON paths. One for our plain text nodes and one for our HTML formatted nodes:
...
Example
...
7 - Using advanced node selectors
Check out the file below. Each product has “status” field. We only want to translate if its value is “yes”!
...
It checks that the product object has a “translate” property with value “yes”: @.translate == 'yes'
. The @
character represents the object in the array we look at.
...
Example
...
8 - Extract meta-data from any location in the file
We already discussed an example earlier on how to extract meta-information. These examples were simple in that the meta-information was on the same object level as the actual text to translate.
...
We only touched on some of the possibilities of the JSON filter. For more advanced files check out the JSON path links printed in the top.
With the advent of CHAT GPT and other LLMs, constructing JSON paths is made a bit easier. See the the following question and result:
Code Block |
---|
Given the JSON below: What is the JSON path to extract the "text" properties?
[
{
"text": "Hello",
"description": "Whatever"
},
{
"text": "Good morning",
"description": "Whatever again"
}
] |
Answer from GTP 4o:
Code Block |
---|
The JSON path to extract all the "text" properties from the given JSON array is: $[*].text Here’s a breakdown of the JSON path: $ refers to the root element of the JSON document. [*] refers to all elements in the array at the root level. .text refers to the "text" property of each element in the array. |