Search code examples
jsoncsvparse-platform

Converting CSV to JSON Solution


I have just developed an iOS app using parse for my back end server. I have the app complete and ready to go and have a large number of entires to add to the parse backend, however i have aculated the data not realising until now to up load a class including geo points i need to use json. my data file is structured as follows:

Country,PostCode,State,Suburb,location/__type,location/latitude,location/longitude,locname,phone,streetaddress,website
Australia,2000,NSW,Cronulla,GeoPoint,-33.935434,151.026887,Shop ABC,+61297901401,ABC Canterbury Road,http://www.123.com.au

I need to convert this format to the following

{ "results": [
    {
        "Country": "Australia",
        "PostCode": "2000",
        "State": "NSW",
        "Suburb": “Crounlla”,
        "location": {
            "__type": "GeoPoint",
            "latitude": -33.935434,
            "longitude": 151.026887
        },
        "locname": "Shop ABC”,
        "phone": "+123456”,
        "streetaddress": “ABC Canterbury Road",
        "website": "http://www.123.com.au"
    }
] }

I have several thousand entries so as you can imagine i don't want to have to do it manually. I only have access to a Mac so any suggestions will need to be Mac friendly. Previous answers I've found haven't worked because of the geographic data.


Solution

  • You can use a python script (Mac is pre-installed with python) Sample code:

    #!/usr/bin/python
    
    import csv
    import json
    
    
    header = []
    results = []
    with open('data.csv', 'rb') as f:
        reader = csv.reader(f)
        for row in reader:
            if len(header) > 0:
                location = {}
                datarow = {}
                for key, value in (zip(header,row)):
                    if key.startswith('location'):
                        location[key.split('/')[1]] = value
                    else:
                        datarow[key] = value
                datarow['location'] = location
                results.append(datarow)
            else:
                header = row
    
            print json.dumps(dict(results=results))