0

как получить значения service, где значение replicas равно 2?

[
  {
    "replicas": 1,
    "version": "8.0.0",
    "env": "aw4",
    "service": "cat-prev-v2",
    "jobname": "cat/AppManage.8"
  },
  {
    "replicas": 2,
    "version": "2.0.8",
    "env": "aw3",
    "service": "cat-prev-v2",
    "jobname": "cat/AppManage.2"
  }
]

Пытался

import json
path = '/Users/isamarskii/Downloads/json/small.json'

with open(path, 'r') as json_data:
    parsed_data = json.load((json_data))
    #print(parsed_data)
    for i in parsed_data:
        if parsed_data['replicas'] == 2:
            print(parsed_data['service'])

Но получал ошибку TypeError: list indices must be integers or slices, not str

1

1 ответ 1

0

Правильный варинт

import json

path = '/Users/isamarskii/Downloads/json/small.json'

with open(path, 'r') as json_data:
    parsed_data = json.load((json_data))
    #print(parsed_data)
    for i in parsed_data:
        if i['replicas'] == 1:
            print(i['service'])

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.