I'm trying to sort the JSON dict below.
I want to sort the list-of-dict per dict values of both consumers
and providers
. consumers
and providers
are each list-of-dict in themselves. They need to be sorted alphabetically.
Here's the dict:
[{'consumers': [{'ip_list': {'href': '/orgs/1/sec_policy/draft/ip_lists/xx'}},
{'label': {'href': '/orgs/1/labels/xx'}}],
'consuming_security_principals': [],
'created_by': {'href': '/users/xx'},
'href': '/orgs/1/sec_policy/draft/rule_sets/yy/sec_rules/xx',
'ingress_services': [{'href': '/orgs/1/sec_policy/draft/services/xx'}],
'machine_auth': False,
'network_type': 'brn',
'providers': [{'label': {'href': '/orgs/1/labels/xx'}}],
'resolve_labels_as': {'consumers': ['workloads'],
'providers': ['workloads']},
'sec_connect': False,
'stateless': False,
'unscoped_consumers': True,
'update_type': 'create',
'updated_by': {'href': '/users/xx'}},
{'consumers': [{'label': {'href': '/orgs/1/labels/xx'}}],
'consuming_security_principals': [],
'created_by': {'href': '/users/xx'},
'href': '/orgs/1/sec_policy/draft/rule_sets/7647/sec_rules/xx',
'ingress_services': [{'href': '/orgs/1/sec_policy/draft/services/xx'}],
'machine_auth': False,
'network_type': 'brn',
'providers': [{'label': {'href': '/orgs/1/labels/xx'}},
{'ip_list': {'href': '/orgs/1/sec_policy/draft/ip_lists/xx'}}],
'resolve_labels_as': {'consumers': ['workloads'],
'providers': ['workloads']},
'sec_connect': False,
'stateless': False,
'unscoped_consumers': False,
'update_type': 'create',
'updated_by': {'href': '/users/xx'}}]
Tried this, but did not work:
newlist = sorted(list_to_be_sorted, key=lambda k: k['consumers'])
Output
----> 1 newlist = sorted(d, key=lambda k: k['consumers'])
TypeError: '<' not supported between instances of 'dict' and 'dict'
Need the output as below. With sorted values of consumers and providers alone. Rest of the dict can stay as is. I have tried a couple sorting methods, but it does not work on different value data types
[{'consumers': [{'label': {'href': '/orgs/1/labels/xx'}}, {'ip_list': {'href': '/orgs/1/sec_policy/draft/ip_lists/xx'}}],
'consuming_security_principals': [],
'created_by': {'href': '/users/xx'},
'href': '/orgs/1/sec_policy/draft/rule_sets/yy/sec_rules/xx',
'ingress_services': [{'href': '/orgs/1/sec_policy/draft/services/xx'}],
'machine_auth': False,
'network_type': 'brn',
'providers': [{'label': {'href': '/orgs/1/labels/xx'}}],
'resolve_labels_as': {'consumers': ['workloads'],
'providers': ['workloads']},
'sec_connect': False,
'stateless': False,
'unscoped_consumers': True,
'update_type': 'create',
'updated_by': {'href': '/users/xx'}},
{'consumers': [{'label': {'href': '/orgs/1/labels/xx'}}],
'consuming_security_principals': [],
'created_by': {'href': '/users/xx'},
'href': '/orgs/1/sec_policy/draft/rule_sets/7647/sec_rules/xx',
'ingress_services': [{'href': '/orgs/1/sec_policy/draft/services/xx'}],
'machine_auth': False,
'network_type': 'brn',
'providers': [{'ip_list': {'href': '/orgs/1/sec_policy/draft/ip_lists/xx'}}, {'label': {'href': '/orgs/1/labels/xx'}}],
'resolve_labels_as': {'consumers': ['workloads'],
'providers': ['workloads']},
'sec_connect': False,
'stateless': False,
'unscoped_consumers': False,
'update_type': 'create',
'updated_by': {'href': '/users/xx'}}]