简体   繁体   中英

Remove (Pop) key value from dict item in a nested json file

I have a nested json response from an API and trying to pop key values from nest dict, however unsuccessful.

[{
"uuid": "xxxxxx",
"sequence_id": xxxxx,
"is_recovery": null,
"parent_uuid": null,
"branding": "xxxxx",
"channel": "phone",
"ext_interaction_id": "xx",
"external_url": "xxxx",
"language": "en",
"survey_id": 4241,
"survey_name": "Feedback",
"tags": [
    "xxxxx",
    "4pm-6pm",
    "8am_-_10am",
    "agent_first_reply_made",
    "changes_to_orders__cust_not_in",
    "csat_sent",
    "cust_not_in_policy",
    "ffs_delivery",
    "ft_enquiry",
    "hpwp",
    "inbound_voicecall",
    "og_changes_to_orders__cust_not_in",
    "outbound_voicecall",
    "voicecall",
    "website"
],
"request_created_at": "2022-02-03T04:50:29.258Z",
"request_delivery_status": "Sent",
"request_sent_at": "2022-02-03T04:50:29.258Z",
"requested_via": "API",
"delivered_at": "2022-02-03T04:50:34.876Z",
"response_received_at": "2022-02-03T04:57:10.299Z",
"reward_eligible": false,
"reward_name": null,
"marketing": {
    "custom_link_eligible": false,
    "custom_link_initiated": false,
    "facebook_follow_eligible": false,
    "facebook_follow_initiated": false,
    "facebook_share_eligible": false,
    "facebook_share_initiated": false,
    "twitter_follow_eligible": false,
    "twitter_follow_initiated": false,
    "twitter_share_eligible": false,
    "twitter_share_initiated": false
},
"employee": {
    "custom_id": "xxxx",
    "email": "xxxx",
    "first_name": "xxx",
    "last_name": "."
},
"team_leader": {
    "custom_id": "xxxx",
    "full_name": "xxxx"
},
"customer": {
    "custom_id": null,
    "email": "xxxxx",
    "full_name": "xxxxx"
},
"custom_properties": {
    "Zendesk": "Essentials"
},
"answers": {
    "agent_star_rating": {
        "question_id": 13099,
        "question_text": "{% case channel %}\n{% when 'phone' %}How satisfied are you with the service you received from {{employee.first_name}} today?\n{% when 'chat' %}How satisfied are you with the service you received from {{employee.first_name}} today?\n{% when 'email' %}How satisfied are you with the service you received from {{employee.first_name}} today?\n{% endcase %}",
        "comment": null,
        "selected_options": {
            "101692": {
                "option_id": 101692,
                "option_text": "1",
                "integer_value": 1
            }
        }
    },
    "areas_of_improvement": {
        "question_id": 13100,
        "question_text": "{% case channel %}\n{% when 'phone' %}Sorry to hear about your experience. What could {{employee.first_name}} do to improve?\n{% when 'chat' %}Sorry to hear about your experience. What could {{employee.first_name}} do to improve?\n{% when 'email' %}Sorry to hear about your experience. What could {{employee.first_name}} do to improve?\n{% endcase %}",
        "comment": null,
        "selected_options": {
            "101697": {
                "option_id": 101697,
                "option_text": "Friendliness",
                "integer_value": null
            },
            "101698": {
                "option_id": 101698,
                "option_text": "Knowledge",
                "integer_value": null
            },
            "101699": {
                "option_id": 101699,
                "option_text": "Clarity",
                "integer_value": null
            },
            "101702": {
                "option_id": 101702,
                "option_text": "Professionalism",
                "integer_value": null
            },
            "102366": {
                "option_id": 102366,
                "option_text": "Responsiveness",
                "integer_value": null
            },
            "102367": {
                "option_id": 102367,
                "option_text": "Problem Solving",
                "integer_value": null
            }
        }
    }
}

}]

What I am trying to do is remove the key value "101692","101697","102366" under "selected_options" (in fact, just snip the id and retain its values). Does anyone see an easier way, or can suggest code to scrape the "101692" values from the nested dict above?

You can use the method dict.pop(key) on the nested dictionary, which will remove the entry and return the item.

key = "**101692**"
item = your_dict["answers"]["agent_star_rating"]["selected_options"].pop(key)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM