Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Picking data objects out of json arrays in Python

Tags:

python

I have this data object, and Im wondering how I can just pick the sub-object called commits (or projects). I tried all_commits = all_data['commits'] but python is forcing me to give it an integer as opposed to a string. Thoughts?

 [
            {
                "commits": [
                    {
                        "project_id": "1",
                        "commit_title": "commit 1",
                        "date": "date 1",
                        "markdown": "markdown 1"
                    },
                    {
                        "project_id": "1",
                        "commit_title": "commit 2",
                        "date": "date 2",
                        "markdown": "markdown 2"
                    },
                    {
                        "project_id": "1",
                        "commit_title": "commit 3",
                        "date": "date 3",
                        "markdown": "markdown 3"
                    },
                    {
                        "project_id": "1",
                        "commit_title": "commit 4",
                        "date": "date 4",
                        "markdown": "markdown 4"
                    },
                    {
                        "project_id": "2",
                        "commit_title": "commit 5",
                        "date": "date 5",
                        "markdown": "markdown 5"
                    },
                    {
                        "project_id": "2",
                        "commit_title": "commit 6",
                        "date": "date 6",
                        "markdown": "markdown 6"
                    }
                ]
            },
            {
                "projects": [
                    {
                        "id": 1,
                        "project_name": "GreenGlass for Groups",
                        "description": "Support group projects for retention agreements"
                    },
                    {
                        "id": 2,
                        "project_name": "Zumo Redesign",
                        "description": "New eda theme-based design"
                    }
                ]
            }
        ]
like image 264
redress Avatar asked Mar 25 '26 01:03

redress


2 Answers

It looks like a list, try:

all_commits = all_data[0]['commits']
like image 127
Malik Brahimi Avatar answered Mar 26 '26 16:03

Malik Brahimi


A JSON approach:

s = ''' 
PUT YOUR JSON array here
'''

import json
q = json.loads(s)
print q[0]['commits']

Result

[{u'date': u'date 1', u'project_id': u'1', u'commit_title': u'commit 1', u'markdown': u'markdown 1'}, {u'date': u'date 2', u'project_id': u'1', u'commit_title': u'commit 2', u'markdown': u'markdown 2'}, {u'date': u'date 3', u'project_id': u'1', u'commit_title': u'commit 3', u'markdown': u'markdown 3'}, {u'date': u'date 4', u'project_id': u'1', u'commit_title': u'commit 4', u'markdown': u'markdown 4'}, {u'date': u'date 5', u'project_id': u'2', u'commit_title': u'commit 5', u'markdown': u'markdown 5'}, {u'date': u'date 6', u'project_id': u'2', u'commit_title': u'commit 6', u'markdown': u'markdown 6'}]
like image 34
Aaron Avatar answered Mar 26 '26 16:03

Aaron