标准化/展平 JSON 嵌套列表

问题描述 投票:0回答:1

我有一个包含大量 JSON(此处缩写)的列表,它们非常嵌套,我想对其进行规范化并将其全部提升到 JSON 中的一个级别。

我有:

[{'index': 'exp-000005',
  'type': '_doc',
  'id': 'jdaksjdlkj',
  'score': 9.502488,
  'source': {'user': {'resource_uuid': '123'},
   'verb': 'REPLIED',
   'resource': {'resource_uuid': '/home'},
   'timestamp': '2022-01-20T08:14:00+00:00',
   'with_result': {},
   'in_context': {'screen_width': '3440',
    'screen_height': '1440',
    'build_version': '7235',
    'clientid': '123',
    'question': 'Hallo',
    'request_time': '403',
    'status': 'success',
    'response': '[]',
    'language': 'de'}}},
 {'index': 'exp-000005',
  'type': '_doc',
  'id': 'dddddd',
  'score': 9.502488,
  'source': {'user': {'resource_uuid': '44444'},
   'verb': 'REPLIED',
   'resource': {'resource_uuid': '/home'},
   'timestamp': '2022-01-20T08:14:10+00:00',
   'with_result': {},
   'in_context': {'screen_width': '3440',
    'screen_height': '1440',
    'build_version': '7235',
    'clientid': '345',
    'question': 'Ich brauche Hilfe',
    'request_time': '111',
    'status': 'success',
    'response': '[{"recipientid":"789", "text":"Bitte sehr."}, {"recipientid":"888", "text":"Kann ich Ihnen noch mit etwas anderem behilflich sein?"}]',
    'language': 'de'}}},
 {'index': 'exp-000005',
  'type': '_doc',
  'id': 'jdhdgs',
  'score': 9.502488,
  'source': {'user': {'resource_uuid': '333'},
   'verb': 'REPLIED',
   'resource': {'resource_uuid': '/home'},
   'timestamp': '2022-01-20T08:14:19+00:00',
   'with_result': {},
   'in_context': {'screen_width': '3440',
    'screen_height': '1440',
    'build_version': '7235',
    'clientid': '007',
    'question': 'Zertifikate',
    'request_time': '121',
    'status': 'success',
    'response': '[{"recipientid":"345", "text":"Künstliche Intelligenz"}, {"recipientid":"123", "text":"Kann ich Ihnen noch mit etwas anderem behilflich sein?"}]',
    'language': 'de'}}}]

我想要什么:

[{'index': 'exp-000005',
  'type': '_doc',
  'id': 'jdaksjdlkj',
  'score': 9.502488,
  'resource_uuid': '123',
  'verb': 'REPLIED',
  'resource_uuid': '/home',
  'timestamp': '2022-01-20T08:14:00+00:00',
  'with_result': {},
  'screen_width': '3440',
  'screen_height': '1440',
  'build_version': '7235',
  'clientid': '123',
  'question': 'Hallo',
  'request_time': '403',
  'status': 'success',
  'response': '[]',
  'language': 'de'},
 {'index': 'exp-000005',
  'type': '_doc',
  'id': 'dddddd',
  'score': 9.502488,
  'resource_uuid': '44444',
  'verb': 'REPLIED',
  'resource_uuid': '/home',
  'timestamp': '2022-01-20T08:14:10+00:00',
  'with_result': {},
  'screen_width': '3440',
  'screen_height': '1440',
  'build_version': '7235',
  'clientid': '345',
  'question': 'Ich brauche Hilfe',
  'request_time': '111',
  'status': 'success',
  'recipientid1': "789", 
  'text1': "Bitte sehr",
  'recipientid2': "888", 
  'text2': "Kann ich Ihnen noch mit etwas anderem behilflich sein?",
  'language': 'de'},
 {'index': 'exp-000005',
  'type': '_doc',
  'id': 'jdhdgs',
  'score': 9.502488,
  'resource_uuid': '333',
  'verb': 'REPLIED',
  'resource_uuid': '/home',
  'timestamp': '2022-01-20T08:14:19+00:00',
  'with_result': {},
  'screen_width': '3440',
  'screen_height': '1440',
  'build_version': '7235',
  'clientid': '007',
  'question': 'Zertifikate',
  'request_time': '121',
  'status': 'success',
  'recipientid1': "345", 
  'text1': "Künstliche Intelligenz",
  'recipientid1': "123", 
  'text2': "Kann ich Ihnen noch mit etwas anderem behilflich sein?",
  'language': 'de'}]

我不确定是否使用 JSON 扁平化或 pandas 规范化的一些魔法。

python json
1个回答
0
投票

如果子对象/字典没有重复命名,你可以直接解压它。

递归函数解压并返回一个干净的对象。假设你只走了 x 层,你可以自己编码。如果深度未知,则必须递归。

可能有解释链接 -> 通过谷歌搜索找到

© www.soinside.com 2019 - 2024. All rights reserved.