0

I have two JSON files that look like this

{"type": "FeatureCollection", "features": [{ "type": "Feature", "properties": { **"id"**: "Carlow", **"density"**: "0" } , "geometry": { "type": "MultiPolygon", "coordinates": [ [ [ [ -6.58901, 52.906464 ], [ -6.570265, 52.905682 ], [ -6.556207, 52.906464 ],

Second JSON file

{"features": [{"**count**": 2, "name": "**Sligo**"}, {"count": 3"name":"Fermanagh"},{"count": 1, "name": "Laois"}, 

I am trying to check if "id" in the first file matches with "name" in the second file and if so change the value for "density" to the value for "count" from the second file. I am looking at using recursion from a similar question I found here Replace value in JSON file for key which can be nested by n levels but it only checks if one key matches and changes value. I need two keys to match before changing values. This is the code I have used so far but not sure how to add two keys and two values. I use Counter to count the number of times a string appears and save it to county_names.json, which is my second JSON file. ire_countiesTmp.json is my first file that I am trying to replace the values with from the second file. Im not sure how to do this with Python as only started learning it. Any help would be great, or if you know a better way. Thanks

import json, pprint
from collections import Counter

with open('../county_names.json') as data_file:

county_list = json.load(data_file)

for i in county_list:
    c = Counter(i for i in county_list)


for county,count in c.iteritems():

    with open('ire_countiesTmp.json') as f:

            def fixup(adict, k1, v1, k2, v2):
                for key in adict.keys():
                    if adict[key] == v1:
                        adict[key] = v
                    elif type(adict[key]) is dict:
                        fixup(adict[key], k, v)


            #pprint.pprint( data )

            fixup(data, 'id', county, 'density', count)
            pprint.pprint( data )
1
  • I don't understand what you code does. What is fixup and de the json files referenced in the code have anything to do with the json in your question? You should edit this so the code is a minimal reproducible example of your problem. Commented Feb 12, 2016 at 11:28

1 Answer 1

1

Generally speaking, recursion is not a good idea in Python. The compiler/interpreter does not handle it well and it becomes terribly slow, as there is no tail recursion optimisation: Why is recursion in python so slow? .

A possible brute-force-solution that assumes you have converted your JSON-data into a dict could look like this:

def fixup_dict_a_with_b(a, b):
    for feature_a in a["features"]:
        for feature_b in b["features"]:
            if feature_a["properties"]["id"] == feature_b["name"]:
                feature_a["properties"]["density"] = feature_b["count"]
                break

This can of course be "abstractified" to your liking. ;)

Other, more elegant solutions exist, but this one is straightforward and easy to get when you just started to use Python. (Eventually, you might want to look into pandas, for example.)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.