Shell-Script

使用 SHELL 或 Python 對 JSON 的 API 呼叫到變數

  • November 20, 2021

我對一個看起來有點像這樣的 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 或 description 作為變數排除特定的 device_groups 嗎?

這正是我想要的,我相信@they 可以讓它看起來更優雅,但它可以滿足我的需要。

#!/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":

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

引用自:https://unix.stackexchange.com/questions/678092