Parsing JIRA webhook comments - python

I am trying to parse JIRA webhook comment section properly. Now, following code works well for the first comment:
data = request.json
jira_comment = data['issue']['fields']['comment']['comments'][0].get('body')
However, every time new webhook is fired, for example, new comment is added, I can only receive the first comment in the JSON-message. If I could somehow identify each comment from another or only parse always the last comment of the issue. What would be the best way to do it or is it even possible since JIRA webhook JSON looks broken (multiple 'body' keys).
See JSON data below, to make it cleaner, use this http://jsbeautifier.org/:
{
"timestamp": 1443024903340,
"webhookEvent": "jira:issue_updated",
"user": {
"self": "http://192.168.10.58:8080/rest/api/2/user?username=jhdoe",
"name": "jhdoe",
"key": "jhdoe",
"emailAddress": "john.doe#corp.com",
"avatarUrls": {
"48x48": "http://www.gravatar.com/avatar/f94b6b6c15d155f4d4b3b2c05509ab0a?d=mm&s=48",
"24x24": "http://www.gravatar.com/avatar/f94b6b6c15d155f4d4b3b2c05509ab0a?d=mm&s=24",
"16x16": "http://www.gravatar.com/avatar/f94b6b6c15d155f4d4b3b2c05509ab0a?d=mm&s=16",
"32x32": "http://www.gravatar.com/avatar/f94b6b6c15d155f4d4b3b2c05509ab0a?d=mm&s=32"
},
"displayName": "John Doe",
"active": true,
"timeZone": "Europe/Berlin"
},
"issue": {
"id": "10300",
"self": "http://192.168.10.58:8080/rest/api/2/issue/10300",
"key": "DEMO-6",
"fields": {
"issuetype": {
"self": "http://192.168.10.58:8080/rest/api/2/issuetype/3",
"id": "3",
"description": "A task that needs to be done.",
"iconUrl": "http://192.168.10.58:8080/images/icons/issuetypes/task.png",
"name": "Task",
"subtask": false
},
"components": [],
"timespent": null,
"timeoriginalestimate": null,
"description": "Hey",
"project": {
"self": "http://192.168.10.58:8080/rest/api/2/project/10000",
"id": "10000",
"key": "DEMO",
"name": "DEMO",
"avatarUrls": {
"48x48": "http://192.168.10.58:8080/secure/projectavatar?avatarId=10011",
"24x24": "http://192.168.10.58:8080/secure/projectavatar?size=small&avatarId=10011",
"16x16": "http://192.168.10.58:8080/secure/projectavatar?size=xsmall&avatarId=10011",
"32x32": "http://192.168.10.58:8080/secure/projectavatar?size=medium&avatarId=10011"
}
},
"fixVersions": [],
"aggregatetimespent": null,
"resolution": null,
"timetracking": {},
"customfield_10006": null,
"attachment": [],
"customfield_10009": "0|i0000f:",
"aggregatetimeestimate": null,
"resolutiondate": null,
"workratio": -1,
"summary": "Comment test",
"lastViewed": "2015-09-23T19:14:11.979+0300",
"watches": {
"self": "http://192.168.10.58:8080/rest/api/2/issue/DEMO-6/watchers",
"watchCount": 1,
"isWatching": true
},
"creator": {
"self": "http://192.168.10.58:8080/rest/api/2/user?username=jhdoe",
"name": "jhdoe",
"key": "jhdoe",
"emailAddress": "john.doe#corp.com",
"avatarUrls": {
"48x48": "http://www.gravatar.com/avatar/f94b6b6c15d155f4d4b3b2c05509ab0a?d=mm&s=48",
"24x24": "http://www.gravatar.com/avatar/f94b6b6c15d155f4d4b3b2c05509ab0a?d=mm&s=24",
"16x16": "http://www.gravatar.com/avatar/f94b6b6c15d155f4d4b3b2c05509ab0a?d=mm&s=16",
"32x32": "http://www.gravatar.com/avatar/f94b6b6c15d155f4d4b3b2c05509ab0a?d=mm&s=32"
},
"displayName": "John Doe",
"active": true,
"timeZone": "Europe/Berlin"
},
"subtasks": [],
"created": "2015-09-21T17:39:14.518+0300",
"reporter": {
"self": "http://192.168.10.58:8080/rest/api/2/user?username=jhdoe",
"name": "jhdoe",
"key": "jhdoe",
"emailAddress": "john.doe#corp.com",
"avatarUrls": {
"48x48": "http://www.gravatar.com/avatar/f94b6b6c15d155f4d4b3b2c05509ab0a?d=mm&s=48",
"24x24": "http://www.gravatar.com/avatar/f94b6b6c15d155f4d4b3b2c05509ab0a?d=mm&s=24",
"16x16": "http://www.gravatar.com/avatar/f94b6b6c15d155f4d4b3b2c05509ab0a?d=mm&s=16",
"32x32": "http://www.gravatar.com/avatar/f94b6b6c15d155f4d4b3b2c05509ab0a?d=mm&s=32"
},
"displayName": "John Doe",
"active": true,
"timeZone": "Europe/Berlin"
},
"customfield_10000": null,
"aggregateprogress": {
"progress": 0,
"total": 0
},
"priority": {
"self": "http://192.168.10.58:8080/rest/api/2/priority/2",
"iconUrl": "http://192.168.10.58:8080/images/icons/priorities/high.png",
"name": "High",
"id": "2"
},
"customfield_10001": null,
"customfield_10100": "1234",
"customfield_10200": null,
"labels": [],
"customfield_10004": null,
"environment": null,
"timeestimate": null,
"aggregatetimeoriginalestimate": null,
"versions": [],
"duedate": null,
"progress": {
"progress": 0,
"total": 0
},
"comment": {
"startAt": 0,
"maxResults": 3,
"total": 3,
"comments": [{
"self": "http://192.168.10.58:8080/rest/api/2/issue/10300/comment/10600",
"id": "10600",
"author": {
"self": "http://192.168.10.58:8080/rest/api/2/user?username=jhdoe",
"name": "jhdoe",
"key": "jhdoe",
"emailAddress": "john.doe#corp.com",
"avatarUrls": {
"48x48": "http://www.gravatar.com/avatar/f94b6b6c15d155f4d4b3b2c05509ab0a?d=mm&s=48",
"24x24": "http://www.gravatar.com/avatar/f94b6b6c15d155f4d4b3b2c05509ab0a?d=mm&s=24",
"16x16": "http://www.gravatar.com/avatar/f94b6b6c15d155f4d4b3b2c05509ab0a?d=mm&s=16",
"32x32": "http://www.gravatar.com/avatar/f94b6b6c15d155f4d4b3b2c05509ab0a?d=mm&s=32"
},
"displayName": "John Doe",
"active": true,
"timeZone": "Europe/Berlin"
},
"body": "Comment 1",
"updateAuthor": {
"self": "http://192.168.10.58:8080/rest/api/2/user?username=jhdoe",
"name": "jhdoe",
"key": "jhdoe",
"emailAddress": "john.doe#corp.com",
"avatarUrls": {
"48x48": "http://www.gravatar.com/avatar/f94b6b6c15d155f4d4b3b2c05509ab0a?d=mm&s=48",
"24x24": "http://www.gravatar.com/avatar/f94b6b6c15d155f4d4b3b2c05509ab0a?d=mm&s=24",
"16x16": "http://www.gravatar.com/avatar/f94b6b6c15d155f4d4b3b2c05509ab0a?d=mm&s=16",
"32x32": "http://www.gravatar.com/avatar/f94b6b6c15d155f4d4b3b2c05509ab0a?d=mm&s=32"
},
"displayName": "John Doe",
"active": true,
"timeZone": "Europe/Berlin"
},
"created": "2015-09-23T19:05:26.593+0300",
"updated": "2015-09-23T19:08:07.010+0300"
}, {
"self": "http://192.168.10.58:8080/rest/api/2/issue/10300/comment/10601",
"id": "10601",
"author": {
"self": "http://192.168.10.58:8080/rest/api/2/user?username=jhdoe",
"name": "jhdoe",
"key": "jhdoe",
"emailAddress": "john.doe#corp.com",
"avatarUrls": {
"48x48": "http://www.gravatar.com/avatar/f94b6b6c15d155f4d4b3b2c05509ab0a?d=mm&s=48",
"24x24": "http://www.gravatar.com/avatar/f94b6b6c15d155f4d4b3b2c05509ab0a?d=mm&s=24",
"16x16": "http://www.gravatar.com/avatar/f94b6b6c15d155f4d4b3b2c05509ab0a?d=mm&s=16",
"32x32": "http://www.gravatar.com/avatar/f94b6b6c15d155f4d4b3b2c05509ab0a?d=mm&s=32"
},
"displayName": "John Doe",
"active": true,
"timeZone": "Europe/Berlin"
},
"body": "Comment 2",
"updateAuthor": {
"self": "http://192.168.10.58:8080/rest/api/2/user?username=jhdoe",
"name": "jhdoe",
"key": "jhdoe",
"emailAddress": "john.doe#corp.com",
"avatarUrls": {
"48x48": "http://www.gravatar.com/avatar/f94b6b6c15d155f4d4b3b2c05509ab0a?d=mm&s=48",
"24x24": "http://www.gravatar.com/avatar/f94b6b6c15d155f4d4b3b2c05509ab0a?d=mm&s=24",
"16x16": "http://www.gravatar.com/avatar/f94b6b6c15d155f4d4b3b2c05509ab0a?d=mm&s=16",
"32x32": "http://www.gravatar.com/avatar/f94b6b6c15d155f4d4b3b2c05509ab0a?d=mm&s=32"
},
"displayName": "John Doe",
"active": true,
"timeZone": "Europe/Berlin"
},
"created": "2015-09-23T19:08:13.644+0300",
"updated": "2015-09-23T19:08:13.644+0300"
}, {
"self": "http://192.168.10.58:8080/rest/api/2/issue/10300/comment/10606",
"id": "10606",
"author": {
"self": "http://192.168.10.58:8080/rest/api/2/user?username=jhdoe",
"name": "jhdoe",
"key": "jhdoe",
"emailAddress": "john.doe#corp.com",
"avatarUrls": {
"48x48": "http://www.gravatar.com/avatar/f94b6b6c15d155f4d4b3b2c05509ab0a?d=mm&s=48",
"24x24": "http://www.gravatar.com/avatar/f94b6b6c15d155f4d4b3b2c05509ab0a?d=mm&s=24",
"16x16": "http://www.gravatar.com/avatar/f94b6b6c15d155f4d4b3b2c05509ab0a?d=mm&s=16",
"32x32": "http://www.gravatar.com/avatar/f94b6b6c15d155f4d4b3b2c05509ab0a?d=mm&s=32"
},
"displayName": "John Doe",
"active": true,
"timeZone": "Europe/Berlin"
},
"body": "Comment 3",
"updateAuthor": {
"self": "http://192.168.10.58:8080/rest/api/2/user?username=jhdoe",
"name": "jhdoe",
"key": "jhdoe",
"emailAddress": "john.doe#corp.com",
"avatarUrls": {
"48x48": "http://www.gravatar.com/avatar/f94b6b6c15d155f4d4b3b2c05509ab0a?d=mm&s=48",
"24x24": "http://www.gravatar.com/avatar/f94b6b6c15d155f4d4b3b2c05509ab0a?d=mm&s=24",
"16x16": "http://www.gravatar.com/avatar/f94b6b6c15d155f4d4b3b2c05509ab0a?d=mm&s=16",
"32x32": "http://www.gravatar.com/avatar/f94b6b6c15d155f4d4b3b2c05509ab0a?d=mm&s=32"
},
"displayName": "John Doe",
"active": true,
"timeZone": "Europe/Berlin"
},
"created": "2015-09-23T19:13:24.836+0300",
"updated": "2015-09-23T19:14:04.464+0300"
}]
},
"issuelinks": [],
"votes": {
"self": "http://192.168.10.58:8080/rest/api/2/issue/DEMO-6/votes",
"votes": 0,
"hasVoted": false
},
"worklog": {
"startAt": 0,
"maxResults": 20,
"total": 0,
"worklogs": []
},
"assignee": null,
"updated": "2015-09-23T19:15:03.338+0300",
"status": {
"self": "http://192.168.10.58:8080/rest/api/2/status/10000",
"description": "",
"iconUrl": "http://192.168.10.58:8080/images/icons/subtask.gif",
"name": "To Do",
"id": "10000",
"statusCategory": {
"self": "http://192.168.10.58:8080/rest/api/2/statuscategory/2",
"id": 2,
"key": "new",
"colorName": "blue-gray",
"name": "To Do"
}
}
}
},
"changelog": {
"id": "10611",
"items": [{
"field": "Custom ID",
"fieldtype": "custom",
"from": null,
"fromString": null,
"to": null,
"toString": "1234"
}]
}
}

Well your comment section in the json is a list of dicts so you can always get the last in the list then call .get('body')
jira_comment = data['issue']['fields']['comment']['comments'][-1].get('body')
this works because in python negative numbers in an array index are treated as going backwards in the array. so -1 gives you the last item in the array.
Does your code work when there is no comments at all?
If it doesn't consider checking that the length of the the comments array is greater than 0 or just wrap it in a try except block.

When a comment is added, the request body should contain data['comment'], which will contain the comment that triggered the issue update. You can compare data['comment']['created'] and data['comment']['updated'] to figure if the comment is new or if the comment has been edited.
see https://developer.atlassian.com/jiradev/jira-apis/webhooks

Related

How to get specific value from JSON response in Python

I have a response coming in as :
b'
{
"_items": [
{
"_id": "61a8dc29fab70adfacf59789",
"name": "CP",
"url": "",
"sd_subscriber_id": "",
"account_manager": "",
"contact_name": "",
"contact_email": "",
"phone": "",
"country": "other",
"is_enabled": true,
"company_type": null,
"monitoring_administrator": null,
"allowed_ip_list": null,
"expiry_date": null,
"original_creator": "6183d49420d13bc4e332281d",
"events_only": false,
"_created": "2021-12-02T14:46:01+0000",
"_updated": "2022-02-06T11:59:32+0000",
"_etag": "277e2a8667b650fe4ba56f4b9b44780f3992062a",
"archive_access": false,
"sections": {
"wire": true,
"agenda": true,
"news_api": true,
"monitoring": true
},
"_links": {
"self": {
"title": "Companie",
"href": "companies/61a8dc29fab70adfacf59789"
},
"related": {
"original_creator": {
"title": "User",
"href": "users/6183d49420d13bc4e332281d"
}
}
}
},
{
"_id": "635ac6b9b837aa06e8e94ea3",
"name": "Load Company No Exp",
"url": "",
"sd_subscriber_id": "",
"account_manager": "",
"contact_name": "",
"contact_email": "karapetyan.mikayel#gmail.com",
"phone": "6478934734",
"country": "",
"is_enabled": true,
"company_type": null,
"monitoring_administrator": null,
"allowed_ip_list": null,
"expiry_date": null,
"original_creator": "6298c949007f2fb1c968dfdf",
"events_only": false,
"_created": "2022-10-27T17:58:17+0000",
"_updated": "2022-10-27T18:03:17+0000",
"_etag": "9cb17d520b3ca9dc1c3326a1ccab8bbb5e7839f2",
"version_creator": "6183d49420d13bc4e332281d",
"_links": {
"self": {
"title": "Companie",
"href": "companies/635ac6b9b837aa06e8e94ea3"
},
"related": {
"original_creator": {
"title": "User",
"href": "users/6298c949007f2fb1c968dfdf"
},
"version_creator": {
"title": "User",
"href": "users/6183d49420d13bc4e332281d"
}
}
}
}
]
}
All i Need is the _items part of it that is inside the [] :
[
{
"_id": "61a8dc29fab70adfacf59789",
"name": "CP",
"url": "",
"sd_subscriber_id": "",
"account_manager": "",
"contact_name": "",
"contact_email": "",
"phone": "",
"country": "other",
"is_enabled": true,
"company_type": null,
"monitoring_administrator": null,
"allowed_ip_list": null,
"expiry_date": null,
"original_creator": "6183d49420d13bc4e332281d",
"events_only": false,
"_created": "2021-12-02T14:46:01+0000",
"_updated": "2022-02-06T11:59:32+0000",
"_etag": "277e2a8667b650fe4ba56f4b9b44780f3992062a",
"archive_access": false,
"sections": {
"wire": true,
"agenda": true,
"news_api": true,
"monitoring": true
},
"_links": {
"self": {
"title": "Companie",
"href": "companies/61a8dc29fab70adfacf59789"
},
"related": {
"original_creator": {
"title": "User",
"href": "users/6183d49420d13bc4e332281d"
}
}
}
},
{
"_id": "635ac6b9b837aa06e8e94ea3",
"name": "Load Company No Exp",
"url": "",
"sd_subscriber_id": "",
"account_manager": "",
"contact_name": "",
"contact_email": "karapetyan.mikayel#gmail.com",
"phone": "6478934734",
"country": "",
"is_enabled": true,
"company_type": null,
"monitoring_administrator": null,
"allowed_ip_list": null,
"expiry_date": null,
"original_creator": "6298c949007f2fb1c968dfdf",
"events_only": false,
"_created": "2022-10-27T17:58:17+0000",
"_updated": "2022-10-27T18:03:17+0000",
"_etag": "9cb17d520b3ca9dc1c3326a1ccab8bbb5e7839f2",
"version_creator": "6183d49420d13bc4e332281d",
"_links": {
"self": {
"title": "Companie",
"href": "companies/635ac6b9b837aa06e8e94ea3"
},
"related": {
"original_creator": {
"title": "User",
"href": "users/6298c949007f2fb1c968dfdf"
},
"version_creator": {
"title": "User",
"href": "users/6183d49420d13bc4e332281d"
}
}
}
}
]
How to get it.
I tried getting it as
temp = response['_items']
but it wont work
Please help me out.
You need to convert raw byte string to Python dict first, assuming that you are using Python version 3.6+ and your response object is either string or bytes:
import json
data = json.loads(response) # loads() decodes it to dict
temp = response['_items']

What is the best way for me to iterate over this dataset to return all matching values from another key value pair if I match a separate key?

I want to be able to search through this list (see bottom of post) of dicts (I think that is what this particular arrangement is called) to search for an ['address'] that matches '0xd2'. If that match is found, I want to return/print all the corresponding ['id']s.
So in this case I would like to return:
632, 315, 432, 100
I'm able to extract individual values like this:
none = None
print(my_dict['result'][2]["id"])
432
I'm struggling with how to get a loop to do this properly.
{
"total": 4,
"page": 0,
"page_size": 100,
"result": [
{
"address": "0xd2",
"id": "632",
"amount": "1",
"name": "Avengers",
"group": "Marvel",
"uri": "https://google.com/",
"metadata": null,
"synced_at": "2022-05-26T22:52:34.113Z",
"last_sync": "2022-05-26T22:52:34.113Z"
},
{
"address": "0xd2",
"id": "315",
"amount": "1",
"name": "Avengers",
"group": "Marvel",
"uri": "https://google.com/",
"metadata": null,
"synced_at": "2022-05-26T22:52:34.113Z",
"last_sync": "2022-05-26T22:52:34.113Z"
},
{
"address": "0xd2",
"id": "432",
"amount": "1",
"name": "Avengers",
"group": "Marvel",
"uri": "https://google.com/",
"metadata": null,
"synced_at": "2022-05-26T22:52:34.113Z",
"last_sync": "2022-05-26T22:52:34.113Z"
},
{
"address": "0x44",
"id": "100",
"amount": "1",
"name": "Suicide Squad",
"group": "DC",
"uri": "https://google.com/",
"metadata": null,
"synced_at": "2022-05-26T22:52:34.113Z",
"last_sync": "2022-05-26T22:52:34.113Z"
}
],
"status": "SYNCED"
}
Welcome to StackOverflow.
You can try list comprehension:
[res["id"] for res in my_dict["result"] if res["address"] == "0xd2"]
If you'd like to use a for loop:
l = []
for res in my_dict["result"]:
if res["address"] == "0xd2":
l.append(res["id"])
You can use a list comprehension.
import json
json_string = """{
"total": 4,
"page": 0,
"page_size": 100,
"result": [
{
"address": "0xd2",
"id": "632",
"amount": "1",
"name": "Avengers",
"group": "Marvel",
"uri": "https://google.com/",
"metadata": null,
"synced_at": "2022-05-26T22:52:34.113Z",
"last_sync": "2022-05-26T22:52:34.113Z"
},
{
"address": "0xd2",
"id": "315",
"amount": "1",
"name": "Avengers",
"group": "Marvel",
"uri": "https://google.com/",
"metadata": null,
"synced_at": "2022-05-26T22:52:34.113Z",
"last_sync": "2022-05-26T22:52:34.113Z"
},
{
"address": "0xd2",
"id": "432",
"amount": "1",
"name": "Avengers",
"group": "Marvel",
"uri": "https://google.com/",
"metadata": null,
"synced_at": "2022-05-26T22:52:34.113Z",
"last_sync": "2022-05-26T22:52:34.113Z"
},
{
"address": "0x44",
"id": "100",
"amount": "1",
"name": "Suicide Squad",
"group": "DC",
"uri": "https://google.com/",
"metadata": null,
"synced_at": "2022-05-26T22:52:34.113Z",
"last_sync": "2022-05-26T22:52:34.113Z"
}
],
"status": "SYNCED"
}"""
json_dict = json.loads(json_string)
result = [elem['id'] for elem in json_dict['result'] if elem['address'] == '0xd2']
print(result)
Output:
['632', '315', '432']
This would store the associated ids in the list:
ids=[]
for r in dataset.get('result'):
if r.get('address')=='0xd2':
ids.append(r.get('id'))

How do I iterate through a nested list from Spotipy's playlist_items function and only print out the fields I'm interested in?

I am attempting to get all the track names and corresponding artist from a playlist using Spotipy. I am at the point where I am able to get a json dump of the fields, but I cannot figure out how to iterate through the dump and print out the fields I'm interested in.
Here is what I'm using to get all information about the playlist:
playlist = spotipy.playlist_items(playlist_id)
If I then print(json.dumps(playlist, indent=2)), then I get the following output:
{
"collaborative": false,
"description": "",
"external_urls": {
"spotify": "https://open.spotify.com/playlist/50uWPcNFdJElMVZWo0IebB"
},
"followers": {
"href": null,
"total": 0
},
"href": "https://api.spotify.com/v1/playlists/50uWPcNFdJElMVZWo0IebB?additional_types=track",
"id": "50uWPcNFdJElMVZWo0IebB",
"images": [
{
"height": 640,
"url": "https://i.scdn.co/image/ab67616d0000b273942a0c9ac8f1def7c8805044",
"width": 640
}
],
"name": "Spotipy Test",
"owner": {
"display_name": "username",
"external_urls": {
"spotify": "https://open.spotify.com/user/username"
},
"href": "https://api.spotify.com/v1/users/username",
"id": "username",
"type": "user",
"uri": "spotify:user:username"
},
"primary_color": null,
"public": false,
"snapshot_id": "Nyw0OTk4MDg1NDM3NzRlOWI2MGY2MDc2ZjMxNTE4OGFkZWQ5Y2QyZDdk",
"tracks": {
"href": "https://api.spotify.com/v1/playlists/50uWPcNFdJElMVZWo0IebB/tracks?offset=0&limit=100&additional_types=track",
"items": [
{
"added_at": "2021-11-17T07:32:59Z",
"added_by": {
"external_urls": {
"spotify": "https://open.spotify.com/user/username"
},
"href": "https://api.spotify.com/v1/users/username",
"id": "username",
"type": "user",
"uri": "spotify:user:username"
},
"is_local": false,
"primary_color": null,
"track": {
"album": {
"album_type": "album",
"artists": [
{
"external_urls": {
"spotify": "https://open.spotify.com/artist/6fOMl44jA4Sp5b9PpYCkzz"
},
"href": "https://api.spotify.com/v1/artists/6fOMl44jA4Sp5b9PpYCkzz",
"id": "6fOMl44jA4Sp5b9PpYCkzz",
"name": "NF",
"type": "artist",
"uri": "spotify:artist:6fOMl44jA4Sp5b9PpYCkzz"
}
],
"available_markets": [],
"external_urls": {
"spotify": "https://open.spotify.com/album/46xdC4Qcvscfs3Ai2RIHcv"
},
"href": "https://api.spotify.com/v1/albums/46xdC4Qcvscfs3Ai2RIHcv",
"id": "46xdC4Qcvscfs3Ai2RIHcv",
"images": [
{
"height": 640,
"url": "https://i.scdn.co/image/ab67616d0000b273942a0c9ac8f1def7c8805044",
"width": 640
},
{
"height": 300,
"url": "https://i.scdn.co/image/ab67616d00001e02942a0c9ac8f1def7c8805044",
"width": 300
},
{
"height": 64,
"url": "https://i.scdn.co/image/ab67616d00004851942a0c9ac8f1def7c8805044",
"width": 64
}
],
"name": "The Search",
"release_date": "2019-07-26",
"release_date_precision": "day",
"total_tracks": 20,
"type": "album",
"uri": "spotify:album:46xdC4Qcvscfs3Ai2RIHcv"
},
"artists": [
{
"external_urls": {
"spotify": "https://open.spotify.com/artist/6fOMl44jA4Sp5b9PpYCkzz"
},
"href": "https://api.spotify.com/v1/artists/6fOMl44jA4Sp5b9PpYCkzz",
"id": "6fOMl44jA4Sp5b9PpYCkzz",
"name": "NF",
"type": "artist",
"uri": "spotify:artist:6fOMl44jA4Sp5b9PpYCkzz"
}
],
"available_markets": [],
"disc_number": 1,
"duration_ms": 248053,
"episode": false,
"explicit": false,
"external_ids": {
"isrc": "USUM71907048"
},
"external_urls": {
"spotify": "https://open.spotify.com/track/3oLe5ZILASG8vU5dxIMfLY"
},
"href": "https://api.spotify.com/v1/tracks/3oLe5ZILASG8vU5dxIMfLY",
"id": "3oLe5ZILASG8vU5dxIMfLY",
"is_local": false,
"name": "The Search",
"popularity": 74,
"preview_url": "https://p.scdn.co/mp3-preview/c26b3dedc967f5009451a693ac275e50c6ecb53c?cid=1f51bd6bdbd94e34884f3ec17c0f68ed",
"track": true,
"track_number": 1,
"type": "track",
"uri": "spotify:track:3oLe5ZILASG8vU5dxIMfLY"
},
"video_thumbnail": {
"url": null
}
},
{
"added_at": "2021-11-17T07:33:24Z",
"added_by": {
"external_urls": {
"spotify": "https://open.spotify.com/user/username"
},
"href": "https://api.spotify.com/v1/users/username",
"id": "username",
"type": "user",
"uri": "spotify:user:username"
},
"is_local": false,
"primary_color": null,
"track": {
"album": {
"album_type": "album",
"artists": [
{
"external_urls": {
"spotify": "https://open.spotify.com/artist/3uL4UpqShC4p2x1dJutoRW"
},
"href": "https://api.spotify.com/v1/artists/3uL4UpqShC4p2x1dJutoRW",
"id": "3uL4UpqShC4p2x1dJutoRW",
"name": "Ghostemane",
"type": "artist",
"uri": "spotify:artist:3uL4UpqShC4p2x1dJutoRW"
}
],
"available_markets": [],
"external_urls": {
"spotify": "https://open.spotify.com/album/4pzCKSWYNM2yUWrNvycC6e"
},
"href": "https://api.spotify.com/v1/albums/4pzCKSWYNM2yUWrNvycC6e",
"id": "4pzCKSWYNM2yUWrNvycC6e",
"images": [
{
"height": 640,
"url": "https://i.scdn.co/image/ab67616d0000b27347fa4895a04fbb517fd42070",
"width": 640
},
{
"height": 300,
"url": "https://i.scdn.co/image/ab67616d00001e0247fa4895a04fbb517fd42070",
"width": 300
},
{
"height": 64,
"url": "https://i.scdn.co/image/ab67616d0000485147fa4895a04fbb517fd42070",
"width": 64
}
],
"name": "Hexada",
"release_date": "2017-09-05",
"release_date_precision": "day",
"total_tracks": 10,
"type": "album",
"uri": "spotify:album:4pzCKSWYNM2yUWrNvycC6e"
},
"artists": [
{
"external_urls": {
"spotify": "https://open.spotify.com/artist/3uL4UpqShC4p2x1dJutoRW"
},
"href": "https://api.spotify.com/v1/artists/3uL4UpqShC4p2x1dJutoRW",
"id": "3uL4UpqShC4p2x1dJutoRW",
"name": "Ghostemane",
"type": "artist",
"uri": "spotify:artist:3uL4UpqShC4p2x1dJutoRW"
}
],
"available_markets": [],
"disc_number": 1,
"duration_ms": 124538,
"episode": false,
"explicit": true,
"external_ids": {
"isrc": "TCADG1741539"
},
"external_urls": {
"spotify": "https://open.spotify.com/track/2Vc6NJ9PW9gD9q343XFRKx"
},
"href": "https://api.spotify.com/v1/tracks/2Vc6NJ9PW9gD9q343XFRKx",
"id": "2Vc6NJ9PW9gD9q343XFRKx",
"is_local": false,
"name": "Mercury: Retrograde",
"popularity": 76,
"preview_url": "https://p.scdn.co/mp3-preview/7d4cb282d38d48d85e9faedb3ea5e74546281d07?cid=1f51bd6bdbd94e34884f3ec17c0f68ed",
"track": true,
"track_number": 4,
"type": "track",
"uri": "spotify:track:2Vc6NJ9PW9gD9q343XFRKx"
},
"video_thumbnail": {
"url": null
}
}
],
"limit": 100,
"next": null,
"offset": 0,
"previous": null,
"total": 2
},
"type": "playlist",
"uri": "spotify:playlist:50uWPcNFdJElMVZWo0IebB"
}
And this is only for two songs being in the playlist!
At this point my limited knowledge of python fails me, because I dont know how to proceed from here. All I am looking for is the track name and the corresponding artist.
So, how do I go through the massive nested list, and only print out the fields Im interested in?
You can use the fields variable to specify the information you want.
For example with
playlist = sp.playlist_items(paylist_id, fields='items.track.artists.name,items.track.name'),
you'll get only the artist name(s) and track name information back:
{"items": [{"track": {"artists": [{"name": "Avicii"}], "name": "Levels - Radio Edit"}}, {"track": {"artists": [{"name": "Avicii"}], "name": "Wake Me Up - Radio Edit"}}]}

Parsing JSON with Python to get specific value

I am trying to parse JSON with Python. I am trying to get the value of "login" which is michael for "type" which is "CreateEvent".
Here's my JSON:
[
{
"id": "7",
"type": "PushEvent",
"actor": {
"id": 5,
"login": "michael",
"display_login": "michael",
"gravatar_id": "",
"url": "https://ec2",
"avatar_url": "https://ec2"
},
"repo": {
"id": 2,
"name": "myorganization/puppet",
"url": "https://ec2"
},
"payload": {
"push_id": 5,
"size": 1,
"distinct_size": 1,
"ref": "refs/heads/dev",
"head": "5584d504f971",
"before": "e485f37ce935775846f33b",
"commits": [
{
"sha": "5584cd504f971",
"author": {
"email": "michael.conte#gmail.ca",
"name": "michael"
},
"message": "Create dev.pp",
"distinct": true,
"url": "https://ec2"
}
]
},
"public": true,
"created_at": "2018-02-20T16:15:57Z",
"org": {
"id": 6,
"login": "myorganization",
"gravatar_id": "",
"url": "https://ec2",
"avatar_url": "https://ec2"
}
},
{
"id": "6",
"type": "CreateEvent",
"actor": {
"id": 5,
"login": "michael",
"display_login": "michael",
"gravatar_id": "",
"url": "https://ec2",
"avatar_url": "https://ec2"
},
"repo": {
"id": 2,
"name": "myorganization/puppet",
"url": "https://ec2"
},
"payload": {
"ref": "dev",
"ref_type": "branch",
"master_branch": "master",
"description": null,
"pusher_type": "user"
},
"public": true,
"created_at": "2018-02-20T16:15:44Z",
"org": {
"id": 6,
"login": "myorganization",
"gravatar_id": "",
"url": "https://ec2",
"avatar_url": "https://ec2"
}
},
{
"id": "5",
"type": "PushEvent",
"actor": {
"id": 5,
"login": "michael",
"display_login": "michael",
"gravatar_id": "",
"url": "https://ec2",
"avatar_url": "https://ec2"
},
"repo": {
"id": 2,
"name": "myorganization/puppet",
"url": "https://ec2"
},
"payload": {
"push_id": 3,
"size": 1,
"distinct_size": 1,
"ref": "refs/heads/master",
"head": "e485f84b875846f33b",
"before": "f8bb87b952bfb4",
"commits": [
{
"sha": "e485f37ce6f33b",
"author": {
"email": "michael.conte#gmail.ca",
"name": "michael"
},
"message": "Create hello.pp",
"distinct": true,
"url": "https://ec2"
}
]
},
"public": true,
"created_at": "2018-02-20T15:48:42Z",
"org": {
"id": 6,
"login": "myorganization",
"gravatar_id": "",
"url": "https://ec2",
"avatar_url": "https://ec2"
}
},
{
"id": "4",
"type": "CreateEvent",
"actor": {
"id": 5,
"login": "michael",
"display_login": "michael",
"gravatar_id": "",
"url": "https://ec2",
"avatar_url": "https://ec2?"
},
"repo": {
"id": 2,
"name": "myorganization/puppet",
"url": "https://ec2"
},
"payload": {
"ref": "master",
"ref_type": "branch",
"master_branch": "master",
"description": null,
"pusher_type": "user"
},
"public": true,
"created_at": "2018-02-20T15:48:21Z",
"org": {
"id": 6,
"login": "myorganization",
"gravatar_id": "",
"url": "https://ec2",
"avatar_url": "https://ec2"
}
},
{
"id": "3",
"type": "CreateEvent",
"actor": {
"id": 5,
"login": "michael",
"display_login": "michael",
"gravatar_id": "",
"url": "https://ec2",
"avatar_url": "https://ec2"
},
"repo": {
"id": 2,
"name": "myorganization/puppet",
"url": "https://ec2"
},
"payload": {
"ref": null,
"ref_type": "repository",
"master_branch": "master",
"description": null,
"pusher_type": "user"
},
"public": true,
"created_at": "2018-02-20T15:48:05Z",
"org": {
"id": 6,
"login": "myorganization",
"gravatar_id": "",
"url": "https://ec2",
"avatar_url": "https://ec2"
}
}
]
Here's my code:
response = requests.get(url, headers=headers, verify=False)
name = response.json()
fname = (name['type']['actor']['login'])
print(fname)
When I run the above code, I get a type error.
TypeError: list indices must be integers or slices, not str.
What am I doing wrong? I am using Python3 for my code.
Try
fname = name[0]['payload']['commits'][0]['author']['name']
The name Michael you are trying to get, is inside the dictionary named author, which is inside a single item list, which is inside the commits dictionary, which is inside the payload dictionary, which is inside a single item list.
Check out the docs for more info on collection types: http://python-textbok.readthedocs.io/en/1.0/Collections.html

Detect hindi encoding, response received from Facebook API in Python

I m trying to access a post on a Facebook Page which has the content in Hindi. So the raw response that I get from Facebook API is as below.
In this response the message is actually in Hindi, how do I detect the encoding of the message and print it in Hindi?
{
"id": "182929845081087_579535732087161",
"from": {
"id": "182929845081087",
"category": "Non-profit organization",
"name": "Brahma Kumaris"
},
"message": "\u092e\u0941\u0930\u0932\u0940 \u0938\u093e\u0930:- \u092e\u0940\u0920\u0947 \u092c\u091a\u094d\u091a\u0947-\u0924\u0941\u092e\u094d\u0939\u0947\u0902 \u0905\u0928\u094d\u0924 \u0924\u0915 \u092f\u0939 \u092e\u0940\u0920\u0940 \u0928\u0949\u0932\u0947\u091c \u0938\u0941\u0928\u0924\u0947 \u0930\u0939\u0928\u093e \u0939\u0948 \u091c\u092c \u0924\u0915 \u091c\u0940\u0928\u093e \u0939\u0948-\u092a\u0922\u093c\u0928\u093e \u0914\u0930 \u092f\u094b\u0917 \u0938\u0940\u0916\u0928\u093e \u0939\u0948 \n \u092a\u094d\u0930\u0936\u094d\u0928:- \u092c\u093e\u092a \u0915\u0947 \u0938\u093e\u0925-\u0938\u093e\u0925 \u0924\u0941\u092e \u092c\u091a\u094d\u091a\u0947 \u0915\u093f\u0938 \u0938\u0947\u0935\u093e \u0915\u0947 \u0928\u093f\u092e\u093f\u0924\u094d\u0924 \u092c\u0928\u0947 \u0939\u0941\u090f \u0939\u094b? \n \u0909\u0924\u094d\u0924\u0930:- \u091c\u0948\u0938\u0947 \u092c\u093e\u092a \u0938\u093e\u0930\u0947 \u0935\u093f\u0936\u094d\u0935 \u0915\u094b \u0932\u093f\u092c\u0930\u0947\u091f \u0915\u0930\u0924\u0947 \u0939\u0948\u0902, \u0938\u092c \u092a\u0930 \u092c\u094d\u0932\u093f\u0938 \u0915\u0930\u0924\u0947 \u0939\u0948\u0902, \u092a\u0940\u0938 \u092e\u0947\u0915\u0930 \u092c\u0928 \u092a\u0940\u0938 \u0938\u094d\u0925\u093e\u092a\u0928 \u0915\u0930\u0924\u0947 \u0939\u0948\u0902 \u0910\u0938\u0947 \u0924\u0941\u092e \u092c\u091a\u094d\u091a\u0947 \u092d\u0940 \u092c\u093e\u092a \u0915\u0947 \u0938\u093e\u0925 \u0907\u0938 \u0938\u0947\u0935\u093e \u0915\u0947 \u0928\u093f\u092e\u093f\u0924\u094d\u0924 \u0939\u094b\u0964 \u0924\u0941\u092e \u0939\u094b \u0938\u0948\u0932\u0935\u0947\u0936\u0928 \u0906\u0930\u094d\u092e\u0940\u0964 \u0924\u0941\u092e\u094d\u0939\u0947\u0902 \u092d\u093e\u0930\u0924 \u0915\u0947 \u0921\u0942\u092c\u0947 \u0939\u0941\u090f \u092c\u0947\u095c\u0947 \u0915\u094b \u0938\u0948\u0932\u0935\u0947\u091c \u0915\u0930\u0928\u093e \u0939\u0948\u0964 21 \u091c\u0928\u094d\u092e\u094b\u0902 \u0915\u0947 \u0932\u093f\u090f \u0938\u092c\u0915\u094b \u0938\u092e\u094d\u092a\u0924\u094d\u0924\u093f\u0935\u093e\u0928 \u092c\u0928\u093e\u0928\u093e \u0939\u0948\u0964 \u0910\u0938\u0940 \u0938\u0947\u0935\u093e \u0924\u0941\u092e \u092c\u091a\u094d\u091a\u094b\u0902 \u0915\u0947 \u0938\u093f\u0935\u093e\u090f \u0914\u0930 \u0915\u094b\u0908 \u0915\u0930 \u0928\u0939\u0940\u0902 \u0938\u0915\u0924\u093e\u0964 \n \u0927\u093e\u0930\u0923\u093e \u0915\u0947 \u0932\u093f\u090f \u092e\u0941\u0916\u094d\u092f \u0938\u093e\u0930:- \n 1) \u0935\u093f\u0915\u0930\u094d\u092e\u093e\u091c\u0940\u0924 \u092c\u0928\u0928\u0947 \u0915\u0947 \u0932\u093f\u090f \u091a\u0932\u0924\u0947 \u092b\u093f\u0930\u0924\u0947 \u092c\u093e\u092a \u0915\u094b \u092f\u093e\u0926 \u0915\u0930\u0928\u0947 \u0915\u093e \u0905\u092d\u094d\u092f\u093e\u0938 \u0915\u0930\u0928\u093e \u0939\u0948\u0964 \u092f\u093e\u0926 \u0915\u093e \u091a\u093e\u0930\u094d\u091f \u091c\u0930\u0942\u0930 \u0930\u0916\u0928\u093e \u0939\u0948\u0964 \n 2) \u0905\u092a\u0928\u0940 \u0939\u0930 \u091a\u0932\u0928 \u0938\u0947 \u092e\u093e\u0924-\u092a\u093f\u0924\u093e \u0914\u0930 \u091f\u0940\u091a\u0930 \u0915\u093e \u0936\u094b \u0915\u0930\u0928\u093e \u0939\u0948\u0964 \u0935\u093f\u0928\u093e\u0936 \u0915\u093e\u0932 \u092e\u0947\u0902 \u092a\u094d\u0930\u0940\u0924 \u092c\u0941\u0926\u094d\u0927\u093f \u092c\u0928\u0915\u0930 \u0930\u0939\u0928\u093e \u0939\u0948\u0964 \u0930\u0942\u0939\u093e\u0928\u0940 \u0938\u0947\u0935\u093e \u0915\u0930\u0928\u0940 \u0939\u0948\u0964 \n \u0935\u0930\u0926\u093e\u0928:- \u0935\u093e\u092f\u0926\u094b\u0902 \u0915\u0940 \u0938\u094d\u092e\u0943\u0924\u093f \u0926\u094d\u0935\u093e\u0930\u093e \u095e\u093e\u092f\u0926\u093e \u0909\u0920\u093e\u0928\u0947 \u0935\u093e\u0932\u0947 \u0938\u0926\u093e \u092c\u093e\u092a \u0915\u0940 \u092c\u094d\u0932\u0948\u0938\u093f\u0902\u0917 \u0915\u0947 \u092a\u093e\u0924\u094d\u0930 \u092d\u0935 \n \u091c\u094b \u092d\u0940 \u0935\u093e\u092f\u0926\u0947 \u092e\u0928 \u0938\u0947, \u092c\u094b\u0932 \u0938\u0947 \u0905\u0925\u0935\u093e \u0932\u093f\u0916\u0915\u0930 \u0915\u0930\u0924\u0947 \u0939\u094b, \u0909\u0928\u094d\u0939\u0947\u0902 \u0938\u094d\u092e\u0943\u0924\u093f \u092e\u0947\u0902 \u0930\u0916\u094b \u0924\u094b \u0935\u093e\u092f\u0926\u0947 \u0915\u093e \u092a\u0942\u0930\u093e \u092b\u093e\u092f\u0926\u093e \u0909\u0920\u093e \u0938\u0915\u0924\u0947 \u0939\u094b\u0964 \u091a\u0947\u0915 \u0915\u0930\u094b \u0915\u093f \u0915\u093f\u0924\u0928\u0947 \u092c\u093e\u0930 \u0935\u093e\u092f\u0926\u093e \u0915\u093f\u092f\u093e \u0939\u0948 \u0914\u0930 \u0915\u093f\u0924\u0928\u093e \u0928\u093f\u092d\u093e\u092f\u093e \u0939\u0948! \u0935\u093e\u092f\u0926\u093e \u0914\u0930 \u095e\u093e\u092f\u0926\u093e - \u0907\u0928 \u0926\u094b\u0928\u094b\u0902 \u0915\u093e \u092c\u0948\u0932\u0947\u0928\u094d\u0938 \u0930\u0939\u0947 \u0924\u094b \u0935\u0930\u0926\u093e\u0924\u093e \u092c\u093e\u092a \u0926\u094d\u0935\u093e\u0930\u093e \u092c\u094d\u0932\u0948\u0938\u093f\u0902\u0917 \u092e\u093f\u0932\u0924\u0940 \u0930\u0939\u0947\u0917\u0940\u0964 \u091c\u0948\u0938\u0947 \u0938\u0902\u0915\u0932\u094d\u092a \u0936\u094d\u0930\u0947\u0937\u094d\u0920 \u0915\u0930\u0924\u0947 \u0939\u094b \u0910\u0938\u0947 \u0915\u0930\u094d\u092e \u092d\u0940 \u0936\u094d\u0930\u0947\u0937\u094d\u0920 \u0939\u094b\u0902 \u0924\u094b \u0938\u092b\u0932\u0924\u093e \u092e\u0942\u0930\u094d\u0924 \u092c\u0928 \u091c\u093e\u092f\u0947\u0902\u0917\u0947\u0964 \n \u0938\u094d\u0932\u094b\u0917\u0928:- \u0938\u094d\u0935\u092f\u0902 \u0915\u094b \u0910\u0938\u093e \u0926\u093f\u0935\u094d\u092f \u0906\u0907\u0928\u093e \u092c\u0928\u093e\u0913 \u091c\u093f\u0938\u092e\u0947\u0902 \u092c\u093e\u092a \u0939\u0940 \u0926\u093f\u0916\u093e\u0908 \u0926\u0947 \u0924\u092c \u0915\u0939\u0947\u0902\u0917\u0947 \u0938\u091a\u094d\u091a\u0940 \u0938\u0947\u0935\u093e\u0964",
"actions": [
{
"name": "Comment",
"link": "http://www.facebook.com/182929845081087/posts/579535732087161"
},
{
"name": "Like",
"link": "http://www.facebook.com/182929845081087/posts/579535732087161"
}
],
"privacy": {
"description": "Public",
"value": "EVERYONE",
"friends": "",
"networks": "",
"allow": "",
"deny": ""
},
"type": "status",
"status_type": "mobile_status_update",
"application": {
"name": "UpdateYou",
"id": "351985104836764"
},
"created_time": "2013-05-30T03:00:08+0000",
"updated_time": "2013-05-30T15:08:42+0000",
"shares": {
"count": 2
},
"likes": {
"data": [
{
"name": "Bhumika Mahant",
"id": "100002238635044"
},
{
"name": "Kumar DrVinay",
"id": "100002736938311"
},
{
"name": "Namrata Trehan Pathria",
"id": "100000281688593"
},
{
"name": "Devesh Sharma",
"id": "100001192346711"
}
],
"count": 37
},
"comments": {
"data": [
{
"id": "579535732087161_6364194",
"from": {
"name": "Namrata Trehan Pathria",
"id": "100000281688593"
},
"message": "Om shanti meet he baba",
"can_remove": true,
"created_time": "2013-05-30T15:08:42+0000",
"like_count": 0,
"user_likes": false
},
{
"id": "579535732087161_6363607",
"from": {
"name": "Cetan Patil",
"id": "100003155153074"
},
"message": "om shanti",
"can_remove": true,
"created_time": "2013-05-30T11:06:27+0000",
"like_count": 0,
"user_likes": false
},
{
"id": "579535732087161_6363549",
"from": {
"name": "Maya Ramchandani",
"id": "100003705148351"
},
"message": "Omshanti",
"can_remove": true,
"created_time": "2013-05-30T10:38:39+0000",
"like_count": 0,
"user_likes": false
},
{
"id": "579535732087161_6363525",
"from": {
"name": "Subhash Bambal",
"id": "100002808519452"
},
"message": "Om Shanti",
"can_remove": true,
"created_time": "2013-05-30T10:29:05+0000",
"like_count": 0,
"user_likes": false
},
{
"id": "579535732087161_6363354",
"from": {
"name": "Poonam Dhanuka",
"id": "100004088191006"
},
"message": "om shanti baba",
"can_remove": true,
"created_time": "2013-05-30T09:12:35+0000",
"like_count": 0,
"user_likes": false
},
{
"id": "579535732087161_6363232",
"from": {
"name": "Hemprakash Pant",
"id": "100004354350224"
},
"message": "Om Shanti Baba",
"can_remove": true,
"created_time": "2013-05-30T07:45:38+0000",
"like_count": 0,
"user_likes": false
},
{
"id": "579535732087161_6362963",
"from": {
"name": "Barun Sharma",
"id": "100005696734282"
},
"message": "om shanti....My baba beloved baba sweet baba.....",
"can_remove": true,
"created_time": "2013-05-30T05:33:26+0000",
"like_count": 0,
"user_likes": false
},
{
"id": "579535732087161_6362770",
"from": {
"name": "Arya Singh",
"id": "100001924554892"
},
"message": "om shanti...................",
"can_remove": true,
"created_time": "2013-05-30T04:41:45+0000",
"like_count": 0,
"user_likes": false
},
{
"id": "579535732087161_6362737",
"from": {
"name": "Khushi Dhurve",
"id": "100001700564503"
},
"message": "Om shanti...gm...mere pyare baapdada...awm...love lots...\u003C3:-):-*:-*",
"can_remove": true,
"created_time": "2013-05-30T04:23:53+0000",
"like_count": 0,
"user_likes": false
},
{
"id": "579535732087161_6362675",
"from": {
"name": "NNibedita Behera",
"id": "100002645048155"
},
"message": "Om shanti baba",
"can_remove": true,
"created_time": "2013-05-30T03:59:49+0000",
"like_count": 0,
"user_likes": false
},
{
"id": "579535732087161_6362654",
"from": {
"name": "Sonali Supe",
"id": "100002108817901"
},
"message": "OM SHANTI MERE PYARE BABA......................",
"can_remove": true,
"created_time": "2013-05-30T03:52:05+0000",
"like_count": 0,
"user_likes": false
},
{
"id": "579535732087161_6362647",
"from": {
"name": "Tejsingh Gurjar",
"id": "100004301563182"
},
"message": "om shanti",
"can_remove": true,
"created_time": "2013-05-30T03:47:46+0000",
"like_count": 0,
"user_likes": false
},
{
"id": "579535732087161_6362620",
"from": {
"name": "Swati Sonar",
"id": "100002927228747"
},
"message": "om shanti",
"can_remove": true,
"created_time": "2013-05-30T03:34:13+0000",
"like_count": 0,
"user_likes": false
},
{
"id": "579535732087161_6362605",
"from": {
"name": "Megha Gulati",
"id": "100004777265970"
},
"message": "gd mrng baba.om shanti",
"can_remove": true,
"created_time": "2013-05-30T03:28:23+0000",
"like_count": 0,
"user_likes": false
},
{
"id": "579535732087161_6362579",
"from": {
"name": "Jay Rathod",
"id": "100005154643627"
},
"message": "OM SHANTI",
"can_remove": true,
"created_time": "2013-05-30T03:14:18+0000",
"like_count": 0,
"user_likes": false
}
],
"paging": {
"cursors": {
"after": "MQ==",
"before": "MTU="
}
}
}
},
Correctly detecting encoding is it impossible. There are libraries that try to guess and work very well but you can't completely trust them. Normally in web environments encodings come in response headers (Content-Encoding), have you checked there?
Then, when you know what encoding is (by guessing or by looking at charset encoding header) you then will have to parse the JSON dictionary and in the proper fields where the strings are encoding (message, for example) you may do message.decode('').
That will return unicode decode string and you may work with that.
Now, it seems to me that you're already getting the response decoded in unicode. The reason why I think that is that in message you get \u092e character which is DEVANAGARI LETTER MA.
So probably you have already decode in Unicode the string and you may processing as you want in python. For instance, do something like: message.encode('utf-8') and there you'll have it encoded in utf-8.
Hope this bring some light!

Categories