Skip to content

Latest commit

 

History

History
135 lines (108 loc) · 3.36 KB

jmespath.md

File metadata and controls

135 lines (108 loc) · 3.36 KB

jmespath extension

The jmespath extension implements JMESPath. JMESPath is a query language for transforming JSON documents into other JSON documents. It's supported in both the AWS and Azure CLI and has libraries available in a number of languages.

Compliance level

Fully compliant. The jsoncons implementation passes all compliance tests.

Classes

jmespath_expression Represents the compiled form of a JMESPath string.

Functions

search Searches for all values that match a JMESPath expression
make_expression Returns a compiled JMESPath expression for later evaluation. (since 0.159.0)

Examples

The examples below are from the JMESPath front page and the JMESPath Tutorial.

search function

jsoncons::jmespath::search takes two arguments, a basic_json and a JMESPath expression string, and returns a basic_json result. This is the simplest way to compile and evaluate a JMESPath expression.

#include <jsoncons/json.hpp>
#include <jsoncons_ext/jmespath/jmespath.hpp>

// for brevity
using jsoncons::json; 
namespace jmespath = jsoncons::jmespath;

int main() 
{
    std::string jtext = R"(
    {
      "locations": [
        {"name": "Seattle", "state": "WA"},
        {"name": "New York", "state": "NY"},
        {"name": "Bellevue", "state": "WA"},
        {"name": "Olympia", "state": "WA"}
      ]
    }        
    )";

    std::string expr = "locations[?state == 'WA'].name | sort(@) | {WashingtonCities: join(', ', @)}";

    json doc = json::parse(jtext);

    json result = jmespath::search(doc, expr);

    std::cout << pretty_print(result) << "\n\n";
}

Output:

{
    "WashingtonCities": "Bellevue, Olympia, Seattle"
}

jmespath_expression

A jsoncons::jmespath::jmespath_expression represents the compiled form of a JMESPath string. It allows you to evaluate a single compiled expression on multiple JSON documents. A jmespath_expression is immutable and thread-safe.

#include <jsoncons/json.hpp>
#include <jsoncons_ext/jmespath/jmespath.hpp>

// for brevity
using jsoncons::json; 
namespace jmespath = jsoncons::jmespath;

int main()
{ 
    std::string jtext = R"(
        {
          "people": [
            {
              "age": 20,
              "other": "foo",
              "name": "Bob"
            },
            {
              "age": 25,
              "other": "bar",
              "name": "Fred"
            },
            {
              "age": 30,
              "other": "baz",
              "name": "George"
            }
          ]
        }        
    )";

    auto expr = jmespath::make_expression<json>("people[?age > `20`].[name, age]"); // since 0.159.0
    // auto expr = jmespath::jmespath_expression<json>::compile("people[?age > `20`].[name, age]"); // until 0.159.0

    json doc = json::parse(jtext);

    json result = expr.evaluate(doc);

    std::cout << pretty_print(result) << "\n\n";
}

Output:

[
    ["Fred", 25],
    ["George", 30]
]