有没有办法使用 jsonpath-ng 从 JSON 中提取密钥?
Is there a way to extract the keys from JSON using jsonpath-ng?
我想从 JSON(实际上是 JSON 模式)创建一个键列表。 JSON 复杂且嵌套,所以我正在使用 jsonpath-ng 库。
我无法获取密钥列表。另一方面,使用以下语法成功提取值:print([match.value for match in tagNames])
.
尝试使用类似的语法提取密钥,如下所示...
from jsonpath_ng import jsonpath, parse
import json
filePath = "./sample.json"
with open(filePath, "r") as jsonFile:
jsonData = json.load(jsonFile)
tagNames = parse("tags[*].*").find(jsonData)
print([match.key for match in tagNames])
returns 这个错误:
AttributeError: 'DatumInContext' object has no attribute 'key'
虽然我在输入 'match.'
后收到 'key' 建议
JSON 我工作的例子:
{
"tags": [
{
"name": "auth",
"description": "Authentication"
},
{
"name": "b2b",
"description": "B 2b"
}
],
"paths": {
"/rest/auth": {
"post": {
"tags": [
"auth"
],
"operationId": "generateToken",
"consumes": [
"application/json"
],
"produces": [
"*/*"
],
"responses": {
"200": {
"description": "Generated"
},
"201": {
"description": "Created"
}
}
},
"get": {
"tags": [
"status"
],
"operationId": "check Token",
"consumes": [
"application/json"
],
"produces": [
"*/*"
],
"responses": {
"200": {
"description": "Generated"
},
"201": {
"description": "Created"
}
}
}
}
}
}
任务 1 - 在标签下获取键的更简单任务(预期结果:名称、描述)。
任务 2 - 更复杂的情况是从路径获取密钥(预期结果:post,获取)。
如果 jsonpath 不支持提取密钥,有人对嵌套 JSONs 有任何其他建议吗?
我也尝试将匹配结果转换为纯 python dict 以便使用 .keys() 获取密钥,但没有成功。
如果我没理解错的话,您可能正在寻找这样的东西:
from jsonpath_ng import parse
import json
my_str = """[your json string above]"""
data = json.loads(my_str)
第一个表达式:
sonpath_expr = parse('$.tags')
for d in jsonpath_expr.find(data):
for k in d.value[0].keys():
print(k)
输出:
name
description
第二个表达式:
jsonpath_expr = parse('$.paths..*..*')
for k in jsonpath_expr.find(data)[0].context.value.keys():
print(k)
输出:
post
get
我想从 JSON(实际上是 JSON 模式)创建一个键列表。 JSON 复杂且嵌套,所以我正在使用 jsonpath-ng 库。
我无法获取密钥列表。另一方面,使用以下语法成功提取值:print([match.value for match in tagNames])
.
尝试使用类似的语法提取密钥,如下所示...
from jsonpath_ng import jsonpath, parse
import json
filePath = "./sample.json"
with open(filePath, "r") as jsonFile:
jsonData = json.load(jsonFile)
tagNames = parse("tags[*].*").find(jsonData)
print([match.key for match in tagNames])
returns 这个错误:
AttributeError: 'DatumInContext' object has no attribute 'key'
虽然我在输入 'match.'
后收到 'key' 建议JSON 我工作的例子:
{
"tags": [
{
"name": "auth",
"description": "Authentication"
},
{
"name": "b2b",
"description": "B 2b"
}
],
"paths": {
"/rest/auth": {
"post": {
"tags": [
"auth"
],
"operationId": "generateToken",
"consumes": [
"application/json"
],
"produces": [
"*/*"
],
"responses": {
"200": {
"description": "Generated"
},
"201": {
"description": "Created"
}
}
},
"get": {
"tags": [
"status"
],
"operationId": "check Token",
"consumes": [
"application/json"
],
"produces": [
"*/*"
],
"responses": {
"200": {
"description": "Generated"
},
"201": {
"description": "Created"
}
}
}
}
}
}
任务 1 - 在标签下获取键的更简单任务(预期结果:名称、描述)。
任务 2 - 更复杂的情况是从路径获取密钥(预期结果:post,获取)。
如果 jsonpath 不支持提取密钥,有人对嵌套 JSONs 有任何其他建议吗?
我也尝试将匹配结果转换为纯 python dict 以便使用 .keys() 获取密钥,但没有成功。
如果我没理解错的话,您可能正在寻找这样的东西:
from jsonpath_ng import parse
import json
my_str = """[your json string above]"""
data = json.loads(my_str)
第一个表达式:
sonpath_expr = parse('$.tags')
for d in jsonpath_expr.find(data):
for k in d.value[0].keys():
print(k)
输出:
name
description
第二个表达式:
jsonpath_expr = parse('$.paths..*..*')
for k in jsonpath_expr.find(data)[0].context.value.keys():
print(k)
输出:
post
get