Fundamentals 9 min read

Master JSON Queries in Python with JMESPath and JsonPath

This guide shows how to install, import, and use the JMESPath and JsonPath Python modules to perform basic and advanced queries on JSON data, including example scripts that filter, sort, and extract specific fields.

Test Development Learning Exchange
Test Development Learning Exchange
Test Development Learning Exchange
Master JSON Queries in Python with JMESPath and JsonPath

Introduction

JMESPath and JsonPath are query languages that enable fast and easy extraction of specific attributes or values from JSON structures. This article explains how to use the corresponding Python modules to query and manipulate JSON data.

1. Installation and Import

Install the modules via pip install jmespath and pip install jsonpath-ng, then import them in your Python code:

import jmespath</code>
<code>import jsonpath_ng

2. Basic JMESPath Queries

After defining a JSON object, you can run JMESPath expressions to retrieve values, test key existence, or apply filters. Example data:

data = {"person": {"name": "John Doe", "age": 35, "email": "[email protected]", "phone": [{"type": "home", "number": "555-555-5555"}, {"type": "work", "number": "555-555-1234"}]}}

Common queries:

jmespath.search("person.name", data)               # → 'John Doe'</code>
<code>jmespath.search("person.phone[].number", data)   # → ['555-555-5555', '555-555-1234']</code>
<code>jmespath.search("person.nickname", data)        # → None</code>
<code>jmespath.search("person.phone[?type=='home'].number", data)  # → ['555-555-5555']

3. Using JMESPath in Python

A complete script demonstrates querying a list of people and selecting the oldest person:

import jmespath</code>
<code>data = {"people": [{"name": "John Doe", "age": 35, "email": "[email protected]"}, {"name": "Jane Smith", "age": 27, "email": "[email protected]"}, {"name": "Bob Johnson", "age": 48, "email": "[email protected]"}]}</code>
<code>expression = "people[?age>=35].name | sort(@) [0]"</code>
<code>name = jmespath.search(expression, data)</code>
<code>print("The oldest person is:", name)

Running this script prints:

The oldest person is: John Doe

4. Basic JsonPath Queries

After installing jsonpath-ng, you can perform similar queries. Example data (same as above) and queries:

match = jsonpath_ng.parse("person.name").find(data)</code>
<code>for result in match:
    print(result.value)   # → 'John Doe'</code>
<code>match = jsonpath_ng.parse("person.phone[*].number").find(data)</code>
<code>for result in match:
    print(result.value)   # → ['555-555-5555', '555-555-1234']</code>
<code>match = jsonpath_ng.parse("person.phone[?type=='home'].number").find(data)</code>
<code>for result in match:
    print(result.value)   # → ['555-555-5555']

5. Using JsonPath in Python

A full script shows how to find the oldest person using JsonPath:

import jsonpath_ng</code>
<code>data = {"people": [{"name": "John Doe", "age": 35, "email": "[email protected]"}, {"name": "Jane Smith", "age": 27, "email": "[email protected]"}, {"name": "Bob Johnson", "age": 48, "email": "[email protected]"}]}</code>
<code>expression = jsonpath_ng.parse("people[?age>=35].name | sort(@) [0]")</code>
<code>name = expression.find(data)</code>
<code>print("The oldest person is:", name[0].value)

Output:

The oldest person is: John Doe

Conclusion

Both JMESPath and JsonPath provide concise, powerful ways to query and manipulate JSON data in Python. By installing the appropriate module, writing expressions, and using the provided search functions, developers can efficiently extract, filter, and transform JSON structures for further processing.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PythonJSONJsonPathTutorialJMESPathData Query
Test Development Learning Exchange
Written by

Test Development Learning Exchange

Test Development Learning Exchange

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.