jmespath.cpp
C++ implementation of JMESPath, a query language for JSON http://jmespath.org
|
jmespath.cpp is a C++ implementation of JMESPath, a query language for JSON. It can be used to extract and transform elements of a JSON document.
Input JSON document:
JMESPath expression:
Result of evaluating the expression:
For more examples take a look at the JMESPath Tutorial or the JMESPath Examples pages.
For installation instructions check out the Installation and integration page.
To use the public functions and classes of jmespath.cpp you should include the header file "#include \<jmespath/jmespath.h\>"
. The public interface is declared in the "jmespath"
namespace.
The main entry point of the library is the jmespath::search function which takes a JMESPath expression as its first argument and a JSON document as the second argument and returns the result of the evaluated expression as a new JSON document.
Besided passing the input document as an lvalue reference you can also pass it as an rvalue reference, which would allow the library to take advantage of move operations internally and produce the output document in a more efficient and faster manner.
The jmespath::Expression class allows to store a parsed JMESPath expression which is usefull if you want to evaluate the same expression on multiple JSON documents.
By importing the names from the jmespath::literals namespace you can also use user defined literals to define JMESPath expressions.
For the handling of JSON documents and values jmespath.cpp relies on the excelent nlohmann_json library.
For defining JSON values you can either use "_json"
user defined literal or the jmespath::Json type which is just an alias for nlohmann_json
or you can also include the header of the nlohmann_json library and use the "nlohmann::json"
type directly.
All the exceptions that might get thrown by jmespath::search or jmespath::Expression are listed on the Exception classes page.
The library uses the Boost Exception library for throwing exceptions. Which means that the thrown exceptions can carry additional data attached to them. All the additional data that exceptions might carry is listed on the Error info types page.
This additional information can be used for example to pinpoint exactly which part of a JMESPath expression caused a parsing failure.
This code would produce the following output:
You can also use boost::diagnostic_information function to get all the additional information attached to an exception.
This code would produce the following output: