Skip to content
Advertisement

How to select a value from a JSON dictionary and assign the new selected value as a Key value?

This is the JSON data I have. I want to assign ‘organization’ as the new key and when I call ‘organization’ rest of the field data should appear based on the organization number.

{
    "objects": {
      "record": [
        {
          "organization": 3,
          "code": 34,
          "name": "Luku' Dean",
          "address_1": "",
          "address_2": "",
          "city": "",
          "postcode": "",
          "state": "",
          "country": "",
          "vat_number": "",
          "telephone_number": "",
          "fax_number": "",
          "currency": "EUR",
          "start_date": "2001-01-01",
          "end_date": "2999-12-31",
          "status": "ACTIVE"
        },
{
          "organization": 4,
          "code": 45,
          "name": "Mr Adr",
          "address_1": "",
          "address_2": "",
          "city": "",
          "postcode": "",
          "state": "",
          "country": "",
          "vat_number": "",
          "telephone_number": "",
          "fax_number": "",
          "currency": "EUR",
          "start_date": "2001-01-01",
          "end_date": "2999-12-31",
          "status": "ACTIVE"
        },
        {
          "organization": 5,
          "code": 67,
          "name": "MR. K.J.Abhinand",
          "address_1": "",
          "address_2": "",
          "city": "",
          "postcode": "",
          "state": "",
          "country": "",
          "vat_number": "",
          "telephone_number": "",
          "fax_number": "",
          "currency": "EUR",
          "start_date": "2001-01-01",
          "end_date": "2999-12-31",
          "status": "ACTIVE"
        },
}

I want to load these data into postgresql database in the form of json object in each field. Is that possible.

Advertisement

Answer

IIC you want a new dictionary with organization values as keys:

data = {
    "objects": {
      "record": [
        {
          "organization": 3,
          "code": 34,
          "name": "Luku' Dean",
          "address_1": "",
          "address_2": "",
          "city": "",
          "postcode": "",
          "state": "",
          "country": "",
          "vat_number": "",
          "telephone_number": "",
          "fax_number": "",
          "currency": "EUR",
          "start_date": "2001-01-01",
          "end_date": "2999-12-31",
          "status": "ACTIVE"
        },
{
          "organization": 4,
          "code": 45,
          "name": "Mr Adr",
          "address_1": "",
          "address_2": "",
          "city": "",
          "postcode": "",
          "state": "",
          "country": "",
          "vat_number": "",
          "telephone_number": "",
          "fax_number": "",
          "currency": "EUR",
          "start_date": "2001-01-01",
          "end_date": "2999-12-31",
          "status": "ACTIVE"
        },
        {
          "organization": 5,
          "code": 67,
          "name": "MR. K.J.Abhinand",
          "address_1": "",
          "address_2": "",
          "city": "",
          "postcode": "",
          "state": "",
          "country": "",
          "vat_number": "",
          "telephone_number": "",
          "fax_number": "",
          "currency": "EUR",
          "start_date": "2001-01-01",
          "end_date": "2999-12-31",
          "status": "ACTIVE"
        }
      ]
    }
}

new_dict = {
    x["organization"]: {k:v for k,v in x.items() if k!= "organization"}
    for x in data["objects"]["record"]
}

print(new_dict[5])

Output:

{'code': 67, 'name': 'MR. K.J.Abhinand', 'address_1': '', 'address_2': '', 'city': '', 'postcode': '', 'state': '', 'country': '', 'vat_number': '', 'telephone_number': '', 'fax_number': '', 'currency': 'EUR', 'start_date': '2001-01-01', 'end_date': '2999-12-31', 'status': 'ACTIVE'}
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement