title: Python - Replace value in JSON file from second file if keys match
body: <p>I have two JSON files that look like this</p>
  
  <pre><code>{"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 ],
  </code></pre>
  
  <p>Second JSON file</p>
  
  <pre><code>{"features": [{"**count**": 2, "name": "**Sligo**"}, {"count": 3"name":"Fermanagh"},{"count": 1, "name": "Laois"}, 
  </code></pre>
  
  <p>I am trying to check if <strong>"id"</strong> in the first file matches with <strong>"name"</strong> in the second file and if so change the value for <strong>"density"</strong> to the value for <strong>"count"</strong> from the second file. I am looking at using recursion from a similar question I found here <a href="https://stackoverflow.com/questions/14882138/replace-value-in-json-file-for-key-which-can-be-nested-by-n-levels">Replace value in JSON file for key which can be nested by n levels</a> 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</p>
  
  <pre><code>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 )
  </code></pre>