0

in the following example I am trying to replace value of one key by the value of another key; but I tried multiple ways and it doesn't seem to work.

following is my code

d = {
  "name" : "ABC",
  "type" : "Service",
  "clusterRef" : {
    "clusterName" : "ABCSTUFF"
  },
  "serviceState" : "STARTED",
  "healthChecks" : [ {
    "name" : "STORAGE",
    "summary" : "GOOD"
  }, {
    "name" : "CPU UTILIZATION",
    "summary" : "GOOD"
  } ],
  "maintenanceMode" : "false"
}


########################
## Get Key Value

def get_key_values(d, key):
  for k, v in d.items():
    if k == "name":
        k = (key + "." + v)
    else:
        k = (key + "." + k)
    if isinstance(v, dict):
        get_key_values(v, k)
    elif isinstance(v, list):
        for i in v:
            get_key_values(i, k)
    else:
        print ("{0} : {1}".format(k, v))


get_key_values(d, "TTS")

the result come up like following

TTS.serviceState : STARTED 
TTS.type : Service 
TTS.ABC : ABC
TTS.clusterRef.clusterName : ABCSTUFF
TTS.healthChecks.summary : GOOD         <<< remove this line and replace "Good" with  the value for "TTS.healthChecks.STORAGE"
TTS.healthChecks.STORAGE : STORAGE
TTS.healthChecks.summary : GOOD         <<< remove this line and replace "Good" with  the value for "TTS.healthChecks.CPU UTILIZATION"
TTS.healthChecks.CPU UTILIZATION : CPU UTILIZATION
TTS.maintenanceMode : false

but I want the result to be following

TTS.serviceState : STARTED 
TTS.type : Service 
TTS.ABC : ABC
TTS.clusterRef.clusterName : ABCSTUFF
TTS.healthChecks.STORAGE : GOOD                 <<<
TTS.healthChecks.CPU UTILIZATION : GOOD         <<<
TTS.maintenanceMode : false

Any help is much appreciated

3
  • Are you able to restructure the data, or do you have to work with the data in that exact format? Commented Aug 24, 2016 at 19:02
  • nope, can't restructure the data, as its being fetched from and external API Commented Aug 24, 2016 at 19:04
  • Have you tried using the json library and modifying the dictionary you get from loading your json? Commented Aug 24, 2016 at 19:28

1 Answer 1

1

Here's a non-generic solution which works for your question:

d = {
    "name": "ABC",
    "type": "Service",
    "clusterRef": {
        "clusterName": "ABCSTUFF"
    },
    "serviceState": "STARTED",
    "healthChecks": [{
        "name": "STORAGE",
        "summary": "GOOD"
    }, {
        "name": "CPU UTILIZATION",
        "summary": "GOOD"
    }],
    "maintenanceMode": "false"
}


########################
# Get Key Value

def get_key_values(d, key):
    for k, v in d.items():
        if k == "name":
            k = (key + "." + v)
        else:
            k = (key + "." + k)

        if isinstance(v, dict):
            get_key_values(v, k)
        elif isinstance(v, list):
            for i in v:
                tok1 = k + "." + i.get("name")
                tok2 = i.get("summary")
                print("{0} : {1}".format(tok1, tok2))
        else:
            print("{0} : {1}".format(k, v))

get_key_values(d, "TTS")
Sign up to request clarification or add additional context in comments.

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.