A tools to find the path of a specific key in deep nested JSON.

Related tags

JSONJsonPathFinder
Overview

如何快速从深层嵌套 JSON 中找到特定的 Key

#公众号

在爬虫开发的过程中,我们经常遇到一些 Ajax 加载的接口会返回 JSON 数据。如下图所示,是 Twitter 的用户时间线接口,返回了一段3000多行的深层嵌套 JSON:

其中的cursor这个字段,是请求下一页的必要字段,我必须把它的 value 值读取出来,拼接到请求 URL 中,才能请求下一页的内容。

现在问题来了,cursor字段在这个 JSON 里面的哪个位置?从最外层开始,我应该怎么样才能读取到最里面的这个cursor中的value字段的值?

我知道已经有一些第三方库可以直接根据字段名读取 JSON 内部任意深度的值,不过用别人的东西总没有自己写一个轮子来得过瘾。所以今天我们自己来手写一个模块,我把他叫做JsonPathFinder,传入一个 JSON 字符串和需要读取的字段名,返回从最外层开始直到这个字段的路径。

效果演示

我们用 Python 之父龟叔的 Twitter 时间线来作为演示,运行以后,效果如下图所示:

可以看到,从最外层开始一路读到cursor字段,需要经过非常多的字段名,对应到 JSON 中,如下图所示:

由于entries 字段列表中一共有20个元素,所以这里的18、19实际上对应了倒数第二条和倒数第一条数据。其中,倒数第二条的 cursor 对应的是本页第一条推文,而倒数第一条对应的是本页最后一条推文。所以当我们要往后翻页的时候,应该用的是倒数第一条的 cursor。

我们试着来读取一下结果:

非常轻松地获取到了数据。不需要再肉眼在 JSON 中寻找字段了。

原理分析

JsonPathFinder 的原理并不复杂,全部代码加上空行,一共只有32行,如下图所示:

因为一个字段在 JSON 中可能出现很多次,所以find_one方法返回从外层到目标字段的第一条路径。而find_all方法返回从外层到目标字段的所有路径。

而核心算法,就是iter_node方法。在把 JSON 字符串转成 Python 的字典或者列表以后,这个方法使用深度优先遍历整个数据,记录它走过的每一个字段,如果遇到列表就把列表的索引作为 Key。直到遍历到目标字段,或者某个字段的值不是列表也不是字典时结束本条路径,继续遍历下个节点。

代码第10-15行,分别对列表和字典进行处理。对于字典来说,我们分离 key 和 value,写作:

for key, value in xxx.items():
   ...

对于列表,我们分离索引和元素,写作:

for index, element in enumerate(xxx):
   ...

所以如在第11和第13行,使用生成器推导式分别处理字典和列表,这样得到的key_value_iter生成器对象,就可以在第16行被相同的 for 循环迭代。

我们知道,在 Python 里面可以迭代的对象除了字典和列表以外,还有很多其他的对象,不过我这里只处理了字典和列表。大家也可以试一试修改10-15行的条件判断,增加对其他可迭代对象的处理逻辑。

代码第16-22行,对处理以后的 key-value 进行迭代。首先记录到当前字段为止的迭代路径到current_path列表中。然后判断当前字段是不是目标字段。如果是,那么把当前的路径通过 yield 抛出来。如果当前路径的值是列表或者字典,那么把这个值递归传入 iter_node 方法,进一步检查内部还有没有目标字段。需要注意的是,无论当前字段是不是目标字段,只要它的值是列表或者字典,都需要继续迭代。因为即使当前字段的名字是目标字段,但也许它内部还有某个子孙字段的字段名也是目标字段名。

对于普通函数来说,要递归调用,直接return 当前函数(参数)就可以了。但是对于生成器来说,要递归调用,就需要使用yield from 当前函数名(参数)

由于iter_node方法返回的是一个生成器对象,在 find_onefind_all方法中,for 循环每一次迭代,都能拿到一条从20行抛出来的到目标字段的路径。而在find_one方法中,当我们拿到第一条路径时,不再继续迭代,那么就可以节省大量的时间,减少迭代次数。

正确使用

有了这个工具以后,我们可以直接用它来解析数据,也可以用来辅助分析数据。例如,Twitter 时间线的正文是在full_text中,我可以直接用 JsonPathFinder 获取所有的正文:

但有时候,我们除了获取正文外,还需要每一条推文的其他信息,如下图所示:

可以看到, 这种情况下,我们可以先获取从外层到full_text的路径列表,然后再人工对列表进行一些加工,辅助开发:

从打印出来的路径列表里面可以看到,我们只需要获取globalObjects->tweets就可以了。它的值是20个字典,每个字典的 Key 是推文的 ID,Value 是推文的详情。这个时候,我们再人工去修改一下代码,也能方便地提取一条推文的全部字段。

Owner
kingname
Senior data mining engineer, Microsoft MVP.
kingname
A Python tool that parses JSON documents using JsonPath

A Python tool that parses JSON documents using JsonPath

8 Dec 18, 2022
Python script to extract news from RSS feeds and save it as json.

Python script to extract news from RSS feeds and save it as json.

Alex Trbznk 14 Dec 22, 2022
Atom, RSS and JSON feed parser for Python 3

Atoma Atom, RSS and JSON feed parser for Python 3. Quickstart Install Atoma with pip: pip install atoma

Nicolas Le Manchet 95 Nov 28, 2022
A fast JSON parser/generator for C++ with both SAX/DOM style API

A fast JSON parser/generator for C++ with both SAX/DOM style API Tencent is pleased to support the open source community by making RapidJSON available

Tencent 12.6k Dec 30, 2022
Console to handle object storage using JSON serialization and deserealization.

Console to handle object storage using JSON serialization and deserealization. This is a team project to develop a Python3 console that emulates the AirBnb object management.

Ronald Alexander 3 Dec 03, 2022
simdjson : Parsing gigabytes of JSON per second

JSON is everywhere on the Internet. Servers spend a *lot* of time parsing it. We need a fresh approach. The simdjson library uses commonly available SIMD instructions and microparallel algorithms to

16.3k Dec 29, 2022
A Cobalt Strike Scanner that retrieves detected Team Server beacons into a JSON object

melting-cobalt 👀 A tool to hunt/mine for Cobalt Strike beacons and "reduce" their beacon configuration for later indexing. Hunts can either be expans

Splunk GitHub 150 Nov 23, 2022
Json utils is a python module that you can use when working with json files.

Json-utils Json utils is a python module that you can use when working with json files. it comes packed with a lot of featrues Features Converting jso

Advik 4 Apr 24, 2022
import json files directly in your python scripts

Install Install from git repository pip install git+https://github.com/zaghaghi/direct-json-import.git Use With the following json in a file named inf

Hamed Zaghaghi 51 Dec 01, 2021
MOSP is a platform for creating, editing and sharing validated JSON objects of any type.

MONARC Objects Sharing Platform Presentation MOSP is a platform for creating, editing and sharing validated JSON objects of any type. You can use any

CASES Luxembourg 72 Dec 14, 2022
Convert your JSON data to a valid Python object to allow accessing keys with the member access operator(.)

JSONObjectMapper Allows you to transform JSON data into an object whose members can be queried using the member access operator. Unlike json.dumps in

Owen Trump 4 Jul 20, 2022
The ldap2json script allows you to extract the whole LDAP content of a Windows domain into a JSON file.

ldap2json The ldap2json script allows you to extract the whole LDAP content of a Windows domain into a JSON file. Features Authenticate with password

Podalirius 68 Dec 07, 2022
JSON for Modern C++ Release Scripts

JSON for Modern C++ Release Scripts Preparations Install required tools: make install_requirements. Add required keys to config.json (apparently not c

Niels Lohmann 4 Sep 19, 2022
Same as json.dumps or json.loads, feapson support feapson.dumps and feapson.loads

Same as json.dumps or json.loads, feapson support feapson.dumps and feapson.loads

boris 5 Dec 01, 2021
Roamtologseq - A script loads a json export of a Roam graph and cleans it up for import into Logseq

Roam to Logseq The script loads a json export of a Roam graph and cleans it up f

Sebastian Pech 4 Mar 07, 2022
A JSON utility library for Python featuring Django-style queries and mutations.

JSON Enhanced JSON Enhanced implements fast and pythonic queries and mutations for JSON objects. Installation You can install json-enhanced with pip:

Collisio Technologies 4 Aug 22, 2022
Editor for json/standard python data

Editor for json/standard python data

1 Dec 07, 2021
A fast streaming JSON parser for Python that generates SAX-like events using yajl

json-streamer jsonstreamer provides a SAX-like push parser via the JSONStreamer class and a 'object' parser via the ObjectStreamer class which emits t

Kashif Razzaqui 196 Dec 15, 2022
JSON Interoperability Vulnerability Labs

JSON Interoperability Vulnerability Labs Description These are the companion labs to my research article "An Exploration of JSON Interoperability Vuln

Bishop Fox 168 Dec 25, 2022
Creates fake JSON files from a JSON schema

Use jsf along with fake data generators to provide consistent and meaningful fake data for your system.

Andy Challis 86 Jan 03, 2023