0

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.

4
  • @martineau Any Idea on this. Hope this would be a cakewalk for you... Commented Dec 27, 2019 at 7:54
  • 1
    You should include some of the work you tried. Commented Dec 27, 2019 at 7:55
  • 3
    As stated above, you must post your attempts on solving the problem even if it is not working. A good start point for you to read is Python's json module. Please, check "How to create a Minimal, Complete, and Verifiable example" and "How to ask". You will get better results by following the tips on those articles. Commented Dec 27, 2019 at 8:04
  • looks to me as a combination solution of dict and string operations some lookup procedure. Commented Dec 27, 2019 at 8:13

2 Answers 2

2

Your question is unclear but if I got it right, here is what you are looking for:

import json

json_text = '''
[
  {
    "_id": "211123",
    "_metadata": {
      "version": {
        "document": "CUS",
        "service": "1"
      },
      "rider": [
        {
          "code": "01"
        }
      ]
    }
  },
  {
    "_id": "211123",
    "_metadata": {
      "version": {
        "document": "POL",
        "service": "1"
      },
      "rider": [
        {
          "code": "02"
        }
      ]
    }
  }
]
'''

documents = {
    'CUS': 'Customer',
    'POL': 'Policy'
}

riders = {
    '01': 'RIDER01',
    '02': 'RIDER02'
} 

json_dict = json.loads(json_text)

for _id in json_dict:
    document = _id['_metadata']['version']['document']

    if document in documents:
        _id['_metadata']['version']['document'] = documents[document]

    for i, rider in enumerate(_id['_metadata']['rider']):
        code = rider['code']
        if code in riders:
             rider['code'] = riders[code]

json_text = json.dumps(json_dict)

If your JSON text is in a file called file.json, you can use the following code instead:

import json
from pathlib import Path


documents = {
    'CUS': 'Customer',
    'POL': 'Policy'
}

riders = {
    '01': 'RIDER01',
    '02': 'RIDER02'
} 

json_file = Path('file.json')

json_dict = json.loads(json_file.read_text())

for _id in json_dict:
    document = _id['_metadata']['version']['document']

    if document in documents:
        _id['_metadata']['version']['document'] = documents[document]

    for i, rider in enumerate(_id['_metadata']['rider']):
        code = rider['code']
        if code in riders:
             rider['code'] = riders[code]

json_file.write_text(json.dumps(json_dict, indent=4))

I hope it helps.

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

Comments

1

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)

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.