Home:ALL Converter>How to rename key for nested JSON object in Python

How to rename key for nested JSON object in Python

Ask Time:2018-11-08T14:10:28         Author:dx123

Json Formatter

I have this JSON object response after performing Logstash aggregation.

 "aggregations": {
    "range": {
      "buckets": [
        {
          "key": "2018-01-01T00:00:00.000Z-2018-01-31T00:00:00.000Z",
          "from_as_string": "2018-01-01T00:00:00.000Z",
          "to_as_string": "2018-01-31T00:00:00.000Z",
          "doc_count": 13000,
          "by ip": {
            "doc_count_error_upper_bound": 10,
            "sum_other_doc_count": 10300,
            "buckets": [
              {
                "key": "192.168.0.1",
                "doc_count": 20 <---
                 .
                 .
                 . (Sub-buckets for other fields containing doc_count as 
                    well)
              },
              {
                "key": "1.2.3.4",
                "doc_count": 50 <---
              }
            }
          ]
        }
      }
    ]
  }
}

I would like to rename the "doc_count" key to "Number of unique events". However I only want this just for the doc_count of the IP Addresses buckets, not any other bucket fields.

This was one of the solutions that I found, but it renamed doc_count for every field buckets.

def rename_doc_count(obj):
    for key in obj.keys():
        new_key = key.replace("doc_count","Number of unique events")
        if new_key != key:
            obj[new_key] = obj[key]
            del obj[key]
    return obj

new_json = json.loads(jres, object_hook=rename_doc_count)

Anyone has a solution that can fit my situation? Thanks!

Author:dx123,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/53202332/how-to-rename-key-for-nested-json-object-in-python
yy