0

I have a nested list dictionary. I need to sort the list(Upper-level list) by inner dictionary when it is matching the label and by value in python. For example, I want to sort the list where ("label"=="Name" and by value of the key "value" of the inner dictionary), Here is my dictionary,

[
    [
      {
        "propertyId": 1,
        "fieldName": "Name",
        "value": "mahir contact 1",
        "objectTypeName": "Contact properties",
        "groupName": "Contact information",
        "name": "name",
        "label": "Name",
        "description": "default properties by roboket",
        "fieldType": "Single line text",
        "fieldDataType": "string",
        "propertyOwnerId": null,
        "propertyOwnerAccountId": 1,
        "default": true,
        "hasOptions": false,
        "id": 1,
        "contactCreatorId": 1,
        "contactOwnerId": 1,
        "createdAt": "2020-11-24T05:57:01.248107"
      },
      {
        "propertyId": 2,
        "fieldName": "Email",
        "value": "[email protected]",
        "objectTypeName": "Contact properties",
        "groupName": "Contact information",
        "name": "email",
        "label": "Email",
        "description": "default properties by roboket",
        "fieldType": "Single line text",
        "fieldDataType": "email",
        "propertyOwnerId": null,
        "propertyOwnerAccountId": 1,
        "default": true,
        "hasOptions": false,
        "id": 1,
        "contactCreatorId": 1,
        "contactOwnerId": 1,
        "createdAt": "2020-11-24T05:57:01.253318"
      },
      {
        "propertyId": 6,
        "fieldName": "Contact creation date",
        "value": "2020-11-24 05:57:01.216359",
        "objectTypeName": "Contact properties",
        "groupName": "Contact information",
        "name": "contactCreationDate",
        "label": "Contact creation date",
        "description": "default properties by roboket",
        "fieldType": "Date picker",
        "fieldDataType": "datetime",
        "propertyOwnerId": null,
        "propertyOwnerAccountId": 1,
        "default": false,
        "hasOptions": false,
        "id": 1,
        "contactCreatorId": 1,
        "contactOwnerId": 1,
        "createdAt": "2020-11-24T05:57:01.256631"
      },
      {
        "propertyId": 11,
        "fieldName": "Contact owner",
        "value": "1",
        "objectTypeName": "Contact properties",
        "groupName": "Contact information",
        "name": "contactOwner",
        "label": "Contact owner",
        "description": "default properties by roboket",
        "fieldType": "Dropdown select",
        "fieldDataType": "select",
        "propertyOwnerId": null,
        "propertyOwnerAccountId": 1,
        "default": true,
        "hasOptions": true,
        "id": 1,
        "contactCreatorId": 1,
        "contactOwnerId": 1,
        "createdAt": "2020-11-24T05:57:01.261312"
      }
    ],
    [
      {
        "propertyId": 3,
        "fieldName": "Phone number",
        "value": "+8801517179498",
        "objectTypeName": "Contact properties",
        "groupName": "Contact information",
        "name": "phoneNumber",
        "label": "Phone number",
        "description": "default properties by roboket",
        "fieldType": "Single line text",
        "fieldDataType": "number",
        "propertyOwnerId": null,
        "propertyOwnerAccountId": 1,
        "default": true,
        "hasOptions": false,
        "id": 2,
        "contactCreatorId": 1,
        "contactOwnerId": 1,
        "createdAt": "2020-11-24T05:58:04.115570"
      },
      {
        "propertyId": 6,
        "fieldName": "Contact creation date",
        "value": "2020-11-24 05:58:04.070069",
        "objectTypeName": "Contact properties",
        "groupName": "Contact information",
        "name": "contactCreationDate",
        "label": "Contact creation date",
        "description": "default properties by roboket",
        "fieldType": "Date picker",
        "fieldDataType": "datetime",
        "propertyOwnerId": null,
        "propertyOwnerAccountId": 1,
        "default": false,
        "hasOptions": false,
        "id": 2,
        "contactCreatorId": 1,
        "contactOwnerId": 1,
        "createdAt": "2020-11-24T05:58:04.122804"
      },
      {
        "propertyId": 11,
        "fieldName": "Contact owner",
        "value": "1",
        "objectTypeName": "Contact properties",
        "groupName": "Contact information",
        "name": "contactOwner",
        "label": "Contact owner",
        "description": "default properties by roboket",
        "fieldType": "Dropdown select",
        "fieldDataType": "select",
        "propertyOwnerId": null,
        "propertyOwnerAccountId": 1,
        "default": true,
        "hasOptions": true,
        "id": 2,
        "contactCreatorId": 1,
        "contactOwnerId": 1,
        "createdAt": "2020-11-24T05:58:04.130811"
      },
      {
        "propertyId": 1,
        "fieldName": "Name",
        "value": "mahir contact 2",
        "objectTypeName": "Contact properties",
        "groupName": "Contact information",
        "name": "name",
        "label": "Name",
        "description": "default properties by roboket",
        "fieldType": "Single line text",
        "fieldDataType": "string",
        "propertyOwnerId": null,
        "propertyOwnerAccountId": 1,
        "default": true,
        "hasOptions": false,
        "id": 2,
        "contactCreatorId": 1,
        "contactOwnerId": 1,
        "createdAt": "2020-11-24T05:58:04.111468"
      }
    ]
]

My Expected Output:

[
    [
      {
        "propertyId": 3,
        "fieldName": "Phone number",
        "value": "+8801517179498",
        "objectTypeName": "Contact properties",
        "groupName": "Contact information",
        "name": "phoneNumber",
        "label": "Phone number",
        "description": "default properties by roboket",
        "fieldType": "Single line text",
        "fieldDataType": "number",
        "propertyOwnerId": null,
        "propertyOwnerAccountId": 1,
        "default": true,
        "hasOptions": false,
        "id": 2,
        "contactCreatorId": 1,
        "contactOwnerId": 1,
        "createdAt": "2020-11-24T05:58:04.115570"
      },
      {
        "propertyId": 6,
        "fieldName": "Contact creation date",
        "value": "2020-11-24 05:58:04.070069",
        "objectTypeName": "Contact properties",
        "groupName": "Contact information",
        "name": "contactCreationDate",
        "label": "Contact creation date",
        "description": "default properties by roboket",
        "fieldType": "Date picker",
        "fieldDataType": "datetime",
        "propertyOwnerId": null,
        "propertyOwnerAccountId": 1,
        "default": false,
        "hasOptions": false,
        "id": 2,
        "contactCreatorId": 1,
        "contactOwnerId": 1,
        "createdAt": "2020-11-24T05:58:04.122804"
      },
      {
        "propertyId": 11,
        "fieldName": "Contact owner",
        "value": "1",
        "objectTypeName": "Contact properties",
        "groupName": "Contact information",
        "name": "contactOwner",
        "label": "Contact owner",
        "description": "default properties by roboket",
        "fieldType": "Dropdown select",
        "fieldDataType": "select",
        "propertyOwnerId": null,
        "propertyOwnerAccountId": 1,
        "default": true,
        "hasOptions": true,
        "id": 2,
        "contactCreatorId": 1,
        "contactOwnerId": 1,
        "createdAt": "2020-11-24T05:58:04.130811"
      },
      {
        "propertyId": 1,
        "fieldName": "Name",
        "value": "mahir contact 2",
        "objectTypeName": "Contact properties",
        "groupName": "Contact information",
        "name": "name",
        "label": "Name",
        "description": "default properties by roboket",
        "fieldType": "Single line text",
        "fieldDataType": "string",
        "propertyOwnerId": null,
        "propertyOwnerAccountId": 1,
        "default": true,
        "hasOptions": false,
        "id": 2,
        "contactCreatorId": 1,
        "contactOwnerId": 1,
        "createdAt": "2020-11-24T05:58:04.111468"
      }
    ],
    [
      {
        "propertyId": 1,
        "fieldName": "Name",
        "value": "mahir contact 1",
        "objectTypeName": "Contact properties",
        "groupName": "Contact information",
        "name": "name",
        "label": "Name",
        "description": "default properties by roboket",
        "fieldType": "Single line text",
        "fieldDataType": "string",
        "propertyOwnerId": null,
        "propertyOwnerAccountId": 1,
        "default": true,
        "hasOptions": false,
        "id": 1,
        "contactCreatorId": 1,
        "contactOwnerId": 1,
        "createdAt": "2020-11-24T05:57:01.248107"
      },
      {
        "propertyId": 2,
        "fieldName": "Email",
        "value": "[email protected]",
        "objectTypeName": "Contact properties",
        "groupName": "Contact information",
        "name": "email",
        "label": "Email",
        "description": "default properties by roboket",
        "fieldType": "Single line text",
        "fieldDataType": "email",
        "propertyOwnerId": null,
        "propertyOwnerAccountId": 1,
        "default": true,
        "hasOptions": false,
        "id": 1,
        "contactCreatorId": 1,
        "contactOwnerId": 1,
        "createdAt": "2020-11-24T05:57:01.253318"
      },
      {
        "propertyId": 6,
        "fieldName": "Contact creation date",
        "value": "2020-11-24 05:57:01.216359",
        "objectTypeName": "Contact properties",
        "groupName": "Contact information",
        "name": "contactCreationDate",
        "label": "Contact creation date",
        "description": "default properties by roboket",
        "fieldType": "Date picker",
        "fieldDataType": "datetime",
        "propertyOwnerId": null,
        "propertyOwnerAccountId": 1,
        "default": false,
        "hasOptions": false,
        "id": 1,
        "contactCreatorId": 1,
        "contactOwnerId": 1,
        "createdAt": "2020-11-24T05:57:01.256631"
      },
      {
        "propertyId": 11,
        "fieldName": "Contact owner",
        "value": "1",
        "objectTypeName": "Contact properties",
        "groupName": "Contact information",
        "name": "contactOwner",
        "label": "Contact owner",
        "description": "default properties by roboket",
        "fieldType": "Dropdown select",
        "fieldDataType": "select",
        "propertyOwnerId": null,
        "propertyOwnerAccountId": 1,
        "default": true,
        "hasOptions": true,
        "id": 1,
        "contactCreatorId": 1,
        "contactOwnerId": 1,
        "createdAt": "2020-11-24T05:57:01.261312"
      }
    ]       
]

  

Advance thanks for your help.

2
  • Can you update your question with the preferred output based on your sample? Also is there anything you've tried so far? Commented Nov 24, 2020 at 7:29
  • Yap, I tried, sorted(all_contact_data_response, key=lambda k: k["value"] and k["label"]==property_name) . But I need to go one level further. Commented Nov 24, 2020 at 7:48

4 Answers 4

1

I'd suggest the following two-step solution for your problem:

import json

with open("test.json") as json_file:
    data = json.load(json_file)


labeled_contacts = {}
for contact_props in data:
    for contact_prop in contact_props:
        if contact_prop["label"] == "Name":
            labeled_contacts[contact_prop["value"]] = contact_props


sorted_contacts = [
    labeled_contacts[label] for label in sorted(labeled_contacts.keys(), reverse=True)
]
  1. Create a dictionary in which you can store your lists as values and the name labels as keys. To populate the dictionary iterate through the list of lists and then the dictionaries within the respective list. Find the dictionary which holds the name label, and add the label and the list to the dictionary.

  2. Then you can sort the keys/labels of the dictionary and then loop through the sorted keys and retrieve your lists in a sorted order.

Sign up to request clarification or add additional context in comments.

Comments

0

If this - sorted(all_contact_data_response, key=lambda k: k["value"] and k["label"]==property_name) - works, to up level try [sorted(all_contact_data_response, key=lambda k: k["value"] and k["label"]==property_name),] However, I don't know, what do you have in variable all_contact_data_response

Comments

0

my idea is to pop out the value key from the inner dictionary and add it to the outer list as a temporary variable.

# data contains the JSON as a dictionary.
for idx1, item1 in enumerate(data):
   value = None
   for idx2, item2 in enumerate(item1):
      if item2["label"] == "Name":
        value = item2["value"]
        break
   data[idx1].append(value)

Now, once we have that value in the outer scope, we can easily use it to sort the list.

result = sorted(data, key=lambda i: i[-1])

Further down the line you can remove the extra variable.

for item in result:
   item.pop(-1)

Comments

0

Ok. if I understand the problem correctly, try this

    def foo(list_item):
    for a in list_item:
        if a['label'] == 'Name':
            return a['value']

    res = sorted(all_contact_data_response, reverse=True, key = lambda list_item: foo(list_item))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.