-3

HI guys I basically want to parse this json data.

[
    {
        "id": 417862,
        "name": "octokit.rb",
        "full_name": "octokit/octokit.rb",
        "owner": {
            "login": "octokit",
            "id": 3430433,
            "avatar_url": "https://avatars0.githubusercontent.com/u/3430433?v=4",
            "gravatar_id": "",
            "url": "https://api.github.com/users/octokit",
            "html_url": "https://github.com/octokit",
            "followers_url": "https://api.github.com/users/octokit/followers",
            "following_url": "https://api.github.com/users/octokit/following{/other_user}",
            "gists_url": "https://api.github.com/users/octokit/gists{/gist_id}",
            "starred_url": "https://api.github.com/users/octokit/starred{/owner}{/repo}",
            "subscriptions_url": 
        } ]

My question is how do I get the value of "id": 417862 from json

Thanks.

duhaime
  • 25,611
  • 17
  • 169
  • 224
prime
  • 47
  • 1
  • 7

2 Answers2

0

How about:

mylist = [{
  "id": 417862,
  "name": "octokit.rb",
  "full_name": "octokit/octokit.rb",
  "owner": {
    "login": "octokit",
    "id": 3430433,
    "avatar_url": "https://avatars0.githubusercontent.com/u/3430433?v=4",
    "gravatar_id": "",
    "url": "https://api.github.com/users/octokit",
    "html_url": "https://github.com/octokit",
    "followers_url": "https://api.github.com/users/octokit/followers",
    "following_url": "https://api.github.com/users/octokit/following{/other_user}",
    "gists_url": "https://api.github.com/users/octokit/gists{/gist_id}",
    "starred_url": "https://api.github.com/users/octokit/starred{/owner}{/repo}",
    "subscriptions_url": "cats"
  }
}]

print mylist[0]['id']

This returns prints the id attribute of the 0th (i.e. first) member of mylist, which equals 417862.

duhaime
  • 25,611
  • 17
  • 169
  • 224
0

Let's assume you are using pandas:

create a function and return the 1st value of list,

def get_json_data(mylist):
    return mylist[0]

Create a new column and apply the function:

df['new_col']=df.apply(lambda df: get_json_data(df['old_col']),axis=1)

Once you get the data, just normalize it:

pd.io.json.json_normalize(df['new_col'])

Hope this helps :)

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Mantej Singh
  • 392
  • 4
  • 8