How can I sort a JSON array by a key inside of it?

Question:

I have an unknown number of items and item categories in a json array like so:

[
    {
        "x_name": "Some Name",
        "x_desc": "Some Description",
        "id": 1,
        "category": "Email"
    },
    {
        "x_name": "Another name here",
        "x_desc": "Another description",
        "id": 2,
        "category": "Email"
    },
    {
        "x_name": "Random Name",
        "x_desc": "Random Description",
        "id": 3,
        "category": "Email"
    },
    {
        "x_name": "Owner Meetings",
        "x_desc": "Total count",
        "id": 167,
        "category": "Owner Specific"
    },
    {
        "x_name": "Owner Tasks",
        "x_desc": "Total count of tasks",
        "id": 168,
        "category": "Owner Specific"
    },
    {
        "x_name": "Owner Calls",
        "x_desc": "Total count of calls",
        "id": 169,
        "category": "Owner Specific"
    },
    {
        "x_name": "Overall Total Views",
        "x_desc": "The total views",
        "id": 15,
        "category": "Totals Report"
    }
    ......
]

I need to group these JSONObjects based on the property "category".

I’ve seen similar examples in JS using the reduce function but couldn’t get a similar python solution. How can I efficiently do this in Python?

The desired outcome would be:

{
    "category": "Email",
    "points": [
        {
            "x_name": "Some Name",
            "x_desc": "Some Description",
            "id": 1,
            "category": "Email"
        },
        {
            "x_name": "Another name here",
            "x_desc": "Another description",
            "id": 2,
            "category": "Email"
        },
        {
            "x_name": "Random Name",
            "x_desc": "Random Description",
            "id": 3,
            "category": "Email"
        }
    ]
}

and then:

{
    "category": "Owner Specific",
    "points": [
        {
            "x_name": "Owner Meetings",
            "x_desc": "Total count",
            "id": 167,
            "category": "Owner Specific"
        },
        {
            "x_name": "Owner Tasks",
            "x_desc": "Total count of tasks",
            "id": 168,
            "category": "Owner Specific"
        },
        {
            "x_name": "Owner Calls",
            "x_desc": "Total count of calls",
            "id": 169,
            "category": "Owner Specific"
        }
    ]
}

and so on.

I do not know the value of the key "category" or the number of "categories" in the original JSON array.

Asked By: nt95

||

Answers:

Here is a small script I made for that.

Script

def sort_by_category():
    categories = {}
    output = []

    a = [
        {
            "x_name": "Some Name",
            "x_desc": "Some Description",
            "id": 1,
            "category": "Email",
        },
        ...
    ]

    for i in a:
        if i["category"] in categories:
            categories[i["category"]].append(i)
        else:
            categories[i["category"]] = [i]

    for c in categories:
        o = {"category": c, "points": categories[c]}
        output.append(o)
    return(output)

It browses your a array and creates another array based on categories, then it formats the output as you asked.

Output

[
    {
        "category":"Email",
        "points":[
            {
                "x_name":"Some Name",
                "x_desc":"Some Description",
                "id":1,
                "category":"Email"
            },
            {
                "x_name":"Another name here",
                "x_desc":"Another description",
                "id":2,
                "category":"Email"
            },
            {
                "x_name":"Random Name",
                "x_desc":"Random Description",
                "id":3,
                "category":"Email"
            }
        ]
    },
    {
        "category":"Owner Specific",
        "points":[
            {
                "x_name":"Owner Meetings",
                "x_desc":"Total count",
                "id":167,
                "category":"Owner Specific"
            },
            {
                "x_name":"Owner Tasks",
                "x_desc":"Total count of tasks",
                "id":168,
                "category":"Owner Specific"
            },
            {
                "x_name":"Owner Calls",
                "x_desc":"Total count of calls",
                "id":169,
                "category":"Owner Specific"
            }
        ]
    },
    {
        "category":"Totals Report",
        "points":[
            {
                "x_name":"Overall Total Views",
                "x_desc":"The total views",
                "id":15,
                "category":"Totals Report"
            }
        ]
    }
]
Answered By: The Coding Penguin
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.