0

This is the content of my json file:

{
  "tabID": [
    {
      "dat": [1, "q"],
      "opt": []
    }
  ]
}

I'm building a python app that process that json file and I need to remove "opt":[].

I've tried next code:

data['tabID'][0].remove('data') 

But it doesn't work.

Could you give me any advice? Thanks.

2
  • 1
    You can use del statement: del data['tabID'][0]['opt']. Commented Jun 1, 2020 at 21:25
  • 1
    For things this simple, post running code so we can just copy/paste for experimenting. Your json has a bug and we have to write the surrounding code to test an answer. Commented Jun 1, 2020 at 21:30

1 Answer 1

2

Make it a dict and pop the key.

Fixed an error in your JSON. Working example is at https://repl.it/repls/FrighteningPungentEquipment or as actual code:

import json

the_json_string = '{"tabID":[{"dat":[1, "q"],"opt":[] }]}'

obj = json.loads(the_json_string)
obj['tabID'][0].pop('opt')

print(json.dumps(obj))
Sign up to request clarification or add additional context in comments.

3 Comments

The json string could be in triple quotes for readability `"""{"tabID":[{"dat":[1, "q"],"opt":[] }]}""". And just delete your first attempt since it doesn't really help people in the future.
@tdelaney, raw prefix or single quotes could be even better.
@OlvinRoght - Potentially, yes. I was thinking in terms of keeping the line breaks in the original example. Raw strings are great if you want embedded "\n" in the json too.

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.