0

I have a program that takes some file and transforms it into a json format.

Im trying to get all the values of certain keys into a list but, because the format of the json file has a bunch of keys that are present multiple times, I cant find a way to do it properly.

My json file looks like this

{
    "data": {
        "__schema": {
            "queryType": {
                "fields": [
                    {
                        "description": "",
                        "name": "project"
                    },
                    {
                        "description": "",
                        "name": "projectEventFeed"
                    },
                    {
                        "description": "",
                        "name": "projectEventFeedFetchMore"
                    },
                    {
                        "description": "",
                        "name": "projectRecentEventFeed"
                    },
                    {
                        "description": "",
                        "name": "unseenProjectActivityCount"
                    },
                    {
                        "description": "",
                        "name": "projectFiles"
                    },
                    {
                        "description": "",
                        "name": "projectFilesIdSet"
                    },
                    {
                        "description": "",
                        "name": "projectFileMessages"
                    },
                    {
                        "description": "",
                        "name": "projectUserStatus"
                    },
                    {
                        "description": "",
                        "name": "projectFileScribble"
                    },
                    {
                        "description": "",
                        "name": "user"
                    },
                    {
                        "description": "",
                        "name": "viewer"
                    },
                    {
                        "description": "",
                        "name": "profile"
                    },
                    {
                        "description": "",
                        "name": "site"
                    },
                    {
                        "description": "",
                        "name": "designers"
                    },
                    {
                        "description": "",
                        "name": "predictImageCategory"
                    },
                    {
                        "description": "",
                        "name": "getPortfolioDesign"
                    }
                ]
            }
        }
    }
}

My goal is to get all the name values into a list.

Before turning the file into json, I tried getting that with regex but failed. With json format I tried the following

map(lambda parsed_json: parsed_json['data']['__schema']['queryType']['fields']['name'], List)

Im getting List from typing

But when i want to turn the map into a list, I get

TypeError: Parameters to generic types must be types. Got 0.

From the conversion.

1
  • Im getting List from typing. Im doing from typing import List Commented Oct 2, 2019 at 17:32

1 Answer 1

1

You could just use list comprehension on the nested 'fields' key in the dict you have converted from your json.

d = {"data": {"__schema": {"queryType": {"fields": [{"description": "", "name": "project"}, {"description": "", "name": "projectEventFeed"}, {"description": "", "name": "projectEventFeedFetchMore"}, {"description": "", "name": "projectRecentEventFeed"}, {"description": "", "name": "unseenProjectActivityCount"}, {"description": "", "name": "projectFiles"}, {"description": "", "name": "projectFilesIdSet"}, {"description": "", "name": "projectFileMessages"}, {"description": "", "name": "projectUserStatus"}, {"description": "", "name": "projectFileScribble"}, {"description": "", "name": "user"}, {"description": "", "name": "viewer"}, {"description": "", "name": "profile"}, {"description": "", "name": "site"}, {"description": "", "name": "designers"}, {"description": "", "name": "predictImageCategory"}, {"description": "", "name": "getPortfolioDesign"}]}}}}

fields = [f['name'] for f in d['data']['__schema']['queryType']['fields']]
print(fields)
# ['project', 'projectEventFeed', 'projectEventFeedFetchMore', 'projectRecentEventFeed', 'unseenProjectActivityCount', 'projectFiles', 'projectFilesIdSet', 'projectFileMessages', 'projectUserStatus', 'projectFileScribble', 'user', 'viewer', 'profile', 'site', 'designers', 'predictImageCategory', 'getPortfolioDesign']
Sign up to request clarification or add additional context in comments.

1 Comment

That worked perfectly! Thank you! I was trying to do something similar but failed at it

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.