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 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
Define your JSON schema as Python dataclasses

Define your JSON schema as Python dataclasses

62 Sep 20, 2022
Easy JSON wrapper modfied to wrok with suggestions

🈷️ Suggester Easy JSON wrapper modfied to wrok with suggestions. This was made for small discord bots, for big bots you should not use this. 📥 Usage

RGBCube 1 Jan 22, 2022
Random JSON Key:Pair Json Generator

Random JSON Key:Value Pair Generator This simple script take an engish dictionary of words and and makes random key value pairs. The dictionary has ap

Chris Edwards 1 Oct 14, 2021
jq for Python programmers Process JSON and HTML on the command-line with familiar syntax.

jq for Python programmers Process JSON and HTML on the command-line with familiar syntax.

Denis Volk 3 Jan 09, 2022
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
Simple Python Library to convert JSON to XML

json2xml Simple Python Library to convert JSON to XML

Vinit Kumar 79 Nov 11, 2022
Make JSON serialization easier

Make JSON serialization easier

4 Jun 30, 2022
cysimdjson - Very fast Python JSON parsing library

Fast JSON parsing library for Python, 7-12 times faster than standard Python JSON parser.

TeskaLabs 235 Dec 29, 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
RedisJSON - a JSON data type for Redis

RedisJSON is a Redis module that implements ECMA-404 The JSON Data Interchange Standard as a native data type. It allows storing, updating and fetching JSON values from Redis keys (documents).

3.4k Dec 29, 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
A python library to convert arbitrary strings representing business opening hours into a JSON format that's easier to use in code

A python library to convert arbitrary strings representing business opening hours into a JSON format that's easier to use in code

Adrian Edwards 9 Dec 02, 2022
A daily updated JSON dataset of all the Open House London venues, events, and metadata

Open House London listings data All of it. Automatically scraped hourly with updates committed to git, autogenerated per-day CSV's, and autogenerated

Jonty Wareing 4 Jan 01, 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
A tools to find the path of a specific key in deep nested JSON.

如何快速从深层嵌套 JSON 中找到特定的 Key #公众号 在爬虫开发的过程中,我们经常遇到一些 Ajax 加载的接口会返回 JSON 数据。

kingname 56 Dec 13, 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
A Python application to transfer Zeek ASCII (not JSON) logs to Elastic/OpenSearch.

zeek2es.py This Python application translates Zeek's ASCII TSV logs into ElasticSearch's bulk load JSON format. For JSON logs, see Elastic's File Beat

Corelight, Inc. 28 Dec 22, 2022
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
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