Mapping JSON Data with the json.path() Function (2024)

The following is an example of how to use the function:

json.path("$.store.book[?(@.price > 10)].title", $flow.body)

In this example, $.store.book[?(@.price > 10)].title is the query path. [?(@.price > 10)] is a filter used to narrow down the query results. $flow.body is the JSON object against which the query is run (in this case the JSON object comes from the flow input, hence $flow). So, this query basically says 'search the books array within $flow.body JSON object and return the title of the books whose price is more than $10'.

Consider the following sample JSON data:

{ "store": { "book": [ { "category": "reference", "author": "Nigel Rees", "title": "Sayings of the Century", "Availability": [ { "Country": "India", "Quantity": 4000, "Address": [ { "city": "houston" } ] } ], "price": 8.95 }, { "category": "fiction2", "author": "Evelyn Waugh", "title": "Sword of Honour", "Availability": [ { "Country": "USA", "Quantity": 5000, "Address": [ { "city": "sugarland" } ] } ], "price": 12.99 }, { "category": "fiction3", "author": "Herman Melville", "title": "Moby Dick", "isbn": "0-553-21311-3", "Availability": [ { "Country": "UK", "Quantity": 7000, "Address": [ { "city": "stafford" } ] } ], "price": 8.99 }, { "category": "fiction4", "author": "J. R. R. Tolkien", "title": "The Lord of the Rings", "isbn": "0-395-19395-8", "Availability": [ { "Country": "Australia", "Quantity": 2000, "Address": [ { "city": "aaaaa" } ] } ], "price": 22.99 } ], "bicycle": { "color": "red", "price": 19.95 } }, "expensive": 10}

The following are examples of some JSON query paths that search the JSON data above and return the category of the book. In the examples below, the second input parameter for this function, data is the name of the file that contains the above JSON code.

  • json.path("$.store.book[?(@.Availability[?(@.Quantity >= 6000)])].category", $flow.data)

    In the example above, the query scope is the entire book array. The filter used to query this array is the condition, [?(@.Availability[?(@.Quantity >= 6000)])]. Only the category values for the book elements that have Quantity >= 6000 is returned. So, this query returns fiction3.

  • json.path("$.store.book[?(@.author == 'Nigel Rees')].category", $flow.data)

    returns reference since it uses the filter [?(@.author == 'Nigel Rees')] and the only book authored by Nigel Rees in this array of books has its category as reference.

  • json.path("$.store.book[?(@.Availability[?(@.Address[?(@.city == 'sugarland')])])].category", $flow.data)

    This query is an example of a nested filter where [?(@.Availability[?(@.Address[?(@.city == 'sugarland')])])] is the outer filter and the nested filter within it is [?(@.city == 'sugarland')]. It returns reference.

  • json.path("$.store.book[0].category", $flow.data)

    This query does not use a filter. It returns reference, since your query scope is limited to the book[0] element only within the store object and your request is to return the value of categoy.

Mapping JSON Data with the json.path() Function (2024)

FAQs

Mapping JSON Data with the json.path() Function? ›

The JSON data being queried can come from the output of an activity or trigger. In the mapper, you can use the json. path() function by itself when providing value to an input parameter or use it within expressions to refer to data within a JSON structure.

How to create mapping in JSON? ›

Procedure
  1. In Maximo Manage, in the JSON Mapping application, create a new record.
  2. Name the new record and then, in the Object Structure field, select an option, and from the Direction list, select the direction to process data. ...
  3. In the JSON Data field, provide a map service query.

How to specify a path in JSON? ›

You use a JSONPath expression to traverse the path to an element in the JSON structure. You start at the root node or element, represented by $, and reach the required element in the JSON structure to extract data from it. You can use either the dot-notation or the bracket-notation to form the expressions.

How to use JSON path? ›

JSONPath Syntax

A JsonPath expression begins with the dollar sign ( $ ) character, which refers to the root element of a query. The dollar sign is followed by a sequence of child elements, which are separated via dot (code) notation or via the square brackets (code).

What is an example of a JSONPath function? ›

Overview
PathDescription
$["object"]['name']Return the object.name contents.
$.['object'].["name"]Return the object.name contents.
$.object.history.length()Return the number of object.history array elements.
$[?(@.name == 'Object')].price.first()Return the price field of the first object with name 'Object'.
5 more rows

What is path mapping in JSON? ›

path() function to query an element within JSON data. The JSON data being queried can come from the output of an activity or trigger. In the mapper, you can use the json. path() function by itself when providing value to an input parameter or use it within expressions to refer to data within a JSON structure.

How to convert JSON data into Map? ›

We can use Jackson to convert a JSON to a Map. Here, we're using the readValue() method from the ObjectMapper class to convert the JSON document to a Map. It takes the JSON document as a File object and a TypeReference object as parameters.

How do you use path ()? ›

To use it, you just pass a path or filename into a new Path() object using forward slashes and it handles the rest: Notice two things here: You should use forward slashes with pathlib functions. The Path() object will convert forward slashes into the correct kind of slash for the current operating system.

What is the difference between JSON path and JSON pointer? ›

JSON Pointer is a syntax that allows the selection of a specific value within a JSON document. This is contrasted to JSON Path which is used to perform searches, returning multiple values.

How do you specify a path? ›

Windows
  1. In Search, search for and then select: System (Control Panel)
  2. Click the Advanced system settings link.
  3. Click Environment Variables. ...
  4. In the Edit System Variable (or New System Variable) window, specify the value of the PATH environment variable. ...
  5. Reopen Command prompt window, and run your java code.

How to read JSONPath in JavaScript? ›

Follow these steps to read the JSON file using the fetch API method:
  1. Create a JSON file and add data to it.
  2. Open the JavaScript file.
  3. In the fetch method pass the path of the JSON file.
  4. Use the . json() method to parse the data in JSON format.
  5. Display the content in the console.
May 9, 2024

Does JSONPath always return an array? ›

When Jayway JSON path is used for finding one or more dynamic elements in an array, it always returns the matching result as an array. This means even it finds just one matching element, it returns that element as an array. This makes it difficult to reuse that leaf element as a single field or single value.

Is JSONPath a standard? ›

JSON Path Language (JSONPath) is a standard created by Stefan Goessner to provide a mechanism for selectively extracting values out of JSON documents, similar to how XPath does is for XML-formatted data.

How to write path in JSON file? ›

JSON Path examples
  1. key selection w/ dot in name { "name.with": 1 } selector: ['name.with'] result: 1.
  2. key selection w/ space in name { "name with": 1 } selector: ['name with'] result: 1.

How to use JSON function? ›

Example Explained
  1. 1: Create an array of objects.
  2. 2: Create a JavaScript function to display the array.
  3. 3: Use an array literal as the argument (instead of the array variable):
  4. 4: Put the function in an external js file.

What is the dot notation of a JSONPath? ›

The JSON column of a dot-notation expression corresponds to the context item of a path expression, and each identifier used in the dot notation corresponds to an identifier used in a path expression. For example, if JSON column jcolumn corresponds to the path-expression context item, then the expression jcolumn.

How to add Map in JSON object? ›

Java Map to JSON using JSON-Java

We created a HashMap object and populated it with key-value pairs. Then we used the JSON-Java library's JSONObject class to convert the HashMap into a JSON object.

Can you Map through a JSON object? ›

Since you have an array as the told structure of your JSON file, you can do array operations on it, such as map , filter , reduce , and the rest of methods available.

How do I create a mapping File? ›

Select Tools > Generate, or press Ctrl+G. The Generate Mapping File dialog appears. Enter the reader and writer parameters you want to use for the mapping file. Parameters:When you select certain source formats, the Parameters button becomes available.

What is a Map in JSON? ›

Javascript Object Notation, or Json, is a way of representing data using the JavaScript programming language. It is a lightweight data-interchange format used to store and transfer data between computers. Json maps are composed of key-value pairs, which are surrounded by curly brackets ( { } ).

Top Articles
Latest Posts
Article information

Author: Ouida Strosin DO

Last Updated:

Views: 5529

Rating: 4.6 / 5 (76 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Ouida Strosin DO

Birthday: 1995-04-27

Address: Suite 927 930 Kilback Radial, Candidaville, TN 87795

Phone: +8561498978366

Job: Legacy Manufacturing Specialist

Hobby: Singing, Mountain biking, Water sports, Water sports, Taxidermy, Polo, Pet

Introduction: My name is Ouida Strosin DO, I am a precious, combative, spotless, modern, spotless, beautiful, precious person who loves writing and wants to share my knowledge and understanding with you.