Python Forum
Indirectlty convert string to float in JSON file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Indirectlty convert string to float in JSON file
#1
Hi,

I need to get rid of " from a JSON file

I have a JSON file like this:

{
    "type": "FeatureCollection",
    "features": [
        {
            "type": "Feature",
            "properties": {
                "Ville": "OZAN"
            },
            "geometry": {
                "type": "Point",
                "coordinates": [
                    "4.91667, 46.3833"
                ]
            }
        },
        {
            "type": "Feature",
            "properties": {
                "Ville": "CORMORANCHE-SUR-SAONE"
            },
            "geometry": {
                "type": "Point",
                "coordinates": [
                    "4.83333, 46.2333"
                ]
            }
        },
I need to remove the " before and after the coordinates in order to get this.

{
    "type": "FeatureCollection",
    "features": [
        {
            "type": "Feature",
            "properties": {
                "Ville": "OZAN"
            },
            "geometry": {
                "type": "Point",
                "coordinates": [
                    4.91667, 46.3833
                ]
            }
        },
        {
            "type": "Feature",
            "properties": {
                "Ville": "CORMORANCHE-SUR-SAONE"
            },
            "geometry": {
                "type": "Point",
                "coordinates": [
                    4.83333, 46.2333
                ]
            }
        },
I can't do it manually because i about 35k cities.

I can't find any viable solution, because I can't get rid of all the " from my file and i can't convert coordniates into float directly because of the [] .. such as this solution : https://stackoverflow.com/questions/3667...rom-python

Do you have any idea ? thank you in advance.
Reply
#2
1. load the json into json_data
2. iterate over features in json_data['features']
3. read the feature['geometry']['coordinates'][0] - this will give you a string, e.g."4.91667, 46.3833"
4. convert coordinates by splitting at comma, then convert individual values to float
5. assign coordinates from 4 as list to feature['geometry']['coordinates']
6. dump json data back to a file
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Hi, thank you for clues, it helped me,

the code below worked:

with open(filepath, 'r+', encoding='utf-8') as data_jason:

 data = json.load(data_jason)

 
 for feature in data['features']:
     print (feature['geometry']['coordinates'][0])
     splitted = feature['geometry']['coordinates'][0].split(',')
     print(splitted)
     floated = [float(i) for i in splitted]
     print(floated)
     del feature['geometry']['coordinates']
     feature['geometry']['coordinates'] = []
     feature['geometry']['coordinates'].extend(floated)
     print(feature['geometry']['coordinates'])

 json.dump(data, data_jason,  indent=4)
Dumping write at the end of the file how could I make it overwrite it instead ?
Reply
#4
don't open it in r+ mode. first open it for reading. After you process data, open it again for writing and dump data.
There are some redundant steps in what you do, e.g. no need to delete the coordinates node first
import json

json_file = 'features.json'

with open(json_file) as f:
    json_data = json.load(f)
    for feature in json_data['features']:
        coordinates = feature['geometry']['coordinates'][0].split(',')
        feature['geometry']['coordinates'] = [float(coord.strip()) for coord in coordinates]

with open(json_file, 'w') as f:
    json.dump(json_data, f, indent=4)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
You are so fast :) :) :), I was about to edit because I found out the 2 steps "with open" way.

Thank you for optimization also, I'd rather make more steps in order to understand better what I do...

When you convert to float, what does strip() does (which I don't do in my version?)

Regards,
Reply
#6
(May-06-2020, 11:36 AM)WBPYTHON Wrote: When you convert to float, what does strip() does (which I don't do in my version?)
it strips the leading/trailing whitespace if present after split at comma,
But you are right - it's not really necessary. it can be removed
feature['geometry']['coordinates'] = [float(coord) for coord in coordinates]
or using map if you prefer
feature['geometry']['coordinates'] = list(map(float, coordinates))
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
Understood, thank you again
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Parse Nested JSON String in Python rwalde 4 2,860 Sep-08-2022, 10:32 AM
Last Post: rwalde
Sad ValueError: could not convert string to float badju 0 4,295 Jul-01-2021, 12:13 AM
Last Post: badju
  HELP! Importing json file into csv into jupyter notebook vilsef 2 2,532 Jan-22-2021, 11:06 AM
Last Post: snippsat
  JSON file Loading issue punna111 4 8,492 Jun-29-2020, 08:07 AM
Last Post: buran
  ValueError: could not convert string to float RahulSingh 3 4,118 Apr-09-2020, 02:59 PM
Last Post: dinesh
  convert a list of string+bytes into a list of strings (python 3) pacscaloupsu 4 10,745 Mar-17-2020, 07:21 AM
Last Post: markfilan
  Help batch converting .json chosen file to MySQL BrandonKastning 2 2,287 Mar-14-2020, 09:19 PM
Last Post: BrandonKastning
  Convert dataframe string column to numeric in Python darpInd 1 2,271 Mar-14-2020, 10:07 AM
Last Post: ndc85430
  convert 'A B C' to numpy float matrix rezabma 4 2,491 Feb-27-2020, 09:48 AM
Last Post: rezabma
  ValueError: could not convert string to float: '4 AVENUE' Kudzo 4 5,867 Jan-26-2020, 10:47 PM
Last Post: Kudzo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020