Q:

How to replace specific parameter in JSON file with KeyValue in python?

In my JSON file how do I replace specific parameter value with key value pair combination?
In the below JSON I want to replace document and code values with by referring dict json sample.
JSON file:
[
  {
    "_id": "211123",
    "_metadata": {
      "version": {
        "document": "CUS",
        "service": "1"
      },
      "rider": [
        {
          "code": "01"
        }
      ]
    }
  },
  {
    "_id": "211123",
    "_metadata": {
      "version": {
        "document": "POL",
        "service": "1"
      },
      "rider": [
        {
          "code": "02"
        }
      ]
    }
  }
]

Referall JSON:
document: 
{
  "_metadata.version.document.CUS" : "Customer",
  "_metadata.version.document.POL" : "Policy"
}

rider:
{
  "rider.code.01" : "RIDER01",
  "rider.code.02" : "RIDER02"
} 

Example:
In the first JSON record, document has CUS value and it should be replaced with Customer.
If code has 01 as value it should be replaced with RIDER01.

A:

This will be helpful
import json

document = {"_metadata.version.document.CUS" : "Customer","_metadata.version.document.POL" : "Policy" }
jsons = {"rider.code.01" : "RIDER01","rider.code.02" : "RIDER02" }

with open('jsonfile.json','r') as f:
    json_input = json.load(f)

dlist = [x.split('.') for x in document.keys()]
jlist = [['_metadata']+k for k in [x.split('.') for x in jsons.keys()]]

for js in json_input:
    for d in dlist:
        if js['_metadata']['version']['document'] == d[-1]:
            js['_metadata']['version']['document']= document['.'.join(d)]
            break
    for j in jlist:
        if js['_metadata']['rider'][0]['code'] == j[-1]:
            js['_metadata']['rider'][0]['code'] = jsons['.'.join(j[1:])]
            break

with open('output_json.json','w') as f:
    json.dump(json_input, f)

