Python Forum

Full Version: geojson to json --missing multiple row output
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have a geojson file zz.geojson and want to convert it to json and csv. I was trying the below for json conversion, it converts but I only get 1 row output. Can you please tell me what I am missing and also how do I convert to csv with multi output as well.
--it is not allowing me to attach the geojson file, so putting it as zz.txt
import json
with open('H:\pytho\zz.geojson','r') as f:
d = json.load(f)

for part in d['features']:
output = {"type":'FeatureCollection',
"features": [{
"type": 'Feature',
"properties": part['properties'],
"geometry": {
"type": 'Point',
"coordinates": part['geometry']['coordinates']
}
}]
}

with open('c:\pytho\zz.json','w') as f_out:
json.dump(output, f_out, ensure_ascii=False, indent=4)
print(json.dumps(output))
I see the attached geojson has multiple lines as source but the target json you are getting is only 1 row , instead of multiple. Row counts source to target does not match. Is that correct? Very strange though , your code looks correct
I would take thought Pandas first with use json_normalize(),then use df.to_csv(index=False).
import pandas as pd
from pprint import pprint
import json

with open('zz.json') as f:
    data = json.loads(f.read())

#df = pd.read_json('attachment.json', orient='index')
df = pd.json_normalize(data, record_path=['features'])
pprint(df)

html_out = df.to_html(index=False)
       type  ...                       geometry.coordinates
0   Feature  ...   [-168.93819999971066, 53.75961000002098]
1   Feature  ...  [-156.47280000002033, 20.898610000380238]
2   Feature  ...  [-146.34639999974192, 61.124730000130796]
3   Feature  ...   [-164.53890000013152, 67.71917999998502]
4   Feature  ...  [-117.17840000026474, 32.708209999763774]
5   Feature  ...   [-122.21401199961349, 37.50536000026958]
.... ect
So it want dispaly all,could fix this but i also save to Html.
There is also Foundation import that you don't see so then the table look better.
Html output of zz.json Features

print(df.to_csv(index=False))
Output:
type,properties.OBJECTID_1,properties.OBJECTID,properties.ID,properties.PORT,properties.PORT_NAME,properties.GRAND_TOTA,properties.FOREIGN_TO,properties.IMPORTS,properties.EXPORTS,properties.DOMESTIC,geometry.type,geometry.coordinates Feature,1,1,124,C4947,"Unalaska Island, AK",1652281,1236829,426251,810578,415452,Point,"[-168.93819999971066, 53.75961000002098]" Feature,2,2,85,C4410,"Kahului, Maui, HI",3615449,20391,20391,0,3595058,Point,"[-156.47280000002033, 20.898610000380238]" Feature,10,10,27,C4816,"Valdez, AK",25807750,249648,0,249648,25558102,Point,"[-146.34639999974192, 61.124730000130796]" Feature,11,11,130,C4978,"Kivilina, AK",1359589,3367,3367,0,1356222,Point,"[-164.53890000013152, 67.71917999998502]" .... ect
Thank you snippsat. Really appreciate your reply.

I dont have panda module. Also my attached is in geojson as source input. MY code works just to output one row of the source not all the rows. Can you provide some insight please.
Yes Lola. "Row counts source to target does not match." That is correct. I am also not sure what i am missing, waiting for some python experts to provide insight.
(Mar-06-2022, 04:21 PM)yoshi Wrote: [ -> ]I dont have panda module.
pip install pandas it only task a minute.
(Mar-06-2022, 04:21 PM)yoshi Wrote: [ -> ]Also my attached is in geojson as source input.
You should see bye my output that i use this file,here in HTML before i convert to .cvs as you see last in post.
(Mar-06-2022, 04:21 PM)yoshi Wrote: [ -> ]MY code works just to output one row of the source not all the rows. Can you provide some insight please.
I have done that and solved it.
I am not gone to the job that json_normalize() has done manually.
thanks again.

I am having issues installing pandas. it is not letting me. Is it possible for you to provide anything w/o using pandas please. I apologize but having ssl cert issue and it is not letting me . if you can show a different direction pls . I honestly appreciate your help.

Yoshi
(Mar-06-2022, 06:42 PM)yoshi Wrote: [ -> ]I apologize but having ssl cert issue and it is not letting me
I guess that you have this on all pip install?
Try:
pip install --trusted-host pypi.org --trusted-host pypi.python.org --trusted-host files.pythonhosted.org pip setuptools
Also GeoJSON is a specify format that many Python packages deal with,have you looked at this?
A quick search.
GeoPandas | PyGeoj | geojson
import geopandas as gpd

earth= gpd.read_file('zz.geojson')
print(earth.head()) 
thank you again.

i tried this
pip install --trusted-host pypi.org --trusted-host pypi.python.org --trusted-host files.pythonhosted.org pip setuptools
saying :requirement already satisfied .

then tried pip install pandas again. No luck.

also geopandas i tried but it doesnot recognize this module as well. I am sorry.
If you just want to convert GeoJSON to csv can use GeoJSON - JSON Formatter
If want to continue use Python you most fix pip,there is no way to use Python without it.

Quote:also geopandas i tried but it doesnot recognize this module as well. I am sorry.
GeoPandas link to conda install first,that for usgage with Anaconda | Miniconda.
There is own section for pip install

Miniconda is fine if like to get new version of Python where both conda and pip works.