redress
redress

Reputation: 1449

Picking data objects out of json arrays in 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"
                    }
                ]
            }
        ]

Upvotes: 3

Views: 134

Answers (2)

Aaron
Aaron

Reputation: 2393

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'}]

Upvotes: 1

Malik Brahimi
Malik Brahimi

Reputation: 16721

It looks like a list, try:

all_commits = all_data[0]['commits']

Upvotes: 4

Related Questions