Как правильно выбрать переменные из json в bash скрипте?

пишу скрипт, есть АПИ ответ, по хорошему мне все это нужно теперь использовать в скрипте, как правильно будет использовать , к примеру получить "ip" и "name"
Пример вывода

{
  "server": {
    "id": 20167538,
    "name": "my-server",
    "status": "initializing",
    "created": "2022-05-04T09:45:09+00:00",
    "public_net": {
      "ipv4": {
        "ip": "142.132.235.187",
        "blocked": false,
        "dns_ptr": "static.187.235.132.142.clients.your-server.de"
      },
      "ipv6": {
        "ip": "2a01:4f8:c012:4462::/64",
        "blocked": false,
        "dns_ptr": [],
        "id": 5600851
      },
      "floating_ips": [],
      "firewalls": []
    },
    "private_net": [],
    "server_type": {
      "id": 25,
      "name": "cpx41",
      "description": "CPX 41",
      "cores": 8,
      "memory": 16.0,
      "disk": 240,
      "deprecated": false,
      "prices": [
        {
          "location": "ash",
          "price_hourly": {
            "net": "0.0380000000",
            "gross": "0.0467400000000000"
          },
          "price_monthly": {
            "net": "22.9000000000",
            "gross": "28.1670000000000000"
          }
        },
        {
          "location": "fsn1",
          "price_hourly": {
            "net": "0.0380000000",
            "gross": "0.0467400000000000"
          },
          "price_monthly": {
            "net": "22.9000000000",
            "gross": "28.1670000000000000"
          }
        },
        {
          "location": "nbg1",
          "price_hourly": {
            "net": "0.0380000000",
            "gross": "0.0467400000000000"
          },
          "price_monthly": {
            "net": "22.9000000000",
            "gross": "28.1670000000000000"
          }
        },
        {
          "location": "hel1",
          "price_hourly": {
            "net": "0.0380000000",
            "gross": "0.0467400000000000"
          },
          "price_monthly": {
            "net": "22.9000000000",
            "gross": "28.1670000000000000"
          }
        }
      ],
      "storage_type": "local",
      "cpu_type": "shared"
    },
    "datacenter": {
      "id": 4,
      "name": "fsn1-dc14",
      "description": "Falkenstein 1 DC14",
      "location": {
        "id": 1,
  "name": "fsn1",
        "description": "Falkenstein DC Park 1",
        "country": "DE",
        "city": "Falkenstein",
        "latitude": 50.47612,
        "longitude": 12.370071,
        "network_zone": "eu-central"
      },
      "server_types": {
        "supported": [
          1,
          3,
          5,
          7,
          9,
          11,
          12,
          13,
          14,
          15,
          22,
          23,
          24,
          25,
          26,
          33,
          34,
          35,
          36,
          37,
          38
        ],
        "available": [
          1,
          3,
          5,
          7,
          9,
          11,
          12,
          13,
          14,
          22,
          23,
          24,
          25,
          26,
          33,
          34,
          35,
          36,
          37,
          38
        ],
        "available_for_migration": [
          1,
          3,
          5,
          7,
          9,
          11,
          12,
          13,
          14,
          22,
          23,
          24,
          25,
          26,
          27,
          28,
          29,
          30,
          31,
          32,
          33,
          34,
          35,
          36,
          37,
          38
        ]
      }
 },
    "image": {
      "id": 15512617,
      "type": "system",
      "status": "available",
      "name": "ubuntu-20.04",
      "description": "Ubuntu 20.04",
      "image_size": null,
      "disk_size": 5,
      "created": "2020-04-23T17:55:14+00:00",
      "created_from": null,
      "bound_to": null,
      "os_flavor": "ubuntu",
      "os_version": "20.04",
      "rapid_deploy": true,
      "protection": {
        "delete": false
      },
      "deprecated": null,
      "labels": {},
      "deleted": null
    },
    "iso": null,
    "rescue_enabled": false,
    "locked": false,
    "backup_window": null,
    "outgoing_traffic": null,
    "ingoing_traffic": null,
    "included_traffic": 21990232555520,
    "protection": {
      "delete": false,
      "rebuild": false
    },
    "labels": {},
    "volumes": [],
    "load_balancers": [],
    "primary_disk_size": 240,
    "placement_group": null
  },
  "action": {
    "id": 467632859,
    "command": "create_server",
    "status": "running",
    "progress": 0,
    "started": "2022-05-04T09:45:09+00:00",
    "finished": null,
    "resources": [
      {
        "id": 20167538,
        "type": "server"
      }
    ],
    "error": null
  },
  "next_actions": [
    {
      "id": 467632855,
      "command": "start_server",
      "status": "running",
      "progress": 0,
      "started": "2022-05-04T09:45:09+00:00",
      "finished": null,
      "resources": [
        {
          "id": 20167538,
          "type": "server"
        }
      ],
      "error": null
    }
  ],
  "root_password": null
}
  • Вопрос задан
  • 661 просмотр
Решения вопроса 2
paran0id
@paran0id
Умный, но ленивый
Воспользуйтесь утилитой jq.

$ cat example.json  | jq  '.server.name, .server.public_net.ipv4.ip'
"my-server"
"142.132.235.187"
Ответ написан
xotkot
@xotkot
хорошо есть и хорошо весьма
$ cat example.json |jq -r '.server | .name,.public_net.ipv4.ip'
my-server
142.132.235.187


если нужно сразу в переменные загнать то
$ cat example.json| jq -r '.server | "name=\(.name)","ip=\(.public_net.ipv4.ip)"'
name=my-server
ip=142.132.235.187

обернуть в eval и всё

$ eval $(jq -r '.server | "name=\(.name)","ip=\(.public_net.ipv4.ip)"' example.json)

$ echo $ip
142.132.235.187
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы