gwydion93
gwydion93

Reputation: 1923

Merging multiple JSON objects into one JSON object

I am doing multiple requests to a REST service and getting JSON data back from each request. What I want to do is merge all of those JSON objects (hoping I am using the right terminology) into one object. How do I do that?

minCount = 1
maxCount = 1000
masterJson = {}
for x in range(36):
    params = {'f': 'json', 'where': 'OBJECTID>='+str(minCount)+'and OBJECTID<='+str(maxCount), 'geometryType': 'esriGeometryPolygon', 'spatialRel': 'esriSpatialRelIntersects','outFields': '*', 'returnGeometry': 'true'}
    r = requests.get('https://hazards.fema.gov/gis/nfhl/rest/services/CSLF/Prelim_CSLF/MapServer/3/query', params)

    cslfJson = r.json()
    masterJson.update(cslfJson)

    for item in cslfJson['features']:
        objCount = item['attributes']['OBJECTID']

        if minCount < objCount:
            break
        else:
        minCount = minCount + maxCount
        maxCount = maxCount + 1000

Essentially, its the cslfJson variables (JSON obj) that I want to combine into one cslfJson object variable.

Upvotes: 0

Views: 898

Answers (1)

Darth_Sourav
Darth_Sourav

Reputation: 116

In the memory JSON objects are treated like a dict so treat each JSON obj as a dict.

masterJson = {}
masterJson['JSON1'] = cslfJson
masterJson['JSON2'] = cslfJson

If you need to write a .json file, you can just pass the whole dict as argument to the JSON writers.

Upvotes: 1

Related Questions