Sorting multidimensional JSON objects in python
Question: I have an object as such and want to sort it by time (line first, point
second) in each dimension (simplified json):

    
    
    [{
        "type":"point"
    },
    {
        "type":"line",
        "children": [
            {
                "type":"point"
            },
            {
                "type":"point"
            },
            {
                "type":"line"
            }
        ]
    
    },
    {
        "type":"point"     
    }]
    

This **dimention could be deeper** and have much more points/lines within each
other.

The **sorted output** would be something like this:

    
    
    [{
        "type":"line",
        "children": [
            {
                "type":"line"
            },
            {
                "type":"point"
            },
            {
                "type":"point"
            }
        ]
    
    },
    {
        "type":"point"
    },
    {
        "type":"point"     
    }]
    

Thanks


Answer: You'd need to process this recursively:

    
    
    from operator import itemgetter
    
    def sortLinesPoints(data):
        if isinstance(data, dict):
            if 'children' in data:
                sortLinesPoints(data['children'])
        else:
            for elem in data:
                sortLinesPoints(elem)
            data.sort(key=itemgetter('type'))
    

