1. Home
  2. Docs
  3. Flexie CRM Methods
  4. jsonPath

jsonPath

Method

jsonPath—  Finds and extracts meaningful information out of JSON structures without the need of advanced scripting.

Description

This method extracts relevant information out of a JSON object without using advanced scripting or large memory consumption.
Minimum number of arguments for this method is two.

Structure of the method:

jsonPath(JSON object, expression)

Parameters

JSON object— the JSON containing the information to be extracted.

expression— the expression to extract the desired part of JSON object

Examples

In the examples below, we will show different type of usages of jsonPath method and its output.

Example JSON object:

{
     "store": {
         "book": [
             {
                 "category": "reference",
                 "author": "Nigel Rees",
                 "title": "Sayings of the Century",
                 "price": 8.95,
                 "available": true
             },
             {
                 "category": "fiction",
                 "author": "Evelyn Waugh",
                 "title": "Sword of Honour",
                 "price": 12.99,
                 "available": false
             },
             {
                 "category": "fiction",
                 "author": "Herman Melville",
                 "title": "Moby Dick",
                 "isbn": "0-553-21311-3",
                 "price": 8.99,
                 "available": true
             },
             {
                 "category": "fiction",
                 "author": "J. R. R. Tolkien",
                 "title": "The Lord of the Rings",
                 "isbn": "0-395-19395-8",
                 "price": 22.99,
                 "available": false
             }
         ],
         "bicycle": {
             "color": "red",
             "price": 19.95,
             "available": true
         }
     },
     "authors": [
         "Nigel Rees",
         "Evelyn Waugh",
         "Herman Melville",
         "J. R. R. Tolkien"
     ]
 }

We may receive this JSON object in different ways in Flexie CRM, analyze and extract meaningful information from it.

Get List of Books available:

expression: $.store.book[?(@.available == true)]

Method example: 
{{jsonPath(__data, '$.store.book[?(@.available == true)]')}}

Output:

 [
    {
        "category": "reference",
        "author": "Nigel Rees",
        "title": "Sayings of the Century",
        "price": 8.95,
        "available": true
    },
    {
        "category": "fiction",
        "author": "Herman Melville",
        "title": "Moby Dick",
        "isbn": "0-553-21311-3",
        "price": 8.99,
        "available": true
    }
] 

Get List of Books not available:

expression: $.store.book[?(@.available == false)]

Method example: 
{{jsonPath(__data, '$.store.book[?(@.available == false)]')}}

Output:

[
    {
        "category": "fiction",
        "author": "Evelyn Waugh",
        "title": "Sword of Honour",
        "price": 12.99,
        "available": false
    },
    {
        "category": "fiction",
        "author": "J. R. R. Tolkien",
        "title": "The Lord of the Rings",
        "isbn": "0-395-19395-8",
        "price": 22.99,
        "available": false
    }
] 

Get Books with title “Moby Dick”:

expression: $.store.book[?(@.title == "Moby Dick")]

Method example: 
{{jsonPath(__data, '$.store.book[?(@.title == "Moby Dick")]')}}

Output:

 [
    {
        "category": "fiction",
        "author": "Herman Melville",
        "title": "Moby Dick",
        "isbn": "0-553-21311-3",
        "price": 8.99,
        "available": true
    }
] 

Get Price of the book with title “Moby Dick”:

expression: $.store.book[?(@.title == "Moby Dick")].price

Method example: 
{{jsonPath(__data, '$.store.book[?(@.title == "Moby Dick")].price')}}

Output:

[
   8.99
] 

Get Book Titles with Price less or equal than ” 12.99″:

expression: $.store.book[?(@.price <= "12.99")].price

Method example: 
{{jsonPath(__data, '$.store.book[?(@.price <= "12.99")].price')}}

Output:

 [
    8.95,
    12.99,
    8.99
] 

Get Book Titles with Price less than ” 12.99″:

expression: $.store.book[?(@.price < "12.99")].price

Method example: 
{{jsonPath(__data, '$.store.book[?(@.price < "12.99")].price')}}

Output:

[
   8.95,
   8.99
] 

Get all Prices of available items in all object regardless of the name:

expression: $..*[?(@.available == true)].price

 Method example: 
{{jsonPath(__data, '$..*[?(@.available == true)].price')}} 

Output:

[
   19.95,
   8.95,
   8.99
] 

JsonPath expressions

JsonPath Result
$.store.bicycle.price The price of the bicycle.
$.store.book[*] All books.
$.store.book[1,3] The second and fourth book.
$.store.book[1:3] From the second book to the third.
$.store.book[:3] From the first book to the third.
$.store.book[x:y:z] Books from x to y with a step of z.
$..book[?(@.category == ‘fiction’)] All books with category == ‘fiction’.
$..*[?(@.available == true)].price All prices of available products.
$..book[?(@.price < 10)].title The title of all books with price lower than 10.
$..book[?(@.author==$.authors[3])] All books by “J. R. R. Tolkien”
$[store] The store.
$[‘store’] The store.
$..book[*][title, ‘category’, “author”] title, category and author of all books.

To stay updated with the latest features, news and how-to articles and videos, please join our group on Facebook, Flexie CRM Academy and subscribe to our YouTube channel Flexie CRM.

How can we help?