skip to Main Content

Good Day,

I have very little knowledge of python scripting. I am trying to get a script to help me to convert a json file to csv. I have tried a number of scripts but keep on running into errors with not enough knowledge to fix the error.

I just need someone to help me with a script that can take the below to a csv. The source json have few thousand records in it.

Can someone please help?

{
    "meterList": [
        {
            "installID": 344,
            "meterKeyRef": "06B1008368_42615",`
            "meterCode": "06B1008368",
            "meterTypeCode": "5020ELS",
            "meterTypeName": "5 Dial 20mm Elster",
            "tagCodeFriendly": "307890",
            "rateCode": "RuralPipe",
            "activatedTimestamp": "2015-12-10 11:00:00 +10:00",
            "deactivatedTimestamp": null,
            "runNumber": "888608",
            "runSequenceNumber": 125,
            "meterStatusCode": "Active",`your text`
            "meterHazard1": null,
            "meterHazard2": null,
            "meterNotes": null,
            "locationNotes": "METER IS 06B1008368",
            "serialNumber": null,
            "meterLatitude": -36.6763,
            "meterLongitude": 142.243,
            "meterDigits": 0,
            "meterInletMaterial": null,
            "meterInletSize": "20",
            "waterOffTimestamp": null,
            "waterOnTimestamp": null,
            "initialMeterRead": 0,
            "finalMeterRead": null,
            "meterLocationCode": null,
            "propertyKeyRef": "108275",
            "masterFlag": false,
            "deductiveFlag": false,
            "slaveFlag": false,
            "sourceCode": "DLT",
            "referenceSystemSyncTimestamp": null,
            "networkPrivateFlag": false,
            "networkMeterFlag": false,
            "schemeCode": null,
            "clientLocationTitle": null,
            "clientLocationPosition": null,
            "excludeFromPathwaySync": false,
            "ignoreLeaksBelow": 0,
            "propertyID": 6325,
            "modelID": 4,
            "compoundMeterInstallID": null,
            "tradeWasteMeterFlag": false,
            "tradeWasteMeterType": null,
            "complexBillingFlag": false,
            "complexBillingComments": null,
            "fireMeterFlag": false,`your text`
            "meterPositionCode": null,
            "asseticAssetID": null,
            "connectionID": null,
            "allocationPercentage": 100
        },

I have tried a number of pre created scripts but run into errors of all sorts. I just don’t have enough knowledge to troubleshoot them properly.

Can someone please help with a script based on the info from my json file.

Appreciate any help.

2

Answers


  1. Here is Basic script to convert json to csv.

    you can dynamically change data in you code.

     import json
     import csv
    
     data = {
     "meterList": 
             [
                {
                    "installID": 344,
                    "meterKeyRef": "06B1008368_42615",
                    "meterCode": "06B1008368",
                    "meterTypeCode": "5020ELS",
                    "meterTypeName": "5 Dial 20mm Elster",
                    "tagCodeFriendly": "307890",
                    "rateCode": "RuralPipe",
                    "activatedTimestamp": "2015-12-10 11:00:00 +10:00",
                    "deactivatedTimestamp": None,
                    "runNumber": "888608",
                    "runSequenceNumber": 125,
                    "meterStatusCode": "Active",
                    "meterHazard1": None,
                    "meterHazard2": None,
                    "meterNotes": None,
                    "locationNotes": "METER IS 06B1008368",
                    "serialNumber": None,
                    "meterLatitude": -36.6763,
                    "meterLongitude": 142.243,
                    "meterDigits": 0,
                    "meterInletMaterial": None,
                    "meterInletSize": "20",
                    "waterOffTimestamp": None,
                    "waterOnTimestamp": None,
                    "initialMeterRead": 0,
                    "finalMeterRead": None,
                    "meterLocationCode": None,
                    "propertyKeyRef": "108275",
                    "masterFlag": False,
                    "deductiveFlag": False,
                    "slaveFlag": False,
                    "sourceCode": "DLT",
                    "referenceSystemSyncTimestamp": None,
                    "networkPrivateFlag": False,
                    "networkMeterFlag": False,
                    "schemeCode": None,
                    "clientLocationTitle": None,
                    "clientLocationPosition": None,
                    "excludeFromPathwaySync": False,
                    "ignoreLeaksBelow": 0,
                    "propertyID": 6325,
                    "modelID": 4,
                    "compoundMeterInstallID": None,
                    "tradeWasteMeterFlag": False,
                    "tradeWasteMeterType": None,
                    "complexBillingFlag": False,
                    "complexBillingComments": None,
                    "fireMeterFlag": False,
                    "meterPositionCode": None,
                    "asseticAssetID": None,
                    "connectionID": None,
                    "allocationPercentage": 100
                }
             ]
            }
    
    
        csv_file = "meter_data.csv"
    
        with open(csv_file, mode='w', newline='') as file:
            writer = csv.DictWriter(file, fieldnames=data['meterList'][0].keys())
            writer.writeheader()
            writer.writerows(data['meterList'])
    
        print(f"JSON data has been converted to {csv_file}")
        
    
    Login or Signup to reply.
  2. You can use the following code:
    In pandas library, you have a function to read json: read_json()
    df= pd.read_json();

    convert that into csv

    df.to_csv()

    https://www.digitalocean.com/community/tutorials/pandas-to_csv-convert-dataframe-to-csv

    or you can use the below code:

    import pandas as pd
    
    with open('file.json', encoding='utf-8') as input_file:
          df = pd.read_json(input_file)
    
    df.to_csv('file.csv', encoding='utf-8', index=False)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search