skip to Main Content

For input json,

{
  "dataSet": {
    "integerFields": {
      "patient_display_id": "1",
      "ehmrn": "123",
      "enctrid": "456"
    },
     "DecimalFields": {
      "class": "1",
      "section": "123",
      "enctrid": "456"
    }
  }
}

I want to delete integerFields but keep the value of it, an output something like this. But keep rest of the fields intact

{
  "dataSet": {
      "patient_display_id": "1",
      "ehmrn": "123",
      "enctrid": "456",
      "DecimalFields": {
      "class": "1",
      "section": "123",
      "enctrid": "456"
    }
  }
}

2

Answers


  1. If you just assign it’s value to the key containing it, it will be overwritten

    inputDict = {'entity': {'dataSet': {'integerFields': {'patient_display_id': '1'}}}}
    inputDict['entity']['dataSet'] = inputDict['entity']['dataSet']['integerFields']
    print(inputDict)
    
    Login or Signup to reply.
  2. Merge the keys of the integerFields dictionary with the dataSet dictionary, then delete the integerFields key:

    import json
    
    input_json = '''
    {
      "dataSet": {
        "integerFields": {
          "patient_display_id": "1",
          "ehmrn": "123",
          "enctrid": "456"
        },
        "DecimalFields": {
          "class": "1",
          "section": "123",
          "enctrid": "456"
        }
      }
    }'''
    data = json.loads(input_json)
    data['dataSet'].update(data['dataSet']['integerFields'])
    del data['dataSet']['integerFields']
    print(json.dumps(data, indent=2))
    

    Output:

    {
      "dataSet": {
        "DecimalFields": {
          "class": "1",
          "section": "123",
          "enctrid": "456"
        },
        "patient_display_id": "1",
        "ehmrn": "123",
        "enctrid": "456"
      }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search