2

I want to iterate through this dictionary and find any 'id' that has a leading zero, like the one below, and replace it without the zero. So 'id': '01001' would become 'id': '1001'

Here is how to get the data I'm working with:

from urllib.request import urlopen
import json
with urlopen('https://raw.githubusercontent.com/plotly/datasets/master/geojson-counties-fips.json') as response:
    counties = json.load(response)

so far I've been able to get one ID at a time, but not sure how to loop through to get all of the IDs:

My code so far: counties['features'][0]['id']

{ 'type': 'FeatureCollection',
 'features': [{'type': 'Feature',
   'properties': {'GEO_ID': '0500000US01001',
    'STATE': '01',
    'COUNTY': '001',
    'NAME': 'Autauga',
    'LSAD': 'County',
    'CENSUSAREA': 594.436},
   'geometry': {'type': 'Polygon',
    'coordinates': [[[-86.496774, 32.344437],
      [-86.717897, 32.402814],
      [-86.814912, 32.340803],
      [-86.890581, 32.502974],
      [-86.917595, 32.664169],
      [-86.71339, 32.661732],
      [-86.714219, 32.705694],
      [-86.413116, 32.707386],
      [-86.411172, 32.409937],
      [-86.496774, 32.344437]]]},
   'id': '01001'}
    ]
}
bad_coder
  • 11,289
  • 20
  • 44
  • 72
user2200270
  • 309
  • 1
  • 11

3 Answers3

4
from urllib.request import urlopen
import json
with urlopen('https://raw.githubusercontent.com/plotly/datasets/master/geojson-counties-fips.json') as response:
    counties = json.load(response)

Then iterate over the list if id's with your JSON structure. And update the id as

counties['features'][0]['id'] = counties['features'][0]['id'].lstrip("0") lstrip will remove leading zeroes from the string.

Paras Mishra
  • 336
  • 1
  • 9
1

Suppose your dictionary counties has the following data. You can use the following code:

counties={'type': 'FeatureCollection', 
     'features': [ {'type': 'Feature','properties': {'GEO_ID': '0500000US01001','STATE': '01','COUNTY': '001','NAME': 'Autauga', 'LSAD': 'County','CENSUSAREA': 594.436},
    'geometry': {'type': 'Polygon','coordinates': [[[-86.496774, 32.344437],[-86.717897, 32.402814],[-86.814912, 32.340803],
      [-86.890581, 32.502974],
      [-86.917595, 32.664169],
      [-86.71339, 32.661732],
      [-86.714219, 32.705694],
      [-86.413116, 32.707386],
      [-86.411172, 32.409937],
      [-86.496774, 32.344437] ]] } ,'id': '01001'}, {'type': 'Feature','properties': {'GEO_ID': '0500000US01001','STATE': '01','COUNTY': '001','NAME': 'Autauga', 'LSAD': 'County','CENSUSAREA': 594.436},
    'geometry': {'type': 'Polygon','coordinates': [[[-86.496774, 32.344437],[-86.717897, 32.402814],[-86.814912, 32.340803],
      [-86.890581, 32.502974],
      [-86.917595, 32.664169],
      [-86.71339, 32.661732],
      [-86.714219, 32.705694],
      [-86.413116, 32.707386],
      [-86.411172, 32.409937],
      [-86.496774, 32.344437] ]] } ,'id': '000000000001001'} ]} 

for feature in counties['features']:
    feature ['id']=feature ['id'].lstrip("0")

print(counties)    
Roohollah Etemadi
  • 1,243
  • 1
  • 6
  • 18
1

Here is shorter and faster way of doing this using json object hooks,

def stripZeroes(d):
    if 'id' in d:
        d['id'] = d['id'].lstrip('0')
        return d
    return d
with urlopen('https://raw.githubusercontent.com/plotly/datasets/master/geojson-counties-fips.json') as response:
    counties = json.load(response, object_hook=stripZeroes)
Anurag Wagh
  • 1,086
  • 6
  • 16