简体   繁体   中英

Using Pandas to convert csv to Json

I want to convert a CSV to a JSON format using pandas. I am a tester and want to send some events to Event Hub for that I want to maintain a CSV file and update my records/data using the CSV file. I created a CSV file by reading a JSON using pandas for reference. Now when I am again converting the CSV into JSON using pandas< the data is not getting displayed in the correct format. Can you please help.

Step 1: Converted JSON to CSV using pandas:

df = pd.read_json('C://Users//DAMALI//Desktop/test.json')
df.to_csv('C://Users//DAMALI//Desktop/test.csv')

Step2: Now if I try to convert the JSON again to CSV, it's not getting converted in the same format as earlier:

df = pd.read_csv('C://Users//DAMALI//Desktop/test.csv')
df.to_json('C://Users//DAMALI//Desktop/test1.json')

Providing JSON below:

{
    "body": {
        "deviceId": "UDM",
        "registrationDate": "12/11/2019",
        "testRegistration": false,
        "serialNumber": "25",
        "articleNumber": "R91",
        "deviceName": "UDM-test",
        "locationId": "lc0",
        "sapSoldToId": "1138474",
        "crmDomainAccountId": "1234566",
        "crmAccountDetails": {
            "accountName": "ProjectX",
            "accountId": "Instal",
            "region": "AP"
        },
        "productLine": "UD",
        "state": "registered",
        "installerName": "ABC Rooms",
        "installationAddress": {
            "street": "Benelu",
            "zipCode": "850",
            "city": "Kortr",
            "state": "OVL",
            "country": "Belgi"
        },
        "customerDetails": {
            "name": "John D",
            "contactName": "John Doe",
            "phone": "+32 999999999",
            "email": "john.doe@test.com"
        },
        "wallConnect": {
            "wallSize": "Width 5 x Height 4",
            "wallOrientation": "LANDSCAPE",
            "displayType": "BVD-D55M21H321A1C300",
            "softwareVersion": "1.13.1.1.3"
        },
        "projector": {
            "name": "UDX 40K-123456789",
            "subType": "UDX 40K"
        },
        "featureLicense": ["UDX-aa00213a-5719-440e-a3b5", "UDX-aa00a-571"],
        "cloudServiceLicense": ["EN04d5-4d2a-9131-875ad37c5883", "E15-4d2a-9131-875ad37c5154"],
        "metadata": {
            "cusQuesAns": [{
                    "ques": "End ucal industry",
                    "ans": "Hosity",
                    "key": "CUST_ANSWER"
                },
                {
                    "ques": "End user video wall application",
                    "ans": "Simulation & Virtual Reality",
                    "key": "CUSSECOND_ANSWER"
                }
            ]
        },
        "frequency": "realtime",
        "subDevices": [{
            "deviceType": "DISPLAY",
            "serialNumber": "68960",
            "articleNumber": "R792",
            "wallConnect": {
                "displayFMWVersion": "3.0.0",
                "displayVariant": "KVD21H331A1C300"
            }
        }]
    },
    "properties": {
        "drs": {
            "type": "salesforce-lm"
        }
    },
    "systemProperties": {
        "user-id": "data-cvice",
        "message-id": "1b1012cc-9b18c192"
    }
}

Try this for converting CSV to JSON

    import pandas as pd
    df = pd.read_csv (r'Fayzan-Bhatti\test.csv')
    df.to_json (r'Fayzan-Bhatti\new_test.json')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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