Deni 'Z Off
Deni 'Z Off

Reputation: 11

How to remove an array from a json object with python?

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.

Upvotes: 0

Views: 1557

Answers (1)

iScripters
iScripters

Reputation: 421

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))

Upvotes: 2

Related Questions