Top Related Projects
Quick Overview
JMESPath (pronounced "james path") is a query language for JSON. The jmespath.py repository is the official Python implementation of the JMESPath specification. It allows users to declaratively specify how to extract elements from a JSON document, providing a powerful and flexible way to search, filter, and manipulate JSON data.
Pros
- Standardized query language for JSON across multiple programming languages
- Powerful and expressive syntax for complex JSON transformations
- Well-documented with a comprehensive specification
- Efficient implementation with good performance
Cons
- Learning curve for users unfamiliar with the JMESPath syntax
- Limited to JSON data structures (not suitable for other data formats)
- May be overkill for simple JSON parsing tasks
- Requires additional dependency in Python projects
Code Examples
- Basic key extraction:
import jmespath
data = {"foo": {"bar": "baz"}}
result = jmespath.search('foo.bar', data)
print(result) # Output: baz
- Filtering an array:
import jmespath
data = {"people": [{"name": "John", "age": 30}, {"name": "Jane", "age": 25}]}
result = jmespath.search('people[?age > `27`].name', data)
print(result) # Output: ['John']
- Using functions:
import jmespath
data = {"items": [1, 2, 3, 4, 5]}
result = jmespath.search('sum(items)', data)
print(result) # Output: 15
Getting Started
To use JMESPath in your Python project, follow these steps:
-
Install the library using pip:
pip install jmespath
-
Import the library in your Python script:
import jmespath
-
Use the
jmespath.search()
function to query your JSON data:data = {"example": {"nested": "value"}} result = jmespath.search('example.nested', data) print(result) # Output: value
For more advanced usage and a complete list of supported expressions, refer to the official JMESPath documentation at https://jmespath.org/.
Competitor Comparisons
Command-line JSON processor
Pros of jq
- More powerful and feature-rich, supporting complex data transformations
- Standalone command-line tool, useful for shell scripting and data processing
- Extensive documentation and community support
Cons of jq
- Steeper learning curve due to its more complex syntax
- Primarily designed for command-line use, less suitable for direct integration into Python projects
Code Comparison
jq:
jq '.items[] | {id: .id, name: .name, price: .price}'
jmespath.py:
jmespath.search('items[*].{id: id, name: name, price: price}', data)
Key Differences
- jq is a standalone tool, while jmespath.py is a Python library
- jq has a more extensive query language, allowing for complex transformations
- jmespath.py integrates seamlessly with Python projects and data structures
- jq is better suited for command-line data processing, while jmespath.py is ideal for in-application use
Use Cases
- Choose jq for shell scripting, data analysis, and complex JSON transformations
- Opt for jmespath.py when working within Python applications or when simplicity is preferred
Both tools are valuable for JSON querying and manipulation, with the choice depending on the specific project requirements and environment.
Query and manipulate JavaScript objects with JSONPath expressions. Robust JSONPath engine for Node.js.
Pros of jsonpath
- More flexible syntax, allowing for complex queries and filters
- Supports wildcards and recursive descent operators
- Wider adoption and compatibility with various programming languages
Cons of jsonpath
- Less standardized implementation across different libraries
- Potentially slower performance for complex queries
- Syntax can be more verbose for simple operations
Code Comparison
jsonpath:
import jsonpath_ng
expr = jsonpath_ng.parse('$.store.book[?(@.price < 10)].title')
result = [match.value for match in expr.find(data)]
jmespath:
import jmespath
result = jmespath.search('store.book[?price < `10`].title', data)
Key Differences
- Syntax: jsonpath uses a more expressive syntax with additional operators, while jmespath has a simpler, more concise syntax
- Query Execution: jsonpath typically returns a list of matches, whereas jmespath returns the result directly
- Standardization: jmespath has a formal specification, leading to more consistent implementations across different libraries
Both libraries serve similar purposes but cater to different use cases and preferences. jmespath is often favored for its simplicity and standardization, while jsonpath offers more advanced querying capabilities at the cost of potential complexity.
Java JsonPath implementation
Pros of JsonPath
- More flexible syntax, allowing for complex queries and filters
- Supports a wider range of operations, including arithmetic and logical expressions
- Better suited for complex JSON structures and advanced querying needs
Cons of JsonPath
- Less standardized implementation across different languages
- Potentially slower performance for simple queries
- Steeper learning curve due to more complex syntax
Code Comparison
JsonPath:
jsonpath_expr = parse('$.store.book[?(@.price < 10)].title')
result = [match.value for match in jsonpath_expr.find(data)]
JMESPath:
import jmespath
result = jmespath.search('store.book[?price < `10`].title', data)
Key Differences
- JsonPath uses a more verbose syntax with explicit operators
- JMESPath has a more concise and intuitive syntax for simple queries
- JsonPath offers more flexibility for complex filtering and expressions
- JMESPath provides better standardization across different programming languages
Both libraries serve similar purposes but cater to different use cases. JsonPath is better suited for complex JSON manipulation, while JMESPath offers a simpler, more standardized approach for basic querying needs.
Get JSON values quickly - JSON parser for Go
Pros of gjson
- Written in Go, offering potential performance benefits
- Supports both JSON and JSON Lines formats
- Provides additional features like JSON modification and creation
Cons of gjson
- Limited to JSON data, while JMESPath supports various data structures
- Less flexible query syntax compared to JMESPath's more expressive language
- Smaller community and ecosystem compared to JMESPath
Code Comparison
JMESPath (Python):
import jmespath
data = {"foo": {"bar": [{"name": "one"}, {"name": "two"}]}}
result = jmespath.search('foo.bar[*].name', data)
print(result) # Output: ['one', 'two']
gjson (Go):
package main
import (
"fmt"
"github.com/tidwall/gjson"
)
func main() {
json := `{"foo":{"bar":[{"name":"one"},{"name":"two"}]}}`
result := gjson.Get(json, "foo.bar.#.name")
fmt.Println(result.Array()) // Output: [one two]
}
Both libraries provide ways to query JSON data, but their syntax and implementation differ. JMESPath offers a more standardized query language, while gjson provides a simpler, Go-specific approach with additional JSON manipulation features.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
JMESPath
.. image:: https://badges.gitter.im/Join Chat.svg :target: https://gitter.im/jmespath/chat
JMESPath (pronounced "james path") allows you to declaratively specify how to extract elements from a JSON document.
For example, given this document::
{"foo": {"bar": "baz"}}
The jmespath expression foo.bar
will return "baz".
JMESPath also supports:
Referencing elements in a list. Given the data::
{"foo": {"bar": ["one", "two"]}}
The expression: foo.bar[0]
will return "one".
You can also reference all the items in a list using the *
syntax::
{"foo": {"bar": [{"name": "one"}, {"name": "two"}]}}
The expression: foo.bar[*].name
will return ["one", "two"].
Negative indexing is also supported (-1 refers to the last element
in the list). Given the data above, the expression
foo.bar[-1].name
will return "two".
The *
can also be used for hash types::
{"foo": {"bar": {"name": "one"}, "baz": {"name": "two"}}}
The expression: foo.*.name
will return ["one", "two"].
Installation
You can install JMESPath from pypi with:
.. code:: bash
pip install jmespath
API
The jmespath.py
library has two functions
that operate on python data structures. You can use search
and give it the jmespath expression and the data:
.. code:: python
>>> import jmespath
>>> path = jmespath.search('foo.bar', {'foo': {'bar': 'baz'}})
'baz'
Similar to the re
module, you can use the compile
function
to compile the JMESPath expression and use this parsed expression
to perform repeated searches:
.. code:: python
>>> import jmespath
>>> expression = jmespath.compile('foo.bar')
>>> expression.search({'foo': {'bar': 'baz'}})
'baz'
>>> expression.search({'foo': {'bar': 'other'}})
'other'
This is useful if you're going to use the same jmespath expression to search multiple documents. This avoids having to reparse the JMESPath expression each time you search a new document.
Options
You can provide an instance of jmespath.Options
to control how
a JMESPath expression is evaluated. The most common scenario for
using an Options
instance is if you want to have ordered output
of your dict keys. To do this you can use either of these options:
.. code:: python
>>> import jmespath
>>> jmespath.search('{a: a, b: b}',
... mydata,
... jmespath.Options(dict_cls=collections.OrderedDict))
>>> import jmespath
>>> parsed = jmespath.compile('{a: a, b: b}')
>>> parsed.search(mydata,
... jmespath.Options(dict_cls=collections.OrderedDict))
Custom Functions
The JMESPath language has numerous
`built-in functions
<http://jmespath.org/specification.html#built-in-functions>`__, but it is
also possible to add your own custom functions. Keep in mind that
custom function support in jmespath.py is experimental and the API may
change based on feedback.
**If you have a custom function that you've found useful, consider submitting
it to jmespath.site and propose that it be added to the JMESPath language.**
You can submit proposals
`here <https://github.com/jmespath/jmespath.site/issues>`__.
To create custom functions:
* Create a subclass of ``jmespath.functions.Functions``.
* Create a method with the name ``_func_<your function name>``.
* Apply the ``jmespath.functions.signature`` decorator that indicates
the expected types of the function arguments.
* Provide an instance of your subclass in a ``jmespath.Options`` object.
Below are a few examples:
.. code:: python
import jmespath
from jmespath import functions
# 1. Create a subclass of functions.Functions.
# The function.Functions base class has logic
# that introspects all of its methods and automatically
# registers your custom functions in its function table.
class CustomFunctions(functions.Functions):
# 2 and 3. Create a function that starts with _func_
# and decorate it with @signature which indicates its
# expected types.
# In this example, we're creating a jmespath function
# called "unique_letters" that accepts a single argument
# with an expected type "string".
@functions.signature({'types': ['string']})
def _func_unique_letters(self, s):
# Given a string s, return a sorted
# string of unique letters: 'ccbbadd' -> 'abcd'
return ''.join(sorted(set(s)))
# Here's another example. This is creating
# a jmespath function called "my_add" that expects
# two arguments, both of which should be of type number.
@functions.signature({'types': ['number']}, {'types': ['number']})
def _func_my_add(self, x, y):
return x + y
# 4. Provide an instance of your subclass in a Options object.
options = jmespath.Options(custom_functions=CustomFunctions())
# Provide this value to jmespath.search:
# This will print 3
print(
jmespath.search(
'my_add(`1`, `2`)', {}, options=options)
)
# This will print "abcd"
print(
jmespath.search(
'foo.bar | unique_letters(@)',
{'foo': {'bar': 'ccbbadd'}},
options=options)
)
Again, if you come up with useful functions that you think make
sense in the JMESPath language (and make sense to implement in all
JMESPath libraries, not just python), please let us know at
`jmespath.site <https://github.com/jmespath/jmespath.site/issues>`__.
Specification
=============
If you'd like to learn more about the JMESPath language, you can check out
the `JMESPath tutorial <http://jmespath.org/tutorial.html>`__. Also check
out the `JMESPath examples page <http://jmespath.org/examples.html>`__ for
examples of more complex jmespath queries.
The grammar is specified using ABNF, as described in
`RFC4234 <http://www.ietf.org/rfc/rfc4234.txt>`_.
You can find the most up to date
`grammar for JMESPath here <http://jmespath.org/specification.html#grammar>`__.
You can read the full
`JMESPath specification here <http://jmespath.org/specification.html>`__.
Testing
=======
In addition to the unit tests for the jmespath modules,
there is a ``tests/compliance`` directory that contains
.json files with test cases. This allows other implementations
to verify they are producing the correct output. Each json
file is grouped by feature.
Discuss
=======
Join us on our `Gitter channel <https://gitter.im/jmespath/chat>`__
if you want to chat or if you have any questions.
Top Related Projects
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot