Python Forum
geojson to json --missing multiple row output
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
geojson to json --missing multiple row output
#1
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))
Larz60+ write Mar-04-2022, 06:10 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.

Please fix indentation as well. Thank you.
Reply
#2
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
Reply
#3
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
Reply
#4
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.
Reply
#5
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.
Reply
#6
(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.
Reply
#7
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
Reply
#8
(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()) 
Reply
#9
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.
Reply
#10
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Parse JSON multiple objects larkin_L 8 5,611 May-27-2020, 11:18 AM
Last Post: nuffink
  json.dumps list output qurr 12 5,076 Apr-08-2020, 10:13 PM
Last Post: micseydel
  API JSON response missing list gives keyerror rolfmadsen 3 3,402 Mar-28-2020, 10:12 AM
Last Post: buran
  print python json dump onto multiple lines lhailey 2 19,658 Mar-02-2020, 12:47 PM
Last Post: vishalhule
  json.dumps output error in python3 prayuktibid 2 2,615 Jan-21-2020, 06:41 AM
Last Post: prayuktibid
  Output to a json file problem Netcode 3 3,663 Nov-22-2019, 01:44 AM
Last Post: Skaperen
  Multiprocessing.Queue Issues (Missing/Corrupted Items/No Output) python3noob 0 3,160 Aug-03-2019, 09:38 PM
Last Post: python3noob
  Using the GeoJSON API ashlynjane 1 4,638 May-27-2019, 04:54 PM
Last Post: heiner55
  decoding sub.process output with multiple \n? searching1 2 2,748 Feb-24-2019, 12:00 AM
Last Post: searching1
  Download multiple large json files at once halcynthis 0 2,752 Feb-14-2019, 08:41 AM
Last Post: halcynthis

Forum Jump:

User Panel Messages

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