使用 SHELL 或 Python 将 JSON 调用为变量的 API

使用 SHELL 或 Python 将 JSON 调用为变量的 API

我有一个对 url 的 API 访问,看起来有点像这样:

curl https://api_url.com/device_groups/ -u api_key:

JSON 输出:

{
   "data":[
      {
         "type":"device_group",
         "id":85015,
         "attributes":{
            "name":"Initial"
         },
         "relationships":{
            "devices":{
               "data":[
                  
               ]
            }
         }
      },
      {
         "type":"device_group",
         "id":85683,
         "attributes":{
            "name":"mode-4"
         },
         "relationships":{
            "devices":{
               "data":[
                  
               ]
            }
         }
      },
      {
         "type":"device_group",
         "id":85684,
         "attributes":{
            "name":"Employees-3"
         },
         "relationships":{
            "devices":{
               "data":[
                  {
                     "type":"device",
                     "id":506044
                  },
                  {
                     "type":"device",
                     "id":658670
                  },
                  {
                     "type":"device",
                     "id":506034
                  },
                  {
                     "type":"device",
                     "id":506037
                  },
                  {
                     "type":"device",
                     "id":506038
                  },
                  {
                     "type":"device",
                     "id":506046
                  },
                  {
                     "type":"device",
                     "id":506043
                  },
                  {
                     "type":"device",
                     "id":658669
                  },
                  {
                     "type":"device",
                     "id":506036
                  },
                  {
                     "type":"device",
                     "id":502256
                  }
               ]
            }
         }
      },
      {
         "type":"device_group",
         "id":91589,
         "attributes":{
            "name":"Subcontractors-2"
         },
         "relationships":{
            "devices":{
               "data":[
                  {
                     "type":"device",
                     "id":658668
                  },
                  {
                     "type":"device",
                     "id":658671
                  },
                  {
                     "type":"device",
                     "id":506051
                  },
                  {
                     "type":"device",
                     "id":506048
                  },
                  {
                     "type":"device",
                     "id":506040
                  },
                  {
                     "type":"device",
                     "id":506050
                  },
                  {
                     "type":"device",
                     "id":506042
                  },
                  {
                     "type":"device",
                     "id":502236
                  },
                  {
                     "type":"device",
                     "id":506041
                  },
                  {
                     "type":"device",
                     "id":523577
                  },
                  {
                     "type":"device",
                     "id":506053
                  },
                  {
                     "type":"device",
                     "id":655780
                  },
                  {
                     "type":"device",
                     "id":508070
                  },
                  {
                     "type":"device",
                     "id":506052
                  },
                  {
                     "type":"device",
                     "id":506047
                  },
                  {
                     "type":"device",
                     "id":506035
                  },
                  {
                     "type":"device",
                     "id":506045
                  },
                  {
                     "type":"device",
                     "id":506039
                  },
                  {
                     "type":"device",
                     "id":506049
                  },
                  {
                     "type":"device",
                     "id":589426
                  }
               ]
            }
         }
      },
      {
         "type":"device_group",
         "id":92316,
         "attributes":{
            "name":"Employees-2"
         },
         "relationships":{
            "devices":{
               "data":[
                  {
                     "type":"device",
                     "id":673452
                  },
                  {
                     "type":"device",
                     "id":576554
                  },
                  {
                     "type":"device",
                     "id":672077
                  },
                  {
                     "type":"device",
                     "id":589167
                  }
               ]
            }
         }
      },
      {
         "type":"device_group",
         "id":92325,
         "attributes":{
            "name":"Employees"
         },
         "relationships":{
            "devices":{
               "data":[
                  
               ]
            }
         }
      },
      {
         "type":"device_group",
         "id":94908,
         "attributes":{
            "name":"mode 2"
         },
         "relationships":{
            "devices":{
               "data":[
                  {
                     "type":"device",
                     "id":501727
                  }
               ]
            }
         }
      },
      {
         "type":"device_group",
         "id":95017,
         "attributes":{
            "name":"Mode 1"
         },
         "relationships":{
            "devices":{
               "data":[
                  
               ]
            }
         }
      },
      {
         "type":"device_group",
         "id":95381,
         "attributes":{
            "name":"Employees-test"
         },
         "relationships":{
            "devices":{
               "data":[
                  {
                     "type":"device",
                     "id":658672
                  }
               ]
            }
         }
      },
      {
         "type":"device_group",
         "id":95382,
         "attributes":{
            "name":"Subcontracters-test"
         },
         "relationships":{
            "devices":{
               "data":[
                  
               ]
            }
         }
      }
   ],
   "has_more":false
}

我需要将每个设备移动到预定义的设备组 ID 中,然后将设备返回到从初始 JSON curl 获取的原始组。

例如。

curl https://api_url.com/group/$predefined_group_id/devices/506044 -u api_key: -X POST

curl https://api_url.com/group/$original_group_id/devices/506044 -u api_key: -X POST

您还可以通过 id 或描述作为变量排除特定的 device_groups 吗?

答案1

命令jq

jq -r '
    .data.id as $groupid |
    .data.relationships.devices.data[].id |
    @sh "https://api_url.com/group/\($groupid)/devices/\(.)"' file

...将提取组 ID,然后提取数组id中的所有值data。对于每个id值,它将构造一个要输出的 URL。

要在调用中使用这些生成的 URL curl

jq -r '
    .data.id as $groupid |
    .data.relationships.devices.data[].id |
    @sh "https://api_url.com/group/\($groupid)/devices/\(.)"' file |
xargs -I {} curl -X POST -u api_key: {}

给定问题中的 JSON 文档,这将执行以下命令:

curl -X POST -u api_key: https://api_url.com/group/85684/devices/506044
curl -X POST -u api_key: https://api_url.com/group/85684/devices/506034
curl -X POST -u api_key: https://api_url.com/group/85684/devices/506037
curl -X POST -u api_key: https://api_url.com/group/85684/devices/506038
curl -X POST -u api_key: https://api_url.com/group/85684/devices/506046
curl -X POST -u api_key: https://api_url.com/group/85684/devices/506043
curl -X POST -u api_key: https://api_url.com/group/85684/devices/658669
curl -X POST -u api_key: https://api_url.com/group/85684/devices/506036
curl -X POST -u api_key: https://api_url.com/group/85684/devices/502256

您还可以使用这些 URL 来尽可能少地xargs调用:curl

jq -r '
    .data.id as $groupid |
    .data.relationships.devices.data[].id |
    @sh "https://api_url.com/group/\($groupid)/devices/\(.)"' file |
xargs curl -X POST -u api_key:

这将生成单个命令

curl -X POST -u api_key: https://api_url.com/group/85684/devices/506044 https://api_url.com/group/85684/devices/506034 https://api_url.com/group/85684/devices/506037 https://api_url.com/group/85684/devices/506038 https://api_url.com/group/85684/devices/506046 https://api_url.com/group/85684/devices/506043 https://api_url.com/group/85684/devices/658669 https://api_url.com/group/85684/devices/506036 https://api_url.com/group/85684/devices/502256

您可以从命令行提供组 ID,而不是像这样使用数据中找到的组 ID(使用变量some_groupid):

jq -r --arg groupid "$some_groupid" '
    .data.relationships.devices.data[].id |
    @sh "https://api_url.com/group/\($groupid)/devices/\(.)"' file

答案2

这正是我想要的,我确信@他们可以让它看起来更优雅,但它满足了我的需要。

#!/bin/bash

# Set groupid also read and write API variables
group="80000"
read_api="read_api"
write_api="write_api"

# get all devicesid and groupid and create groups.json file
curl https://api_url.com/device_groups -u "$read_api": > groups.json

# Move all devices into predefined groupid
jq -r --arg groupid $group '
    .data[].relationships.devices.data[].id |
    @sh "https://api_url.com/device_groups/\($groupid)/devices/\(.)"' groups.json |
xargs curl -X POST -u "$write_api":

# Wait for the groupid to propagate
sleep 60;

# Move all devices into original groupid
jq -r '
    .data[] |
    "https://api_url.com/device_groups/\(.id)/devices/\(.relationships.devices.data[].id)"' groups.json |
xargs curl -X POST -u "$write_api":

相关内容