Python Forum
Aggregation json by nested elements - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Aggregation json by nested elements (/thread-12647.html)



Aggregation json by nested elements - Omri - Sep-05-2018

I have the following structure:
Output:
[ { "Name": "a-1", "Tags": [ { "Value": "a", "Key": "Type" } ], "CreationDate": "2018-02-25T17:33:19.000Z" }, { "Name": "a-2", "Tags": [ { "Value": "a", "Key": "Type" } ], "CreationDate": "2018-02-26T17:33:19.000Z" }, { "Name": "b-1", "Tags": [ { "Value": "b", "Key": "Type" } ], "CreationDate": "2018-01-21T17:33:19.000Z" }, { "Name": "b-2", "Tags": [ { "Value": "b", "Key": "Type" } ], "CreationDate": "2018-01-22T17:33:19.000Z" }, { "Name": "c-1", "Tags": [ { "Value": "c", "Key": "Type" } ], "CreationDate": "2018-08-29T17:33:19.000Z" } ]
I want group the json elements by Value and to print out the oldest Name of each Value when there are more than one member in the group (This should be configurable. For example: The x oldest items when there are more than y members in the group). In this case there are two a, two b and one c, So the expected result will be:
Output:
a-1 b-1
Any idea how to do that in Python?


RE: Aggregation json by nested elements - Larz60+ - Sep-05-2018

if you extract as dictionary, you can use:
see: https://www.saltycrane.com/blog/2007/09/how-to-sort-python-dictionary-by-keys/
section: How to sort a dict by keys (Python older than 2.4):

Or to do as json: https://stackoverflow.com/questions/26924812/python-sort-list-of-json-by-value