0

I have this list of json objects, and I'd like to create a method that returns all commit (or project) entries where project_id : 1, for example. Is this possible?

I currently have all_commits = all_data[0]['commits'] , which represents all of the commit objects, and a method def findAll(self):return all_commits which returns all of them, but I cant figure out how to return some of the records. I can return the value of a record via:

  def findOne(self, index, key):
    return all_commits[index][key]

But how can I return the whole object?


     [
            {
                "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"
                    }
                ]
            }
        ]
1
  • import json and var jsonString = json.dumps(all_commits) Commented Mar 6, 2015 at 18:34

1 Answer 1

1

Filter with a list comprehension (or a genex when you get there).

[commit for commit in all_commits if commit['project_id'] == '1']
Sign up to request clarification or add additional context in comments.

2 Comments

y = [commit for commit in all_commits if commit['project_id'] == 1] return y returns [ ]
@redress: For some reason your data has IDs as strings. Try the updated code.

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.