在Python中从JSON中提取多个字段

j91ykkif  于 2023-03-24  发布在  Python
关注(0)|答案(4)|浏览(119)

下面是一个JSON对象:

{
  'organisations': {
    'total-items': '41477',
    'organisation': [
      {
        'mappings': None,
        'active-request': 'false',
        'identifiers': {
          'identifier': {
            'code': 'ORG-100023310',
            'code-system': '100000167446',
            'code-system-name': ' OMS Organization Identifier'
          }
        },
        'name': 'advanceCOR GmbH',
        'operational-attributes': {
          'created-on': '2016-10-18T15:38:34.322+02:00',
          'modified-on': '2022-11-02T08:23:13.989+01:00'
        },
        'locations': {
          'location': [
            {
              'location-id': {
                'link': {
                  'href': 'https://v1/locations/LOC-100052061'
                },
                'id': 'LOC-100052061'
              }
            },
            {
              'location-id': {
                'link': {
                  'href': 'https://v1/locations/LOC-100032442'
                },
                'id ': 'LOC-100032442'
              }
            },
            {
              'location-id': {
                'link': {
                  'href': 'https://v1/locations/LOC-100042003'
                },
                'id': 'LOC-100042003'
              }
            }
          ]
        },
        'organisation-id': {
          'link': {
            'rel': 'self',
            'href': 'https://v1 /organisations/ORG-100023310'
          },
          'id': 'ORG-100023310'
        },
        'status': 'ACTIVE'
      },
      {
        'mappings': None,
        'active-request': 'false',
        'identifiers': {
          'identifier': {
            'code': 'ORG-100004261',
            'code-system': '100000167446',
            'code-system-name': 'OMS organization Identifier'
          }
        },
        'name': 'Beacon Pharmaceuticals Limited',
        'operational-attributes': {
          'created-on': '2016-10-18T14:48:16.293+02:00',
          'modified-on': '2022-10-12T08:26:24.645+02:00'
        },
        'locations': {
          'location': [
            {
              'location-id': {
                'link': {
                  'href': 'https://v1/locations/LOC-100005615'
                },
                'id': 'LOC-100005615'
              }
            },
            {
              'location-id': {
                'link': {
                  'href': 'https://v1/locations/LOC-100000912'
                },
                'id': 'LOC-100000912'
              }
            },
            {
              'location-id': {
                'link': {
                  'href': 'https://v1/locations/LOC-100043831'
                },
                'id': 'LOC-100043831'
              }
            }
          ]
        },
        'organisation-id': {
          'link': {
            'rel': 'self',
            'href': 'https://v1/organisations/ORG-100004261'
          },
          'id': 'ORG-100004261'
        },
        'status': 'ACTIVE'
      },

我想获取以下字段'代码,名称,位置,状态'为所有的org_id(类型d:class 'list',type of d[0)is class 'dict'.我正在使用以下代码行,但无法获取所有组织ID和location_id(只能获取1)(显示在当前输出中).我如何获取所有org_id的信息(显示在预期输出中)?
代码:

with open('organisations.json', encoding='utf-8') as f:
    d = json.load(f)
    print (d[0]['organisations']['organisation'][2]['identifiers']['identifier']['code'])   #code
    print (d[0]['organisations']['organisation'][2]['name'])                                #name
    print (d[0]['organisations']['organisation'][2]['locations'])                       #location
    print (d[0]['organisations']['organisation'][2]['status'])                          #status

电流输出:

ORG-100023310
advanceCOR GmbH
{'location': {'location-id': {'link': {'href': 'https://v1/locations/LOC-100052061'}, 'id': ' LOC-100052061'}}}
ACTIVE

预期产出:

org_id        org_name                         location_id                                  status
ORG-100023310 advanceCOR GmbH                  LOC-100052061, LOC-100032442, LOC-100042003  ACTIVE
ORG-100004261 Beacon Pharmaceuticals Limited   LOC-100005615, LOC-100000912, LOC-100043831         ACTIVE
zu0ti5jz

zu0ti5jz1#

你的尝试有两个缺陷;正如其他人所指出的,通过硬编码['organisation'][2],您只是提取了最后一项,而没有将位置字典序列化为字符串。
下面是代码的修改版本,它可以生成所需的输出。

import json

fmt = "{0:14}{1:33}{2:45}{3}"
print(fmt.format("org_id", "org_name", "location_id", "status"))

with open('organisations.json', encoding='utf-8') as f:
    d = json.load(f)
    for org in d['organisations']['organisation']:
        locations = ", ".join(loc['location-id']['id'] for loc in org['locations']['location'])
        print(fmt.format(
            org['identifiers']['identifier']['code'],
            org['name'], locations, org['status']))

您发布的“JSON”示例不是真正有效的JSON,因此我必须对其进行一些更改以使代码正常工作。
如果你是Python新手,你可能想避免字典理解loc['location-id']['id'] for loc in org['locations']['location'],而倾向于它的手写体表达:

locations = []
for loc in org['locations']['location']:
    locations.append(loc['location-id']['id'])
gc0ot86w

gc0ot86w2#

你可以使用for循环来遍历整个组织,

with open('organisations.json', encoding='utf-8') as f:
    d = json.load(f)

    # assuming above code is your working code
    # assign organization list in a variable
    organization_list = d[0]['organisations']['organisation']

    # now loop through that list
    for organization in organization_list:
        name = organization['name']
        # fetch other values similary

        # print the values here
        print(name)
t2a7ltrp

t2a7ltrp3#

因为只打印索引2,所以只得到1行输出。希望以下内容对您有所帮助。

with open('data.json') as file:
    data = json.load(file)
    
result = pd.DataFrame( columns=['org_id', 'org_name', "location_id", "status"])
organizations = data['organisations']['organisation']
for organization in organizations:
    record = {
        'org_id': organization['identifiers']['identifier']['code'],
        'org_name': organization['name'],
        'location_id': organization['locations'],
        'status': organization['status']
    }
    result_row = pd.DataFrame.from_records([record])
    result = pd.concat([result, result_row])
print(result)
vnzz0bqm

vnzz0bqm4#

以下摘录了这两个组织的信息。

import json

with open('organisations.json', encoding='utf-8') as f:
    d = json.load(f)
    
    for organisation in d['organisations']['organisation']:
        print(organisation['identifiers']['identifier']['code'])    #code
        print(organisation['name'])                                 #name
        print(organisation['locations']['location'])                #location
        print(organisation['status'])                               #status

产出

ORG-100023310
ORG-100023310
advanceCOR GmbH
[{'location-id': {'link': {'href': 'https://v1/locations/LOC-100052061'}, 'id': 'LOC-100052061'}}, {'location-id': {'link': {'href': 'https://v1/locations/LOC-100032442'}, 'id ': 'LOC-100032442'}}, 
{'location-id': {'link': {'href': 'https://v1/locations/LOC-100042003'}, 'id': 'LOC-100042003'}}]
ACTIVE
ORG-100004261
ORG-100004261
Beacon Pharmaceuticals Limited
[{'location-id': {'link': {'href': 'https://v1/locations/LOC-100005615'}, 'id': 'LOC-100005615'}}, {'location-id': {'link': {'href': 'https://v1/locations/LOC-100000912'}, 'id': 'LOC-100000912'}}, {'location-id': {'link': {'href': 'https://v1/locations/LOC-100043831'}, 'id': 'LOC-100043831'}}]
ACTIVE

相关问题