Rolando
Rolando

Reputation: 62674

How to efficiently search in the following json dict in python and store into list or another dict?

Assuming I have the following JSON Object:

{
  "type": "Bands",
  "Artists": [{
    "profile": {
      "name":"Marissa"
      "age": "7"
      "hobbies": [{
        "name":"Skiing",
        "experience": "1 year"
      }]
    },
    "popularsong":{
      "name":"Wishing on a Star"
      "year": 2007
    }
  },
{
    "profile": {
      "name":"Kelsey"
      "age": "7"
      "hobbies": [{
        "name":"Piano",
        "experience": "1 year"
      }]
    }
    "popularsong":{
      "name":"The Twinkle"
      "year": 2007
    }
  },...
  ]
}

Assuming this json is loaded into a dictionary with json.loads(json_string) What is the most efficient means of searching for all "Artist" entries with "age" of "7" and sticking them into an array or another dictionary?

Note that an artist entry is comprised of both the "profile" and the "popular song" something like:

{ "profile": { "name":"Marissa" "age": "7" "hobbies": [{ "name":"Skiing", "experience": "1 year" }, "popularsong":{ "name":"The Twinkle" "year": 2007 }

Upvotes: 0

Views: 252

Answers (1)

Brenden Brown
Brenden Brown

Reputation: 3235

[artist for artist in myJson['Artists'] if artist['profile']['age'] == 7] might work, if I'm mentally parsing that JSON correctly.

Upvotes: 1

Related Questions