3

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

2 Answers 2

4

It looks like a list, try:

all_commits = all_data[0]['commits']
Sign up to request clarification or add additional context in comments.

3 Comments

i got that to work, but i wasnt confident it would scale? if there's not a more apparent way that Im missing then ill go with it. thanks!
gotta wait 9 minutes broski
i was being sardonic and hpyerbolic, bro
1

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

2 Comments

I was assuming that the data had already been loaded as JSON.
Oh, I thought that was the content of something.json, so I use json.loads.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.