使用python在JSON对象中添加新列表

2024-09-29 23:20:33 发布

您现在位置:Python中文网/ 问答频道 /正文

在执行GET之后,我有以下JSON-

{
"execution": [
    {
        "a": 1634112377,
        "b": 16341124909,
        "c": "set"
    },
    {
    "a": NNN1634112377,
    "b": NNN16341124909,
    "c": "set"
}
],
"href": "https://realtime_data"}

我必须根据变量-ID多次调用GET端点,并且需要检查以下内容- A.如果执行节点包含的a和b的值与我从现有数据中传递的值相同,请跳过处理并转到下一个ID B如果没有,则将其添加到节点-例如-

{
"execution": [
    {
        "a": 1634112377,
        "b": 16341124909,
        "c": "set"
    },
    {
    "a": NNN1634112377,
    "b": NNN16341124909,
    "c": "set"
},
     {
    "a": PPP1634112377,
    "b": PPP16341124909,
    "c": "set"
}
],
"href": "https://realtime_data"}


construct_payload = '''{
    "execution": [
        {
            "a": 1631253720,
            "b": 1631253780,
            "c": "discharge"
        }
    ]
}'''

for id in id_list:
    URL = 'https://aabc.com' + util_ID + '/units/'+id+'/schedule'
    print(URL)
    statusCode= requests.put(URL,data= construct_payload,headers=header_bearerToken)
    print(statusCode)'

我可以理解我需要先添加get步骤,然后使用IF进行检查,但不确定如何在收到的JSON中添加新条目


Tags: httpsidjsonurldataget节点construct

热门问题