0

sample data:

[{"title": "foo", "imageData": "xyz123"}, {"title": "bar", "imageData": "abc123"}, {"title": "baz", "imageData": "def456"}]

this will delete the unwanted key in place, with del, taken from here

import json

with open('data.json') as json_data:
    data = json.load(json_data)
    for element in data: 
        del element['imageData'] 

what is the inverse of this command del element['imageData']. So i want to delete everything but the imageData key.

1
  • Do you want to write the data back to the json file? Commented Jul 19, 2016 at 2:32

2 Answers 2

1

You could just do this:

temp = data['imageData']
data.clear()
data['imageData'] = temp

The resulting dictionary named data will only contain the key imageData and its associated value

Sign up to request clarification or add additional context in comments.

Comments

1

You could do that with a list comprehension to create a new list with only the element(s) of interest:

import json

with open('data.json') as json_data:
    data = json.load(json_data)
    new_data = [{'imageData': element['imageData']} for element in data]

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.