Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
958 views
in Technique[技术] by (71.8m points)

Python: Create csv from json/dictionary

I have the following data that needs to be transformed into a csv:

 "tfeOrganizations": [
        {
            "org": "MyOrg1",
            "org_admins": [
                "[email protected]", "[email protected]"
            ],
            "org_viewers": [
                "[email protected]","[email protected]"
            ],
            "teams": {
                "genericresources-admins": {
                    "ci_cd_token_regenerate": False,
                    "members": ["[email protected]", "[email protected]"],
                    "workspaces": [
                        [
                            "genericresources-dev",
                            "write"
                        ],
                        [
                            "genericresources-qa",
                            "write"
                        ],
                        [
                            "genericresources-prod",
                            "write"
                        ]
                    ]
                 },
                 "genericresources-contributors": {
                    "ci_cd_token_regenerate": False,
                    "members": ["[email protected]", "[email protected]", "[email protected]"],
                    "workspaces": [
                        [
                            "genericresources-dev",
                            "write"
                        ],
                        [
                            "genericresources-qa",
                            "write"
                        ],
                        [
                            "genericresources-prod",
                            "plan"
                        ]
                    ]
                }
            },
            "workspaces": [
                "genericresources-dev",
                "genericresources-qa",
                "genericresources-prod"
            ]
        }
    ]

The result should be something like this whereas the header is built dynamically based on the longest array for the members across all organisations.

organisation,workspace,org_admin1,org_admin2,org_viewer1,org_viewer2,member1,member2,member3,...

MyOrg1,genericresources-dev,[email protected],[email protected],[email protected],[email protected],firstUser@test,[email protected],...

MyOrg1,genericresources-qa,[email protected],[email protected],[email protected],[email protected],firstUser@test,[email protected],...

MyOrg1,genericresources-prod,[email protected],[email protected],[email protected],[email protected],firstUser@test,[email protected],...

Currently I do not how to tackle this problem.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

you can create with pandas

example

import pandas as pd

# access your json file
your_json = data['tfeOrganizations']


data_frame = pd.DataFrame(your_json)


pd.to_csv('result.csv',dataframe)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...