如何将字符串格式的JSON放入表中

时间:2019-10-10 17:11:38

标签: python r json pandas

我有以下字符串格式的JSON数据。如何使用R或Python将data转换为表格格式?

我尝试过df = pd.DataFrame(data),但这没用,因为data是一个字符串。

data = '{"Id":"048f7de7-81a4-464d-bd6d-df3be3b1e7e8","RecordType":20, "CreationTime":"2019-10-08T12:12:32","Operation":"SetScheduledRefresh", "OrganizationId":"39b03722-b836-496a-85ec-850f0957ca6b","UserType":0, "UserAgent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36", "ItemName":"ASO Daily Statistics","Schedules":{"RefreshFrequency":"Daily", "TimeZone":"E. South America Standard Time","Days":["All"], "Time":["07:30:00","10:30:00","13:30:00","16:30:00","19:30:00","22:30:00"]}, "IsSuccess":true,"ActivityId":"4e8b4514-24be-4ba5-a7d3-a69e8cb8229e"}'

所需的输出:

output = 
------------------------------------------------------------------
ID                                      | RecordType | CreationTime
048f7de7-81a4-464d-bd6d-df3be3b1e7e8    | 20         | 2019-10-08T12:12:32

错误:

ValueError                                Traceback (most recent call last)
<ipython-input-26-039b238b38ef> in <module>
----> 1 df = pd.DataFrame(data)

e:\Anaconda3\lib\site-packages\pandas\core\frame.py in __init__(self, data, index, columns, dtype, copy)
    483                 )
    484             else:
--> 485                 raise ValueError("DataFrame constructor not properly called!")
    486 
    487         NDFrame.__init__(self, mgr, fastpath=True)

ValueError: DataFrame constructor not properly called!

2 个答案:

答案 0 :(得分:0)

您将需要reticulate库:您将需要将所有true更改为True。看下面的代码

a <-  'string = {"Id":"048f7de7-81a4-464d-bd6d-df3be3b1e7e8","RecordType":20,
                 "CreationTime":"2019-10-08T12:12:32","Operation":"SetScheduledRefresh",
                 "OrganizationId":"39b03722-b836-496a-85ec-850f0957ca6b","UserType":0,
                 "UserAgent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36",
                 "ItemName":"ASO Daily Statistics","Schedules":{"RefreshFrequency":"Daily",
                 "TimeZone":"E. South America Standard Time","Days":["All"],
                 "Time":["07:30:00","10:30:00","13:30:00","16:30:00","19:30:00","22:30:00"]},
                 "IsSuccess":true,"ActivityId":"4e8b4514-24be-4ba5-a7d3-a69e8cb8229e"}'

data.frame(reticulate::py_eval(gsub('true','True',sub('.*=\\s+','',a))))

答案 1 :(得分:0)

在Python中:

import pandas as pd
from ast import literal_eval
from pandas.io.json import json_normalize

data = '{"Id":"048f7de7-81a4-464d-bd6d-df3be3b1e7e8","RecordType":20, "CreationTime":"2019-10-08T12:12:32","Operation":"SetScheduledRefresh", "OrganizationId":"39b03722-b836-496a-85ec-850f0957ca6b","UserType":0, "UserAgent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36", "ItemName":"ASO Daily Statistics","Schedules":{"RefreshFrequency":"Daily", "TimeZone":"E. South America Standard Time","Days":["All"], "Time":["07:30:00","10:30:00","13:30:00","16:30:00","19:30:00","22:30:00"]}, "IsSuccess":true,"ActivityId":"4e8b4514-24be-4ba5-a7d3-a69e8cb8229e"}'

data = data.replace('true', 'True')
data = literal_eval(data)

{'ActivityId': '4e8b4514-24be-4ba5-a7d3-a69e8cb8229e',
 'CreationTime': '2019-10-08T12:12:32',
 'Id': '048f7de7-81a4-464d-bd6d-df3be3b1e7e8',
 'IsSuccess': True,
 'ItemName': 'ASO Daily Statistics',
 'Operation': 'SetScheduledRefresh',
 'OrganizationId': '39b03722-b836-496a-85ec-850f0957ca6b',
 'RecordType': 20,
 'Schedules': {'Days': ['All'],
               'RefreshFrequency': 'Daily',
               'Time': ['07:30:00',
                        '10:30:00',
                        '13:30:00',
                        '16:30:00',
                        '19:30:00',
                        '22:30:00'],
               'TimeZone': 'E. South America Standard Time'},
 'UserAgent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 '
              '(KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36',
 'UserType': 0}

创建数据框:

df = json_normalize(data)

                                   Id  RecordType         CreationTime            Operation                        OrganizationId  UserType                                                                                                            UserAgent              ItemName  IsSuccess                            ActivityId Schedules.RefreshFrequency              Schedules.TimeZone Schedules.Days                                                Schedules.Time
 048f7de7-81a4-464d-bd6d-df3be3b1e7e8          20  2019-10-08T12:12:32  SetScheduledRefresh  39b03722-b836-496a-85ec-850f0957ca6b         0  Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36  ASO Daily Statistics       True  4e8b4514-24be-4ba5-a7d3-a69e8cb8229e                      Daily  E. South America Standard Time          [All]  [07:30:00, 10:30:00, 13:30:00, 16:30:00, 19:30:00, 22:30:00]