Python - Replace value in JSON file from second file if keys match
Question: 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](http://stackoverflow.com/questions/14882138/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 )
    


Answer: 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?](http://stackoverflow.com/questions/13543019/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.)

