1

I can't seem to be able to figure out how to extra all but the list inside this json object. The error I keep getting is ValueError: too many values to unpack (expected 2).

data = { k:v for (k, v) in organization_types_json if k != organization_types_json['organization_type_names']}

Json:

{
    "is_active": 0,
    "is_delete": 1,
    "organization_type_names": [{
        "lang": "EN",
        "name": "Fire"
    }, {
        "lang": "FR",
        "name": "Feu"
    }]
}
Joshua Dalley
  • 339
  • 3
  • 6
  • 23

1 Answers1

2

You need to add .items() like so:

data = { k:v for (k, v) in organization_types_json.items() if k != 'organization_type_names'}

Or extract all and remove that item.

d = dict(organization_types_json)
del d['organization_type_names']
Bharel
  • 23,672
  • 5
  • 40
  • 80
  • which method do you recommend? My use case is I am receiving a json string that contains data for 3 tables. The nested part represent a child table. – Joshua Dalley Mar 24 '16 at 16:32
  • @Joshua Well, either method will work. I believe deleting it afterwards is nicer and more efficient computation-wise. The other method is more efficient memory wise but doesn't look as good I believe. – Bharel Mar 24 '16 at 16:35