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

Time:10-31

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.

CodePudding user response:

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'}

CodePudding user response:

Try this in your code

new_dict = {

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

}

print(new_dict[2]) #select which field you want to print
  • Related