Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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'}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading