简体   繁体   English

在Python中使用JSON构建表

[英]Build table from JSON in Python

I am trying to transform a JSON text into a standard data table using Python, however I have little experience with this and as I search for solutions online I find I am having difficulty implementing any. 我正在尝试使用Python将JSON文本转换为标准数据表,但是我对此没什么经验,当我在线搜索解决方案时,我发现我很难实现任何。

I was trying to use ast.literal_eval but kept getting an error that I have been unable to solve. 我试图使用ast.literal_eval但一直遇到一个我无法解决的错误。

raise ValueError('malformed node or string: ' + repr(node)) 引发ValueError('格式错误的节点或字符串:'+ repr(节点))

JSON: 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"
}

Desired Table Format (values trimmed to fit for display purposes): 期望的表格格式(修剪的值适合显示目的):

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

Any direct help, or simply good resources to learn up on this would be greatly appreciated. 任何直接的帮助,或者只是很好的资源来学习这一点将不胜感激。 I've had trouble finding items that speak directly to what I am looking to do to create a table from a series of similar JSONs. 我很难找到与我想要做的事情直接对话的项目来创建一系列类似JSON的表格。

pandas is almost always used when interacting with tables. 在与表交互时几乎总是使用pandas And it can 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]

To load JSON string into a dictionary, use json.loads 要将JSON字符串加载到字典中,请使用json.loads

Or use pandas.read_json 或者使用pandas.read_json

You can also do it in the following way, which is something similar to what pandas do internally. 您也可以通过以下方式执行此操作,这与pandas内部执行的操作类似。

import json

jsondata='''{
    "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"
}'''

data=json.loads(jsondata)

table=[[],[]]
def dictList(d, column_name=''):
    for k, v in d.items():
        if isinstance(v, dict):
            dictList(v, column_name=k)
            continue
        if column_name:
            column_name+='.'
        column_name +=k
        table[0].append(column_name)
        table[1].append(v)

dictList(data)

for row in table:
    print (row)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM