[英]parsing/extracting nested JSON data with Python under Conditions
I am trying to extract/parse the values from specifics in a JSON file that I did a post request. 我正在尝试从JSON文件中提取/解析我执行发布请求的特定值。
Here is the JSON File. 这是JSON文件。 I am trying to get the values from the Key "AN".
我想从Key“AN”中获取值。 I want to be able to extract values such as " shannoncampbell_znyq1 ", " katiekapprelmac ", etc. such that the values from the second row does not equal to the number zero.
我希望能够提取诸如“ shannoncampbell_znyq1 ”,“ katiekapprelmac ”等的值,使得第二行的值不等于零。 For example, since the second row values (the for this row is T7 ) of katiekapprelmac does not equal to zero, my code should spit that out ( katiekapprelmac should be the output).
例如,由于katiekapprelmac的第二行值(此行为T7 )不等于零,我的代码应该吐出( katiekapprelmac应该是输出)。 However it does not.
但事实并非如此。
JSON File: JSON文件:
{
"id": "jsonrpc",
"jsonrpc": "2.0",
"result": {
"result": [
{
"AccountId": 697429,
"Flags": [
"AutoDeployed"
],
"PartnerId": 287562,
"Settings": [
{
"AN": "shannoncampbell_znyq1"
},
{
"T7": "0"
}
]
},
{
"AccountId": 725177,
"Flags": null,
"PartnerId": 287562,
"Settings": [
{
"AN": "katiekapprelmac"
},
{
"T7": "5"
}
]
},
{
"AccountId": 689130,
"Flags": [
"AutoDeployed"
],
"PartnerId": 287562,
"Settings": [
{
"AN": "sara-pc_wpv7h"
},
{
"T7": "0"
}
]
},
{
"AccountId": 697531,
"Flags": null,
"PartnerId": 287562,
"Settings": [
{
"AN": "kaelaweeksmac"
},
{
"T7": "0"
}
]
},
{
"AccountId": 615877,
"Flags": null,
"PartnerId": 249098,
"Settings": [
{
"AN": "elenimacbookpro"
},
{
"T7": "0"
}
]
},
{
"AccountId": 700661,
"Flags": null,
"PartnerId": 287562,
"Settings": [
{
"AN": "sethnickersonmac"
},
{
"T7": "0"
}
]
},
Here is my python code: 这是我的python代码:
response2 = requests.request("POST", url, data=payload2, headers=headers)
j = json.loads(response2.text)
def find_all(item, level):
if isinstance(item, dict):
for k in item:
(find_all(item[k], level+1))
else:
print(item)
def find_only(item, level):
if isinstance(item, dict):
for k in item:
(find_only(item[k], level+1))
for each in j['result']['result']:
if (find_only(each['Settings'][1], 0)) != json.loads("0"):
find_all(each['Settings'][0], 0)
Instead, I get all the keys in the output. 相反,我得到输出中的所有键。 I get the following:
我得到以下内容:
shannoncampbell_znyq1
katiekapprelmac
sara-pc_wpv7h
kaelaweeksmac
elenimacbookpro
sethnickersonmac
Rather than just katiekapprelmac 而不仅仅是katiekapprelmac
Please help. 请帮忙。 Thanks
谢谢
In the code: 在代码中:
for each in j['result']['result']:
if (find_only(each['Settings'][1], 0)) != json.loads("0"):
find_all(each['Settings'][0], 0)
I actually see, your condition is always True
, as you are not returning anything in find_only()
. 我实际上看到,你的条件总是为
True
,因为你没有在find_only()
返回任何东西。
I don't know, why you are using level and so many recursive function. 我不知道,为什么你使用级别和这么多的递归函数。 Although it's easy to extract result as per your data posted.
虽然根据您发布的数据提取结果很容易。 please find below code.
请找到以下代码。
response2 = requests.request("POST", url, data=payload2, headers=headers)
j = json.loads(response2.text)
for each in j['result']['result']:
if each['Settings'][1]['T7'] not in ["0", 0]:
print(each['Settings'][0]['AN'])
If your response data is little complex then please post for exact solution. 如果您的回复数据有点复杂,请发布确切的解决方案。
If you have multiple key name then please look at below code: 如果您有多个密钥名称,请查看以下代码:
response2 = requests.request("POST", url, data=payload2, headers=headers)
j = json.loads(response2.text)
def find_all(item):
if isinstance(item, dict):
for k in item:
return item[k]
# If item is non dict and you want to return this as well on `True`.
# Uncomment below commented lines.
# else:
# item
def find_only(item):
if isinstance(item, dict):
for k in item:
return item[k]
for each in j['result']['result']:
if (find_only(each['Settings'][1])) != str(json.loads("0")):
print(find_all(each['Settings'][0]))
jsonpath-ng can help you with this. jsonpath-ng可以帮助你解决这个问题。
from jsonpath_ng.ext import parse
found = parse(f"$..Settings").find(data)
if found:
for i in found:
if ''.join(i.value[1].values()) != '0':
print(i.value[0]['AN'])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.