简体   繁体   中英

Convert deeply nested json from facebook to dataframe in python

I am trying to get user details of persons who has put likes, comments on Facebook posts. I am using python facebook-sdk package. Code is as follows.

import facebook as fi
import json

graph = fi.GraphAPI('Access Token')
data = json.dumps(graph.get_object('DSIfootcandy/posts'))

From the above, I am getting a highly nested json. Here I will put only a json string for one post in the fb.

{
  "paging": {
    "next": "https://graph.facebook.com/v2.0/425073257683630/posts?access_token=&limit=25&until=1449201121&__paging_token=enc_AdD0DL6sN3aDZCwfYY25rJLW9IZBZCLM1QfX0venal6rpjUNvAWZBOoxTjbOYZAaFiBImzMqiv149HPH5FBJFo0nSVOPqUy78S0YvwZDZD",
    "previous": "https://graph.facebook.com/v2.0/425073257683630/posts?since=1450843741&access_token=&limit=25&__paging_token=enc_AdCYobFJpcNavx6STzfPFyFe6eQQxRhkObwl2EdulwL7mjbnIETve7sJZCPMwVm7lu7yZA5FoY5Q4sprlQezF4AlGfZCWALClAZDZD&__previous=1"
  },
  "data": [
    {
      "picture": "https://fbcdn-photos-e-a.akamaihd.net/hphotos-ak-xfa1/v/t1.0-0/p130x130/1285_5066979392443_n.png?oh=b37a42ee58654f08af5abbd4f52b1ace&oe=570898E7&__gda__=1461440649_aa94b9ec60f22004675c4a527e8893f",
      "is_hidden": false,
      "likes": {
        "paging": {
          "cursors": {
            "after": "MTU3NzQxODMzNTg0NDcwNQ==",
            "before": "MTU5Mzc1MjA3NDE4ODgwMA=="
          }
        },
        "data": [
          {
            "id": "1593752074188800",
            "name": "Maduri Priyadarshani"
          },
          {
            "id": "427605680763414",
            "name": "Darshi Mashika"
          },
          {
            "id": "599793563453832",
            "name": "Shakeer Nimeshani Shashikala"
          },
          {
            "id": "1577418335844705",
            "name": "Däzlling Jalali Muishu"
          }
        ]
      },
      "from": {
        "category": "Retail and Consumer Merchandise",
        "name": "Footcandy",
        "category_list": [
          {
            "id": "2239",
            "name": "Retail and Consumer Merchandise"
          }
        ],
        "id": "425073257683630"
      },
      "name": "Timeline Photos",
      "privacy": {
        "allow": "",
        "deny": "",
        "friends": "",
        "description": "",
        "value": ""
      },
      "is_expired": false,
      "comments": {
        "paging": {
          "cursors": {
            "after": "WTI5dGJXVnVkRjlqZFhKemIzSUVXdNVFExTURRd09qRTBOVEE0TkRRNE5EVT0=",
            "before": "WTI5dGJXVnVkRjlqZFhKemIzNE16Y3dNVFExTVRFNE9qRTBOVEE0TkRRME5UVT0="
          }
        },
        "data": [
          {
            "from": {
              "name": "NiFû Shafrà",
              "id": "1025030640553"
            },
            "like_count": 0,
            "can_remove": false,
            "created_time": "2015-12-23T04:20:55+0000",
            "message": "wow lovely one",
            "id": "50018692683829_500458145118",
            "user_likes": false
          },
          {
            "from": {
              "name": "Shamnaz Lukmanjee",
              "id": "160625809961884"
            },
            "like_count": 0,
            "can_remove": false,
            "created_time": "2015-12-23T04:27:25+0000",
            "message": "Nice",
            "id": "500186926838929_500450145040",
            "user_likes": false
          }
        ]
      },
      "actions": [
        {
          "link": "https://www.facebook.com/425073257683630/posts/5001866838929",
          "name": "Comment"
        },
        {
          "link": "https://www.facebook.com/42507683630/posts/500186926838929",
          "name": "Like"
        }
      ],
      "updated_time": "2015-12-23T04:27:25+0000",
      "link": "https://www.facebook.com/DSIFootcandy/photos/a.438926536298302.1073741827.4250732576630/50086926838929/?type=3",
      "object_id": "50018692838929",
      "shares": {
        "count": 3
      },
      "created_time": "2015-12-23T04:09:01+0000",
      "message": "Reach new heights in the cute and extremely comfortable \"Silviar\" www.focandy.lk",
      "type": "photo",
      "id": "425077683630_50018926838929",
      "status_type": "added_photos",
      "icon": "https://www.facebook.com/images/icons/photo1.gif"
    }
  ]
}

Now I need to get this data into a dataframe as follows(no need to get all).

item | Like_id |Like_username | comments_userid |comments_username|comment(msg)|
-----+---------+--------------+-----------------+-----------------+------------+
Bag  |   45546 |  noel        | 641             | James           | nice work  |
-----+---------+--------------+-----------------+-----------------+------------+

Any Help will be Highly Appreciated.

Not exactly like your intended format, but here is the making of a solution :

import pandas

DictionaryObject_as_List = str(mydict).replace("{","").replace("}","").replace("[","").replace("]","").split(",")

newlist = []

for row in DictionaryObject_as_List :
    row = row.replace('https://',' ').split(":")
    exec('newlist.append ( ' + "[" + " , ".join(row)+"]" + ')')

DataFrame_Object = pandas.DataFrame(newlist)

print DataFrame_Object

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM