0

I want to use arcpy to download a JSON file form "NoiseTube.net" and convert the file to a feature class. However, i'm finding it difficult to complete this task, since the json file does not contain "geometry" and "type" keys. I want to be assisted to complete my task. Attached, is my script

import arcpy
import os
from urllib import request
import json
import tempfile

url = "http://www.noisetube.net/users/11463/tracks/88383.json"
temp_dir = tempfile.mkdtemp()
filename = os.path.join(temp_dir, "Noise.json")
response = request.urlretrieve(url, filename)
json_file = open(filename)
json_raw = json.load(json_file)

arcpy.management.CreateFileGDB(r"Z:\ARCGIS", "Noisetube.gdb")
arcpy.env.workspace = r"Z:\ARCGIS\Noisetube.gdb"

Example of the JSON:

{"dataset":[{"lat":null,"lng":null,"db":"40.136","date":"2019-12-08T20:19:02Z","id":64386058},{"lat":5.1178671771186535,"lng":-1.2908399999892466,"db":"37.85","date":"2019-12-08T20:19:03Z","id":64386059},{"lat":5.1178671771186535,"lng":-1.2908399999892466,"db":"36.986","date":"2019-12-08T20:19:04Z","id":64386060},{"lat":5.1178671771186535,"lng":-1.2908399999892466,"db":"37.483","date":"2019-12-08T20:19:05Z","id":64386061}]}
2
  • If this JSON has no spatial information in it then there does not appear to be a GIS component to your question. If it does, then please describe what it is. – PolyGeo Jul 3 '20 at 18:47
  • This JSON has a lat/long. But it’s not geojson or esrijson, this it’ll need to be manually parsed. There is no out of the box parser that will handle this. – KHibma Jul 3 '20 at 19:36
2

Here is a script that reads in the json file and outputs a csv that can be imported into ArcMap. This is something that you will need to do frequently when dealing with json data as many sources won't be in geojson or esrijson.

import json
import csv

#read in json data
with open("data.json","r") as f:
    data = json.load(f)

#open output file
with open("noisedata.csv","w",newline="") as f:
    wtr = csv.writer(f,quoting=csv.QUOTE_NONNUMERIC)
    #build csv header row
    headers = data['dataset'][0].keys()
    wtr.writerow(headers)

    #iterate through items in json file
    for curr_item in data['dataset']:

        #check that all data elements are valid
        if curr_item['lat'] and curr_item['lng'] and curr_item['db'] and curr_item['date'] and curr_item['id']:
            #convert inputs
            curr_row = [float(curr_item['lat']),float(curr_item['lng']),float(curr_item['db']),
                curr_item['date'],curr_item['id']]
            #write current row to output file
            wtr.writerow(curr_row)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.