0

I have a variable prev_data of type dict which contains a value as shown

{
  "values": [
    {
      "color_code": {
        "bg_color": "#FFD9E1", 
        "border": null, 
        "label": "#000000"
      }, 
      "description": "Description Lorem epsum....", 
      "display_text": "display_text 1", 
      "icon_url": "http://www.icon.com", 
      "key": "key_text 1", 
      "label": "label_text 1"
    }, 
    {
      "color_code": {
        "bg_color": "#D4FFFA", 
        "border": null, 
        "label": "#000000"
      }, 
      "description": "Desc Lorem epsum....", 
      "display_text": "display_text 2", 
      "icon_url": "http://www.icon.com", 
      "key": "key_text 2", 
      "label": "label_text 2"
    }
  ]
}

I am trying to reduce the above to this

"values": [
  {
    "key": "key_text 1",
    "label": "label_tex 1",
    "description": "Description Lorem epsum...."
  },
  {
    "key": "key_text 2",
    "label": "label_tex 2",
    "description": "Desc Lorem epsum...."
  }
]

So far tried but no luck

* print(item.get("key") for item in prev_data["values"]) # <generator object function.<locals>.<genexpr> at 0x055C3D48>
* drug_safety_advices_extra = {k: v for k, v in prev_data.items() if k.startswith("key")} # for key, label, description 
            

1 Answer 1

1

try this why not create an empty dict add data and then append it to list named values. inside the loop and for final json add it into another dict. like this,

import json

jobj = """{
  "values": [
    {
      "color_code": {
        "bg_color": "#FFD9E1", 
        "border": null, 
        "label": "#000000"
      }, 
      "description": "Description Lorem epsum....", 
      "display_text": "display_text 1", 
      "icon_url": "http://www.icon.com", 
      "key": "key_text 1", 
      "label": "label_text 1"
    }, 
    {
      "color_code": {
        "bg_color": "#D4FFFA", 
        "border": null, 
        "label": "#000000"
      }, 
      "description": "Desc Lorem epsum....", 
      "display_text": "display_text 2", 
      "icon_url": "http://www.icon.com", 
      "key": "key_text 2", 
      "label": "label_text 2"
    }
  ]
}"""

j = json.loads(jobj)
jarr = j['values']
values = []
for i in jarr:
    tmp = {}
    tmp['key'] = i['key']
    tmp['label'] = i['label']
    tmp['description'] = i['description']
    values.append(tmp)

final_json = {}
final_json['values'] = values
print(final_json)
Sign up to request clarification or add additional context in comments.

4 Comments

Actually prev_data is of type dict not string, Sorry it's my bad. I will mention in question
you can try this approach using dict. replace null with None.
I can't replace it, It is dynamically loaded data
Your answer is right, But it won't work for mine situation. I found solution, Thanks for your reply

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.