1

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, and name but keep the rest of my contacts.

My json looks like this:

{
    "contacts": {
        "example_contact": {
            "email": "[email protected]",
            "number": "phone number"
        }
    }
}
2
  • 1
    Whats desired output? Commented Jul 18, 2018 at 3:56
  • I would like to delete the whole contact. so the email and tag as well as the "example contact part " Commented Jul 19, 2018 at 1:43

1 Answer 1

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 protected]",
     ...:             "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": {}}'
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.