在Python中从JSON构建表

时间:2019-02-21 23:28:05

标签: python json etl

我正在尝试使用Python将JSON文本转换为标准数据表,但是对此我很少有经验,并且当我在线搜索解决方案时,发现我很难实现任何解决方案。

我试图使用ast.literal_eval,但不断遇到我无法解决的错误。

  

提高ValueError('格式错误的节点或字符串:'+ repr(node))

JSON:

{
    "duration": 202.0,
    "session_info": {
        "activation_uuid": "ab90d941-df9d-42c5-af81-069eb4f71515",
        "launch_uuid": "11101c41-2d79-42cc-bf6d-37be46802fc8"
    },
    "timestamp": "2019-01-18T11:11:26.135Z",
    "source_page_view_reference": {
        "page_uuid": "1bede017-7b77-461d-82ef-a6bbcfdae4d7",
        "page_id": "/group/More",
        "page_name": "More",
        "view_uuid": "9580f3c5-1116-432a-83bc-9d0b5337f661",
        "page_type": "Native"
    },
    "analytics_sdk": {
        "component_id": "datasdk",
        "component_version": "1.0.52"
    },
    "treatment_id": "mockTreat",
    "client_event_id": "2b3cd878-6932-410b-b1ad-bc40ae888fdc",
    "campaign_id": "mockCamp"
}

所需的表格格式(为显示目的而修剪的值):

Duration | session_info.activation_uuid | session_info.launch_uuid | timestamp  | etc
   202.0 |  ab90d941-df9d-42c5-af81-069 | 11101c41-2d79-42cc-bf6d- | 2019-01-18 | etc

任何直接帮助,或只是好的资源来学习这一点,将不胜感激。我很难找到可以直接与我要根据一系列相似的JSON创建表的内容进行对话的项目。

2 个答案:

答案 0 :(得分:2)

与表进行交互时,几乎总是使用

pandas。它可以parse a dictionary

In [0]: import pandas

In [1]: from pandas.io.json import json_normalize

In [2]: d = {'duration': 202.0,
   ...:  'session_info':
   ...:     {'activation_uuid': 'ab90d941-df9d-42c5-af81-069eb4f71515',
   ...:      'launch_uuid': '11101c41-2d79-42cc-bf6d-37be46802fc8'},
   ...:  'timestamp': '2019-01-18T11:11:26.135Z',
   ...:  'source_page_view_reference':
   ...:     {'page_uuid': '1bede017-7b77-461d-82ef-a6bbcfdae4d7',
   ...:      'page_id': '/group/More',
   ...:      'page_name': 'More',
   ...:      'view_uuid': '9580f3c5-1116-432a-83bc-9d0b5337f661',
   ...:      'page_type': 'Native'},
   ...:  'analytics_sdk':
   ...:     {'component_id': 'datasdk',
   ...:      'component_version': '1.0.52'},
   ...:  'treatment_id': 'mockTreat',
   ...:  'client_event_id': '2b3cd878-6932-410b-b1ad-bc40ae888fdc',
   ...:  'campaign_id': 'mockCamp'}

In [4]: json_normalize(d)
Out[4]:
  analytics_sdk.component_id analytics_sdk.component_version campaign_id                       client_event_id  duration  ... source_page_view_reference.page_type  source_page_view_reference.page_uuid  source_page_view_reference.view_uuid                 timestamp treatment_id
0                    datasdk                          1.0.52    mockCamp  2b3cd878-6932-410b-b1ad-bc40ae888fdc     202.0  ...                               Native  1bede017-7b77-461d-82ef-a6bbcfdae4d7  9580f3c5-1116-432a-83bc-9d0b5337f661  2019-01-18T11:11:26.135Z    mockTreat

[1 rows x 14 columns]

要将JSON字符串加载到字典中,请使用json.loads

或使用pandas.read_json

答案 1 :(得分:1)

您还可以按照以下方式进行操作,这与熊猫在内部所做的操作类似。

import xml.etree.ElementTree as et

doc = """
your doc
"""
root = et.fromstring(doc)
result = []

for shire in root:
    for location in shire:
        location_id = location.attrib.get('id')
        for reference in location:
            list_of_attribs = [reference.attrib.get(x) for x in filter_reference]
            result.append((location_id, list_of_attribs))

print(result) # [('5178566', '978-1-891830-75-4', '110 Per¢'), ('5178566', '978-1-60309-2395', 'American Elf 1999'), ('5178566', '978-1-891830-37-2', 'The Barefoot Serpent (softcover)'), ('5178566', '978-1-891830-56-3', 'Bighead'), ('5178566', '978-1-891830-19-8', 'Box Office Poison'), ('5178568', '978-1-891830-37-2', 'The Barefoot Serpent (softcover)'), ('5178568', '978-1-936561-69-8', 'Chester 5000 (Book 2)'), ('5178568', '978-1-891830-81-5', 'Cry Yourself to Sleep'), ('5178568', '978-1-891830-75-4', '110 Per¢'), ('5178568', '978-1-891830-77-8', 'Every Girl is the End of the World for Me'), ('5178568', '978-0-9585783-4-9', 'From Hell')]