I have a json and this file continues on until a much higher id value. Notice the "id" at the very end. I have id=1 and id=2 in this sample. I would like to get a hold of them and get json.dumps to work.

{  
   "type":"FeatureCollection",
   "features":[  
      {  
     "geometry":{  
        "type":"MultiLineString",
        "coordinates":[  
           [  
              [  
                 -74.00315442793773,
                 40.72766404544987
              ],
              [  
                 -74.0028172664358,
                 40.728363128134724
              ]
           ],
           [  
              [  
                 -74.0021489154904,
                 40.72803536727916
              ],
              [  
                 -74.00269315136833,
                 40.72831405338023
              ],
              [  
                 -74.0028172664358,
                 40.728363128134724
              ]
           ],
           [  
              [  
                 -74.0028172664358,
                 40.728363128134724
              ],
              [  
                 -74.00531369282497,
                 40.72860449338741
              ]
           ],
           [  
              [  
                 -74.0028172664358,
                 40.728363128134724
              ],
              [  
                 -74.00218010455211,
                 40.729716581196314
              ]
           ]
        ]
     },
     "type":"Feature",
     "properties":{  
        "Category":"Intersection Safety Improvements",
        "Shape_Length":1713.1547151760014,
        "MID_Y":204684.89628821754,
        "OBJECTID_1":1,
        "Name":"6th and Houston",
        "OBJECTID":1,
        "Year":2015.0,
        "MID_X":983107.3932405015,
        "Presentation_Location":"NA",
        "END_X":983645.7682569176,
        "START_X":983375.7005555928,
        "START_Y":204384.20742309093,
        "Shape_Leng":1713.15463977,
        "END_Y":205132.0018592626,
        "Borough":"M",
        "Presentation":"2015_m_6thavehouston_gallery.pdf"
     },
     "id":1
  },
  {  
     "geometry":{  
        "type":"LineString",
        "coordinates":[  
           [  
              -73.99677175771464,
              40.725431942931955
           ],
           [  
              -73.99584577607527,
              40.72520835286401
           ],
           [  
              -73.99532907980338,
              40.72508373862415
           ]
        ]
     },
     "type":"Feature",
     "properties":{  
        "Category":"Intersection Safety Improvements",
        "Shape_Length":419.51660817139646,
        "MID_Y":203507.53694658508,
        "OBJECTID_1":2,
        "Name":"Houston and Crosby",
        "OBJECTID":2,
        "Year":2015.0,
        "MID_X":985344.7207623449,
        "Presentation_Location":"NA",
        "END_X":985544.669314757,
        "START_X":985144.7882247567,
        "START_Y":203570.98398391902,
        "Shape_Leng":419.516589712,
        "END_Y":203444.1404055953,
        "Borough":"M",
        "Presentation":"2015_m_houstoncrosby_gallery.pdf"
     },
     "id":2
  },

I am able to pull from the id. However when i attempt to print my json.dumps i get "none". I want to dump "id". what is going wrong with the code here?

import json

def main():
input = raw_input('JSON Input file path')
p = 0
with open(input, 'r') as jf:
    data = json.loads(jf.read())
    for item in data["features"]:
        p += 1
        item["id"] = p
        item
with open(input, 'w') as jf:
    json.dumps(data, jf)

0 Answers