skip to Main Content

I’ve been trying to plot points on a map over a timeframe using the folium library so I created a .json file from a .txt filled with my time and coordinate values. But, all of the numerical values within that .json file are now double-quoted. Looking like this:

[
    {
        "lat": "37.79185",
        "lon": "-122.4159",
        "aoe": "1695149258521.0"
    },
    {
        "lat": "37.79185",
        "lon": "-122.4159",
        "aoe": "1695149260911.0"
    }
]

I would like them to look like this:

[
    {
        "lat": 37.79185,
        "lon": -122.4159,
        "aoe": 1695149258521.0
    },
    {
        "lat": 37.79185,
        "lon": -122.4159,
        "aoe": 1695149260911.0
    }
]

I created the .json from the .txt by slightly changing this code I found online:

filename = 'Lat_Lon_Time.txt'
dict1 = []
fields =['lat','lon','aoe']
 
with open(filename) as fh:

    for line in fh:

        description = list( line.strip().split(None, 4))

        i = 0
        dict2 = {}
        while i<len(fields):

                dict2[fields[i]]= description[i]
                i = i + 1
               
        dict1.append(dict2)
    
out_file = open("test2.json", "w")
json.dump(dict1, out_file, indent = 4)
out_file.close()

I’ve tried changing this bit of code up to strip the quotes directly but I can’t figure out how to do that.
I’ve also tried taking out the quotes after the fact with regex but I’m not too certain how to do that either.
While I was searching for solutions I found these two questions that I think are similar to mine – but I am very new to Python and I don’t understand how to implement the solutions in my particular case.

regex: remove double quotes from Json values
How to remove quotes around numbers to a JSON stringify object?

Is it possible to either change the initial code to create the .json without double quotes around the numerical values right off the bat or to use a regex to take them out once the file is already made?

2

Answers


  1. Problem is when you’re reading them to begin with you’re reading them as strings, rather than floats. This code should deal with that.

    import json
    
    filename = 'Lat_Lon_Time.txt'
    dict1 = []
    fields = ['lat', 'lon', 'aoe']
    
    with open(filename) as fh:
        for line in fh:
            description = list(line.strip().split())
    
            dict2 = {}
            for i, field in enumerate(fields):
                if field in ['lat', 'lon']:
                    dict2[field] = float(description[i])
                else:  # If 'aoe' is the only other field...?
                    dict2[field] = float(description[i])
    
            dict1.append(dict2)
    
    with open("test2.json", "w") as out_file:
        json.dump(dict1, out_file, indent=4)
    
    Login or Signup to reply.
  2. From what I observed in your code. Since all the field values are numeric strings, and you want them to be float numbers. All you need to do is cast the values as floats before assigning them to their keys in dict2. You’ll find the updated code below

    filename = 'Lat_Lon_Time.txt'
    dict1 = []
    fields =['lat','lon','aoe']
     
    with open(filename) as fh:
    
        for line in fh:
    
            description = list( line.strip().split(None, 4))
    
            i = 0
            dict2 = {}
            while i<len(fields):
    
                    dict2[fields[i]]= float(description[i])
                    i = i + 1
                   
            dict1.append(dict2)
        
    out_file = open("test2.json", "w")
    json.dump(dict1, out_file, indent = 4)
    out_file.close()
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search