Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I filter a .json array in python so that only one parameter in each element shows?

like the title says I’m having a problem filtering an array that I’m getting from the CoinGecko API. The array looks like this:

[
  {
    "id": "01coin",
    "symbol": "zoc",
    "name": "01coin"
  },
  {
    "id": "0-5x-long-algorand-token",
    "symbol": "algohalf",
    "name": "0.5X Long Algorand Token"
  },
  {
    "id": "0-5x-long-altcoin-index-token",
    "symbol": "althalf",
    "name": "0.5X Long Altcoin Index Token"
  }
]

After the filter I’d like it to only show the "id"s like this:

[
  "01coin",
  "0-5x-long-algorand-token",
  "0-5x-long-altcoin-index-token"
]

This is how I tried to filter it:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

coinList = 'https://api.coingecko.com/api/v3/coins/list'
listCall = requests.get(coinList)
jsonCall = json.loads(listCall.content)
coinIds = [x for x in jsonCall if x == 'id']

>Solution :

Your list comprehension is sort of there, but you should be indexing into each dictionary rather than using an if clause. It should look like:

[item["id"] for item in jsonCall]

This outputs:

['01coin', '0-5x-long-algorand-token', '0-5x-long-altcoin-index-token']
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading