How To Properly Delete Data From Json With Python
Im trying to find the proper way to delete json data with python. My goal with the python script is to remove the contact that the user inputs. I want to remove their email, number
Solution 1:
You can turn the json to a dict
, manipulate the dict
, and turn it back to json.
In [244]: json_string = """{
...: "contacts": {
...: "example_contact": {
...: "email": "email@domain.com",
...: "number": "phone number"
...: }
...: }
...: }"""
In [250]: contacts = json.loads(json_string)
In [251]: del contacts['contacts']['example_contact']
In [252]: contacts
Out[252]: {'contacts': {}}
In [253]: json.dumps(contacts)
Out[253]: '{"contacts": {}}'
Post a Comment for "How To Properly Delete Data From Json With Python"