如何在Python中将复杂的JSON文件结构转换为CSV?

时间:2018-03-06 15:55:18

标签: python json pandas powerbi

这就是JSON结构的样子。我从数据中取出了个人信息。

{
    "Version": "1.0",
    "IsUpToDate": false,
    "LastModificationGUID": "{numbers}",
    "DisplayName": "DATE",
    "CreateDate": "DATE AND TIME",
    "ServerID": 0,
    "CreatorServerID": 0,
    "TournamentID": "NUMBERS",
    "PitchMoundDistance": NUMBER,
    "SensorModel": 20,
    "Results": [
        {
            "Guid": "{NUMBERS}",
            "PitchServerID": 0,
            "HitServerID": 0,
            "PitchResult": {
                "LaunchSpeed": LONG FLOATING NUMBER,
                "LaunchAzimDeg": LONG FLOATING NUMBER,
                "LaunchElevDeg": LONG FLOATING NUMBER,
                "BackspinRPM": -NUMBER,
                "SidespinRPM": -NUMBER,
                "RiflespinRPM": -NUMBER,
                "X": [
                    -LONG FLOATING NUMBER,
                    LONG FLOATING NUMBER,
                    -LONG FLOATING NUMBER,
                    LONG FLOATING NUMBER,
                    -LONG FLOATING NUMBER
                ],
                "Y": [
                    LONG FLOATING NUMBER,
                    -LONG FLOATING NUMBER,
                    LONG FLOATING NUMBER,
                    -LONG FLOATING NUMBER,
                    -LONG FLOATING NUMBER
                ],
                "Z": [
                    LONG FLOATING NUMBER,
                    LONG FLOATING NUMBER,
                    -LONG FLOATING NUMBER,
                    -LONG FLOATING NUMBER,
                    LONG FLOATING NUMBER
                ],
                "FlightTime": LONG FLOATING NUMBER,
                "TrackTime": LONG FLOATING NUMBER,
                "StartPositionX": -LONG FLOATING NUMBER,
                "StartPositionY": LONG FLOATING NUMBER,
                "StartPositionZ": LONG FLOATING NUMBER,
                "StrikeZoneFrontPositionX": LONG FLOATING NUMBER,
                "StrikeZoneFrontPositionY": LONG FLOATING NUMBER,
                "StrikeZoneFrontPositionZ": LONG FLOATING NUMBER,
                "StrikeZoneFrontTime": LONG FLOATING NUMBER,
                "StrikeZoneBackPositionX": LONG FLOATING NUMBER,
                "StrikeZoneBackPositionY": LONG FLOATING NUMBER,
                "StrikeZoneBackPositionZ": LONG FLOATING NUMBER,
                "StrikeZoneBackTime": LONG FLOATING NUMBER,
                "BreakDistance": LONG FLOATING NUMBER,
                "MovementDistance": LONG FLOATING NUMBER,
                "Extension": LONG FLOATING NUMBER,
                "MLBSpinAxisDeg": LONG FLOATING NUMBER,
                "MLBSpinTiltHoursOnly": 12,
                "MLBSpinTiltHours": 12,
                "MLBSpinTiltMinutes": 15,
                "MLBHBreak": LONG FLOATING NUMBER,
                "MLBVBreak": -LONG FLOATING NUMBER,
                "MLBVBreakInduced": LONG FLOATING NUMBER,
                "MLBHMovement": -LONG FLOATING NUMBER,
                "MLBVMovement": LONG FLOATING NUMBER,
                "SimpleHBreak": LONG FLOATING NUMBER,
                "SimpleVBreak": LONG FLOATING NUMBER,
                "SimpleHMovement": -LONG FLOATING NUMBER,
                "SimpleVMovement": LONG FLOATING NUMBER,
                "PFX_X": LONG FLOATING NUMBER,
                "PFX_Z": LONG FLOATING NUMBER,
                "PX": LONG FLOATING NUMBER,
                "PZ": LONG FLOATING NUMBER,
                "X0": -LONG FLOATING NUMBER,
                "Y0": LONG FLOATING NUMBER,
                "Z0": LONG FLOATING NUMBER,
                "VX0": LONG FLOATING NUMBER,
                "VY0": -LONG FLOATING NUMBER,
                "VZ0": LONG FLOATING NUMBER,
                "AX": LONG FLOATING NUMBER,
                "AY": LONG FLOATING NUMBER,
                "AZ": -LONG FLOATING NUMBER,
                "ApproachSpeed": LONG FLOATING NUMBER,
                "ApproachAzimDeg": LONG FLOATING NUMBER,
                "ApproachElevDeg": -LONG FLOATING NUMBER
            },
            "Batter": "",
            "TriggerTime": "DATE WITH TIME",
            "PitchTypeShortCode": "FC",
            "PitchMechanicsShortCode": "ST",
            "PitchResultShortCode": "B",
            "PitchTypeDescription": "Cutter",
            "PitchMechanicsDescription": "Stretch",
            "PitchResultDescription": "Ball",
            "StrikeZoneTop": LONG FLOATING NUMBER,
            "StrikeZoneBottom": 0.5,
            "PitchStrikeZonePosition": 9,
            "PitcherName": "NAME",
            "PitcherID": "{NUMBERS AND LETTERS}",
            "PitcherIDType": "",
            "PitcherTeam": "NAME",
            "BatterName": "NAME",
            "BatterID": "{NUMBERS AND LETTERS}",
            "BatterIDType": "",
            "BatterTeam": "NAME",
            "PitchHandedness": "R",
            "SwingHandedness": "R",
            "HomeTeamBatting": false,
            "PAOfInning": 0,
            "PitchOfPA": 0
        },
        {

结构在逗号后继续这样。结构中最重要的部分是Result属性。我试过在Python中展平文件,但它无法转换为数据框。我想将其转换为CSV,以便我可以在PowerBI中进行可视化。谢谢你的帮助!

3 个答案:

答案 0 :(得分:0)

JSON文件和csv文件用于不同的目的。如果您的数据是嵌套的,并且您需要访问其中的特定图层 - JSON / dictionary将是最佳选择。如果您正在寻找DataFrame类型的数据结构 - 请使用csv。 我建议你阅读更多关于:)

编辑:在您编写的数据中,我会使用JSON

答案 1 :(得分:0)

转换JSON非常简单。一旦将其加载到Python中,数据就变成了字典。这些可以很容易地操纵,例如:

for key, value in my_dict.items():
    print('The key is : '.format(key)
    print('The value is : '.format(value)

等。我建议阅读Python词典,这应该会让它变得更容易。

答案 2 :(得分:0)

这是一个简单的函数,可以打印任意深度的JSON文件:

my_dict = {'a': {'b' : 'c', 'd': {'e': 'f'}}, 'g': 'h'}

def flatten_dict(some_dict, indent):
    for key, value in some_dict.items():
        print('    ' * indent + 'Key : {}'.format(key))
        if type(value) == dict:
            indent += 1
            flatten_dict(value, indent)
        else:
            print('    ' * indent + 'Value : {}'.format(value))


flatten_dict(my_dict, 0)