Master JSON Extraction in Python with JsonPath: A Practical Guide

This tutorial shows how to install the JsonPath library, retrieve JSON data from an API, and use both traditional indexing and powerful JsonPath expressions—including filters, wildcards, and slicing—to efficiently extract nested values for automated testing.

FunTester
FunTester
FunTester
Master JSON Extraction in Python with JsonPath: A Practical Guide

When testing APIs, responses are often JSON objects whose nested structures can make manual extraction cumbersome. This article introduces the third‑party Python library jsonpath, which provides a concise syntax for querying JSON data.

Installation

Install the library via pip:

pip install jsonpath

Fetching JSON from an API

Using requests, the example calls a weather service and prints the full JSON response.

import requests
# Request URL
url = 'http://apis.juhe.cn/simpleWeather/query'
# Parameters
data = {
    "city": "上海",
    "key": "331eab8f3481f37868378fcdc76cb7cd"
}
# Send POST request
r = requests.post(url, data=data)
j = r.json()
print(j)

The response contains a top‑level result object with a future array of daily forecasts.

Traditional Indexing

To extract the forecast for March 16, the article first demonstrates direct indexing:

data = result['result']['future'][1]
print(data)
# Output:
# {'date': '2023-03-16', 'temperature': '9/15℃', 'weather': '多云转小雨', 'wid': {'day': '01', 'night': '07'}, 'direct': '东南风'}

While this works, deep nesting quickly becomes error‑prone.

Using JsonPath

JsonPath allows expressive queries. The same March 16 forecast can be retrieved with a filter expression:

data = jsonpath.jsonpath(result, '$..[?(@.date=="2023-03-16")]')
print(data)
# Output:
# [{'date': '2023-03-16', 'temperature': '9/15℃', 'weather': '多云转小雨', 'wid': {'day': '01', 'night': '07'}, 'direct': '东南风'}]

This approach resembles regular‑expression style filtering and avoids manual navigation.

Other JsonPath Patterns

Retrieve entire sub‑objects : $.result.future returns the full forecast list, while $.reason returns the status message.

data = jsonpath.jsonpath(result, '$.result.future')
data1 = jsonpath.jsonpath(result, '$.reason')
print(data)
print(data1)
# Output shows the list of forecasts and ['查询成功!']

Wildcard selection : $.result.future.[*].date extracts all dates.

data = jsonpath.jsonpath(result, '$.result.future.[*].date')
print(data)
# Output: ['2023-03-15', '2023-03-16', '2023-03-17', '2023-03-18', '2023-03-19']

Slicing : $..future[0,1] returns the first two forecast objects.

data = jsonpath.jsonpath(result, '$..future[0,1]')
print(data)
# Output: [{...date:'2023-03-15'...}, {...date:'2023-03-16'...}]

Conclusion

The article demonstrates that JsonPath simplifies JSON extraction for API testing, turning verbose indexing into concise queries. Regular practice with these patterns enables flexible data handling in real‑world test projects.

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.

PythonAutomationJSONPathAPI testingJSON extraction
FunTester
Written by

FunTester

10k followers, 1k articles | completely useless

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.