I'm using the code below to pull in multiple json files into 1 using pagination. When I try to create a spark dataframe I get the error '
java.lang.ArrayStoreException: java.util.HashMap' during spark.read.json. Below the code block I've provided output from printing the 'issues' data set (minus proprietary info). I've done a bit of research and can't figure out what I can try to make this work. Any assistance would be greatly appreciated!!
import requests
import json
limit = 2
startat = 0
issues = []
for page_num in range(2):
startat = page_num*50
url = f"https://URL/rest/api/2/search?jql=TEST&startAt={startat}&maxResults={limit}"
req = requests.get(url, headers={'Accept': 'application/json', 'Authorization': 'Basic xxxxxxxxxxxxxxxxxxxxxxxxxx'})
data = req.json()
issues.extend(data['issues'])
jsonDF = spark.read.json(issues)
jsonDF.printSchema()
[{'expand': 'operations,versionedRepresentations,editmeta,changelog,customfield_10010.requestTypePractice,renderedFields', 'id': '11441', 'self': 'https://my.url.net/rest/api/2/issue/11441', 'key': 'TS-1401', 'fields': {'statuscategorychangedate': '2022-11-29T07:05:17.359-0800', 'issuetype': {'self': 'https://my.url.net/rest/api/2/issuetype/10004', 'id': '10004', 'description': 'Functionality or a feature expressed as a user goal.', 'iconUrl': 'https://my.url.net/rest/api/2/universal_avatar/view/type/issuetype/avatar/10315?size=medium', 'name': 'Story', 'subtask': False, 'avatarId': 10315, 'hierarchyLevel': 0}, 'parent': {'id': '11420', 'key': 'TS-1380', 'self': 'https://my.url.net/rest/api/2/issue/11420', 'fields': {'summary': 'Clone30 - Migration Epics', 'status': {'self': 'https://my.url.net/rest/api/2/status/10003', 'description': '', 'iconUrl': 'https://my.url.net/', 'name': 'Backlog', 'id': '10003', 'statusCategory': {'self': 'https://my.url.net/rest/api/2/statuscategory/2', 'id': 2, 'key': 'new', 'colorName': 'blue-gray', 'name': 'To Do'}}, 'priority': {'self': 'https://my.url.net/rest/api/2/priority/3', 'iconUrl': 'https://my.url.net/images/icons/priorities/medium.svg', 'name': 'Medium', 'id': '3'}, 'issuetype': {'self': 'https://my.url.net/rest/api/2/issuetype/10000', 'id': '10000', 'description': 'A big user story that needs to be broken down. Created by Jira Software - do not edit or delete.', 'iconUrl': 'https://my.url.net/images/icons/issuetypes/epic.svg', 'name': 'Epic', 'subtask': False, 'hierarchyLevel': 1}}}, 'timespent': None, 'project': {'self': 'https://my.url.net/rest/api/2/project/10001', 'id': '10001', 'key': 'TS', 'name': 'Project', 'projectTypeKey': 'software', 'simplified': False, 'avatarUrls': {'48x48': 'https://my.url.net/rest/api/2/universal_avatar/view/type/project/avatar/10556', '24x24': 'https://my.url.net/rest/api/2/universal_avatar/view/type/project/avatar/10556?size=small', '16x16': 'https://my.url.net/rest/api/2/universal_avatar/view/type/project/avatar/10556?size=xsmall', '32x32': 'https://my.url.net/rest/api/2/universal_avatar/view/type/project/avatar/10556?size=medium'}}, 'customfield_10033': None, 'fixVersions': [], 'aggregatetimespent': None, 'customfield_10034': [], 'customfield_10035': None, 'resolution': None, 'customfield_10036': None, 'customfield_10037': None, 'customfield_10027': None, 'customfield_10028': None, 'customfield_10029': None, 'resolutiondate': None, 'workratio': -1, 'watches': {'self': 'https://my.url.net/rest/api/2/issue/TS-1401/watchers', 'watchCount': 1, 'isWatching': True}, 'lastViewed': '2022-12-08T10:06:57.022-0800', 'created': '2022-11-29T07:05:16.501-0800', 'customfield_10020': None, 'customfield_10021': None, 'customfield_10022': None, 'priority': {'self': 'https://my.url.net/rest/api/2/priority/3', 'iconUrl': 'https://my.url.net/images/icons/priorities/medium.svg', 'name': 'Medium', 'id': '3'}, 'customfield_10023': None, 'customfield_10024': None, 'customfield_10025': None, 'customfield_10026': None, 'labels': [], 'customfield_10016': None, 'customfield_10017': None, 'customfield_10018': {'hasEpicLinkFieldDependency': False, 'showField': False, 'nonEditableReason': {'reason': 'EPIC_LINK_SHOULD_BE_USED', 'message': 'To set an epic as the parent, use the epic link instead'}}, 'customfield_10019': '0|i008a3:', 'timeestimate': None, 'aggregatetimeoriginalestimate': None, 'versions': [], 'issuelinks': [], 'assignee': None, 'updated': '2022-11-29T07:05:20.759-0800', 'status': {'self': 'https://my.url.net/rest/api/2/status/10003', 'description': '', 'iconUrl': 'https://my.url.net/', 'name': 'Backlog', 'id': '10003', 'statusCategory': {'self': 'https://my.url.net/rest/api/2/statuscategory/2', 'id': 2, 'key': 'new', 'colorName': 'blue-gray', 'name': 'To Do'}}, 'components': [], 'timeoriginalestimate': None, 'description': 'Data owner completes template (understand scope of migration efforts)', 'customfield_10010': None, 'customfield_10014': 'TS-1380', 'customfield_10015': None, 'customfield_10005': None, 'customfield_10006': None, 'customfield_10007': None, 'security': None, 'customfield_10008': None, 'customfield_10009': None, 'aggregatetimeestimate': None, 'summary': 'Template', 'creator': {'self': 'https://my.url.net/rest/api/2/user?accountId=5d669f4bf81f2c0d99ee9e38', 'accountId': '5d669f4bf81f2c0d99ee9e38', 'emailAddress': 'test#aol.com', 'avatarUrls': {'48x48': 'https://secure.gravatar.com/avatar/69b7db33e65c274c27a07b28b356e329?d=https%3A%2F%2Favatar-management--avatars.us-west-2.test.png', '24x24': 'https://secure.gravatar.com/avatar/69b7db33e65c274c27a07b28b356e329?d=https%3A%2F%2Favatar-management--avatars.us-west-2.test.png', '16x16': 'https://secure.gravatar.com/avatar/69b7db33e65c274c27a07b28b356e329?d=https%3A%2F%2Favatar-management--avatars.us-west-2.test.png', '32x32': 'https://secure.gravatar.com/avatar/69b7db33e65c274c27a07b28b356e329?d=https%3A%2F%2Favatar-management--avatars.us-west-2.test.png'}, 'displayName': 'Joe Test', 'active': True, 'timeZone': 'America/Los_Angeles', 'accountType': 'atlassian'}, 'subtasks': [{'id': '11442', 'key': 'TS-1402', 'self': 'https://my.url.net/rest/api/2/issue/11442', 'fields': {'summary': 'Complete Template with table/views required (in) and produced (out)', 'status': {'self': 'https://my.url.net/rest/api/2/status/10003', 'description': '', 'iconUrl': 'https://my.url.net/', 'name': 'Backlog', 'id': '10003', 'statusCategory': {'self': 'https://my.url.net/rest/api/2/statuscategory/2', 'id': 2, 'key': 'new', 'colorName': 'blue-gray', 'name': 'To Do'}}, 'priority': {'self': 'https://my.url.net/rest/api/2/priority/3', 'iconUrl': 'https://my.url.net/images/icons/priorities/medium.svg', 'name': 'Medium', 'id': '3'}, 'issuetype': {'self': 'https://my.url.net/rest/api/2/issuetype/10006', 'id': '10006', 'description': "A small piece of work that's part of a larger task.", 'iconUrl': 'https://my.url.net/rest/api/2/universal_avatar/view/type/issuetype/avatar/10316?size=medium', 'name': 'Sub-task', 'subtask': True, 'avatarId': 10316, 'hierarchyLevel': -1}}}], 'reporter': {'self': 'https://my.url.net/rest/api/2/user?accountId=5d669f4bf81f2c0d99ee9e38', 'accountId': '5d669f4bf81f2c0d99ee9e38', 'emailAddress': 'test#aol.com', 'avatarUrls': {'48x48': 'https://secure.gravatar.com/avatar/69b7db33e65c274c27a07b28b356e329?d=https%3A%2F%2Favatar-management--avatars.us-west-2.test.png', '24x24': 'https://secure.gravatar.com/avatar/69b7db33e65c274c27a07b28b356e329?d=https%3A%2F%2Favatar-management--avatars.us-west-2.test.png', '16x16': 'https://secure.gravatar.com/avatar/69b7db33e65c274c27a07b28b356e329?d=https%3A%2F%2Favatar-management--avatars.us-west-2.test.png', '32x32': 'https://secure.gravatar.com/avatar/69b7db33e65c274c27a07b28b356e329?d=https%3A%2F%2Favatar-management--avatars.us-west-2.test.png'}, 'displayName': 'Joe Test', 'active': True, 'timeZone': 'America/Los_Angeles', 'accountType': 'atlassian'}, 'aggregateprogress': {'progress': 0, 'total': 0}, 'customfield_10001': None, 'customfield_10002': None, 'customfield_10003': None, 'customfield_10004': None, 'customfield_10038': None, 'environment': None, 'duedate': None, 'progress': {'progress': 0, 'total': 0}, 'votes': {'self': 'https://my.url.net/rest/api/2/issue/TS-1401/votes', 'votes': 0, 'hasVoted': False}}}, {'expand': 'operations,versionedRepresentations,editmeta,changelog,customfield_10010.requestTypePractice,renderedFields', 'id': '11438', 'self': 'https://my.url.net/rest/api/2/issue/11438', 'key': 'TS-1398', 'fields': {'statuscategorychangedate': '2022-11-29T07:05:09.126-0800', 'issuetype': {'self': 'https://my.url.net/rest/api/2/issuetype/10004', 'id': '10004', 'description': 'Functionality or a feature expressed as a user goal.', 'iconUrl': 'https://my.url.net/rest/api/2/universal_avatar/view/type/issuetype/avatar/10315?size=medium', 'name': 'Story', 'subtask': False, 'avatarId': 10315, 'hierarchyLevel': 0}, 'parent': {'id': '11420', 'key': 'TS-1380', 'self': 'https://my.url.net/rest/api/2/issue/11420', 'fields': {'summary': 'Clone30 - Migration Epics', 'status': {'self': 'https://my.url.net/rest/api/2/status/10003', 'description': '', 'iconUrl': 'https://my.url.net/', 'name': 'Backlog', 'id': '10003', 'statusCategory': {'self': 'https://my.url.net/rest/api/2/statuscategory/2', 'id': 2, 'key': 'new', 'colorName': 'blue-gray', 'name': 'To Do'}}, 'priority': {'self': 'https://my.url.net/rest/api/2/priority/3', 'iconUrl': 'https://my.url.net/images/icons/priorities/medium.svg', 'name': 'Medium', 'id': '3'}, 'issuetype': {'self': 'https://my.url.net/rest/api/2/issuetype/10000', 'id': '10000', 'description': 'A big user story that needs to be broken down. Created by Jira Software - do not edit or delete.', 'iconUrl': 'https://my.url.net/images/icons/issuetypes/epic.svg', 'name': 'Epic', 'subtask': False, 'hierarchyLevel': 1}}}, 'timespent': None, 'project': {'self': 'https://my.url.net/rest/api/2/project/10001', 'id': '10001', 'key': 'TS', 'name': 'Project', 'projectTypeKey': 'software', 'simplified': False, 'avatarUrls': {'48x48': 'https://my.url.net/rest/api/2/universal_avatar/view/type/project/avatar/10556', '24x24': 'https://my.url.net/rest/api/2/universal_avatar/view/type/project/avatar/10556?size=small', '16x16': 'https://my.url.net/rest/api/2/universal_avatar/view/type/project/avatar/10556?size=xsmall', '32x32': 'https://my.url.net/rest/api/2/universal_avatar/view/type/project/avatar/10556?size=medium'}}, 'fixVersions': [], 'customfield_10033': None, 'customfield_10034': [], 'aggregatetimespent': None, 'customfield_10035': None, 'resolution': None, 'customfield_10036': None, 'customfield_10037': None, 'customfield_10027': None, 'customfield_10028': None, 'customfield_10029': None, 'resolutiondate': None, 'workratio': -1, 'lastViewed': None, 'watches': {'self': 'https://my.url.net/rest/api/2/issue/TS-1398/watchers', 'watchCount': 1, 'isWatching': True}, 'created': '2022-11-29T07:05:08.312-0800', 'customfield_10020': None, 'customfield_10021': None, 'customfield_10022': None, 'customfield_10023': None, 'priority': {'self': 'https://my.url.net/rest/api/2/priority/3', 'iconUrl': 'https://my.url.net/images/icons/priorities/medium.svg', 'name': 'Medium', 'id': '3'}, 'customfield_10024': None, 'customfield_10025': None, 'customfield_10026': None, 'labels': [], 'customfield_10016': None, 'customfield_10017': None, 'customfield_10018': {'hasEpicLinkFieldDependency': False, 'showField': False, 'nonEditableReason': {'reason': 'EPIC_LINK_SHOULD_BE_USED', 'message': 'To set an epic as the parent, use the epic link instead'}}, 'customfield_10019': '0|i008ae:y', 'timeestimate': None, 'aggregatetimeoriginalestimate': None, 'versions': [], 'issuelinks': [], 'assignee': None, 'updated': '2022-11-29T07:05:22.417-0800', 'status': {'self': 'https://my.url.net/rest/api/2/status/10003', 'description': '', 'iconUrl': 'https://my.url.net/', 'name': 'Backlog', 'id': '10003', 'statusCategory': {'self': 'https://my.url.net/rest/api/2/statuscategory/2', 'id': 2, 'key': 'new', 'colorName': 'blue-gray', 'name': 'To Do'}}, 'components': [], 'timeoriginalestimate': None, 'description': 'Creating reports/reporting cubes; need to find out reports used', 'customfield_10010': None, 'customfield_10014': 'TS-1380', 'customfield_10015': None, 'customfield_10005': None, 'customfield_10006': None, 'security': None, 'customfield_10007': None, 'customfield_10008': None, 'customfield_10009': None, 'aggregatetimeestimate': None, 'summary': '\xa0create reports/cubes', 'creator': {'self': 'https://my.url.net/rest/api/2/user?accountId=5d669f4bf81f2c0d99ee9e38', 'accountId': '5d669f4bf81f2c0d99ee9e38', 'emailAddress': 'test#aol.com', 'avatarUrls': {'48x48': 'https://secure.gravatar.com/avatar/69b7db33e65c274c27a07b28b356e329?d=https%3A%2F%2Favatar-management--avatars.us-west-2.test.png', '24x24': 'https://secure.gravatar.com/avatar/69b7db33e65c274c27a07b28b356e329?d=https%3A%2F%2Favatar-management--avatars.us-west-2.test.png', '16x16': 'https://secure.gravatar.com/avatar/69b7db33e65c274c27a07b28b356e329?d=https%3A%2F%2Favatar-management--avatars.us-west-2.test.png', '32x32': 'https://secure.gravatar.com/avatar/69b7db33e65c274c27a07b28b356e329?d=https%3A%2F%2Favatar-management--avatars.us-west-2.test.png'}, 'displayName': 'Joe Test', 'active': True, 'timeZone': 'America/Los_Angeles', 'accountType': 'atlassian'}, 'subtasks': [{'id': '11439', 'key': 'TS-1399', 'self': 'https://my.url.net/rest/api/2/issue/11439', 'fields': {'summary': 'Confirm: any reporting cubes required using this data are created and in production?', 'status': {'self': 'https://my.url.net/rest/api/2/status/10003', 'description': '', 'iconUrl': 'https://my.url.net/', 'name': 'Backlog', 'id': '10003', 'statusCategory': {'self': 'https://my.url.net/rest/api/2/statuscategory/2', 'id': 2, 'key': 'new', 'colorName': 'blue-gray', 'name': 'To Do'}}, 'priority': {'self': 'https://my.url.net/rest/api/2/priority/3', 'iconUrl': 'https://my.url.net/images/icons/priorities/medium.svg', 'name': 'Medium', 'id': '3'}, 'issuetype': {'self': 'https://my.url.net/rest/api/2/issuetype/10006', 'id': '10006', 'description': "A small piece of work that's part of a larger task.", 'iconUrl': 'https://my.url.net/rest/api/2/universal_avatar/view/type/issuetype/avatar/10316?size=medium', 'name': 'Sub-task', 'subtask': True, 'avatarId': 10316, 'hierarchyLevel': -1}}}, {'id': '11440', 'key': 'TS-1400', 'self': 'https://my.url.net/rest/api/2/issue/11440', 'fields': {'summary': 'Confirm: any structured reports using this data are created and in production?', 'status': {'self': 'https://my.url.net/rest/api/2/status/10003', 'description': '', 'iconUrl': 'https://my.url.net/', 'name': 'Backlog', 'id': '10003', 'statusCategory': {'self': 'https://my.url.net/rest/api/2/statuscategory/2', 'id': 2, 'key': 'new', 'colorName': 'blue-gray', 'name': 'To Do'}}, 'priority': {'self': 'https://my.url.net/rest/api/2/priority/3', 'iconUrl': 'https://my.url.net/images/icons/priorities/medium.svg', 'name': 'Medium', 'id': '3'}, 'issuetype': {'self': 'https://my.url.net/rest/api/2/issuetype/10006', 'id': '10006', 'description': "A small piece of work that's part of a larger task.", 'iconUrl': 'https://my.url.net/rest/api/2/universal_avatar/view/type/issuetype/avatar/10316?size=medium', 'name': 'Sub-task', 'subtask': True, 'avatarId': 10316, 'hierarchyLevel': -1}}}], 'reporter': {'self': 'https://my.url.net/rest/api/2/user?accountId=5d669f4bf81f2c0d99ee9e38', 'accountId': '5d669f4bf81f2c0d99ee9e38', 'emailAddress': 'test#aol.com', 'avatarUrls': {'48x48': 'https://secure.gravatar.com/avatar/69b7db33e65c274c27a07b28b356e329?d=https%3A%2F%2Favatar-management--avatars.us-west-2.test.png', '24x24': 'https://secure.gravatar.com/avatar/69b7db33e65c274c27a07b28b356e329?d=https%3A%2F%2Favatar-management--avatars.us-west-2.test.png', '16x16': 'https://secure.gravatar.com/avatar/69b7db33e65c274c27a07b28b356e329?d=https%3A%2F%2Favatar-management--avatars.us-west-2.test.png', '32x32': 'https://secure.gravatar.com/avatar/69b7db33e65c274c27a07b28b356e329?d=https%3A%2F%2Favatar-management--avatars.us-west-2.test.png'}, 'displayName': 'Joe Test', 'active': True, 'timeZone': 'America/Los_Angeles', 'accountType': 'atlassian'}, 'aggregateprogress': {'progress': 0, 'total': 0}, 'customfield_10001': None, 'customfield_10002': None, 'customfield_10003': None, 'customfield_10004': None, 'customfield_10038': None, 'environment': None, 'duedate': None, 'progress': {'progress': 0, 'total': 0}, 'votes': {'self': 'https://my.url.net/rest/api/2/issue/TS-1398/votes', 'votes': 0, 'hasVoted': False}}}]
Related
I wanted to reach out for any advice on normalizing JSON within brackets. I have made several attempts with json_normalize and also a user defined function to denest any JSON which has worked effortlessly until now (credit goes to Serge de Gosson de Varennes for sharing, thank you!) but I keep running into errors or the user defined functon does not flatten the json data below.
Here is the CSV examples
total_count,start_index,items,items_per_page,filing_history_status
14750,0,"[{'category': 'capital', 'date': '2023-01-18', 'description': 'capital-return-purchase-own-shares', 'links': {'self': '/company/00102498/filing-history/MzM2NjI2NjAwOGFkaXF6a2N4', 'document_metadata': 'https://frontend-doc-api.company-information.service.gov.uk/document/KFIPMRklI9VLmGnwmLTteT757IXrdIXEweOGPopDAtc'}, 'paper_filed': True, 'type': 'SH03', 'annotations': [{'annotation': 'Clarification hmrc confirmation duty paid', 'category': 'annotation', 'date': '2023-01-18', 'description': 'annotation', 'description_values': {'description': 'Clarification hmrc confirmation duty paid'}, 'type': 'ANNOTATION'}], 'pages': 3, 'barcode': 'ABVEZEGG', 'transaction_id': 'MzM2NjI2NjAwOGFkaXF6a2N4'}, {'category': 'capital', 'date': '2023-01-18', 'description': 'capital-return-purchase-own-shares', 'links': {'self': '/company/00102498/filing-history/MzM2NjI2NjAwNGFkaXF6a2N4', 'document_metadata': 'https://frontend-doc-api.company-information.service.gov.uk/document/Vnn3SQ93Bjt1i9j0bbte52sVb9GA5gbT9P7J8RRy35I'}, 'paper_filed': True, 'type': 'SH03', 'pages': 5, 'barcode': 'ABVEZEEG', 'transaction_id': 'MzM2NjI2NjAwNGFkaXF6a2N4'}, {'action_date': '2023-01-10', 'category': 'capital', 'date': '2023-01-17', 'description': 'capital-sale-or-transfer-treasury-shares-with-date-currency-capital-figure', 'description_values': {'capital': [{'currency': 'USD', 'figure': '251,229,378', 'date': '2023-01-10'}], 'date': '2023-01-10'}, 'links': {'self': '/company/00102498/filing-history/MzM2NTYyMDc0NmFkaXF6a2N4', 'document_metadata': 'https://frontend-doc-api.company-information.service.gov.uk/document/kn0cgguEz1QENk-MUsFXRcLUSaUkcc2ds4dLJxRVLDg'}, 'paper_filed': True, 'type': 'SH04', 'pages': 2, 'barcode': 'YBV5MT5S', 'transaction_id': 'MzM2NTYyMDc0NmFkaXF6a2N4'}, {'action_date': '2022-12-23', 'category': 'capital', 'date': '2023-01-17', 'description': 'capital-sale-or-transfer-treasury-shares-with-date-currency-capital-figure', 'description_values': {'capital': [{'currency': 'USD', 'figure': '251,022,732.5', 'date': '2022-12-23'}], 'date': '2022-12-23'}, 'links': {'self': '/company/00102498/filing-history/MzM2NTYyMTIxMmFkaXF6a2N4', 'document_metadata': 'https://frontend-doc-api.company-information.service.gov.uk/document/9P23zv_wuYQkXdRMEbyDv5wbLM9b06hqGtfOXGyeHPE'}, 'paper_filed': True, 'type': 'SH04', 'pages': 2, 'barcode': 'YBV5MVAI', 'transaction_id': 'MzM2NTYyMTIxMmFkaXF6a2N4'}, {'category': 'capital', 'date': '2023-01-12', 'description': 'capital-return-purchase-own-shares', 'links': {'self': '/company/00102498/filing-history/MzM2NTQzMzkxMmFkaXF6a2N4', 'document_metadata': 'https://frontend-doc-api.company-information.service.gov.uk/document/EgbaDfsLiwXqwi-w5Hus-QKPdcI1kYmjrTOoLJmiI_4'}, 'paper_filed': True, 'type': 'SH03', 'annotations': [{'annotation': 'Clarification HMRC confirmation received that appropriate duty has been paid on this repurchase.', 'category': 'annotation', 'date': '2023-01-12', 'description': 'annotation', 'description_values': {'description': 'Clarification hmrc confirmation received that appropriate duty has been paid on this repurchase.'}, 'type': 'ANNOTATION'}], 'pages': 3, 'barcode': 'ABUTWG01', 'transaction_id': 'MzM2NTQzMzkxMmFkaXF6a2N4'}, {'category': 'capital', 'date': '2023-01-12', 'description': 'capital-return-purchase-own-shares', 'links': {'self': '/company/00102498/filing-history/MzM2NTQzMzkxMWFkaXF6a2N4', 'document_metadata': 'https://frontend-doc-api.company-information.service.gov.uk/document/t0ey0FXW0kaaTi9P8Ps-5zM95IG3iTkM3fTHJK2b6dI'}, 'paper_filed': True, 'type': 'SH03', 'annotations': [{'annotation': 'Clarification HMRC confirmation received that appropriate duty has been paid on this repurchase.', 'category': 'annotation', 'date': '2023-01-12', 'description': 'annotation', 'description_values': {'description': 'Clarification hmrc confirmation received that appropriate duty has been paid on this repurchase.'}, 'type': 'ANNOTATION'}], 'pages': 3, 'barcode': 'ABUTWFZU', 'transaction_id': 'MzM2NTQzMzkxMWFkaXF6a2N4'}, {'action_date': '2022-12-22', 'category': 'capital', 'date': '2023-01-09', 'description': 'capital-allotment-shares', 'description_values': {'date': '2022-12-22', 'capital': [{'figure': '12,706,252', 'currency': 'GBP'}, {'currency': 'USD', 'figure': '4,760,695,780.25'}]}, 'links': {'self': '/company/00102498/filing-history/MzM2NTAyOTczNGFkaXF6a2N4', 'document_metadata': 'https://frontend-doc-api.company-information.service.gov.uk/document/XJE1I-VE224cxMQKzzJFSs6hTPS0X0edMLFe2y8ViEU'}, 'type': 'SH01', 'pages': 4, 'barcode': 'XBUUT623', 'transaction_id': 'MzM2NTAyOTczNGFkaXF6a2N4'}, {'action_date': '2022-10-20', 'category': 'capital', 'date': '2023-01-03', 'description': 'capital-cancellation-shares', 'description_values': {'date': '2022-10-20', 'capital': [{'currency': 'GBP', 'figure': '12,706,252'}, {'figure': '4,866,938,331.50', 'currency': 'USD'}]}, 'links': {'self': '/company/00102498/filing-history/MzM2MzUwMTgwMWFkaXF6a2N4', 'document_metadata': 'https://frontend-doc-api.company-information.service.gov.uk/document/WOIUjTKXXPVduvRy3kkNfq6jiwU6fTZPmFAyq9Tc6k8'}, 'paper_filed': True, 'type': 'SH06', 'pages': 8, 'barcode': 'YBJEXNZF', 'transaction_id': 'MzM2MzUwMTgwMWFkaXF6a2N4'}, {'action_date': '2022-11-07', 'category': 'capital', 'date': '2023-01-03', 'description': 'capital-cancellation-shares', 'description_values': {'date': '2022-11-07', 'capital': [{'figure': '12,706,252', 'currency': 'GBP'}, {'figure': '4,841,613,617', 'currency': 'USD'}]}, 'links': {'self': '/company/00102498/filing-history/MzM2MzUwMjMzN2FkaXF6a2N4', 'document_metadata': 'https://frontend-doc-api.company-information.service.gov.uk/document/vGcWIhrp2T51Xjwvh-DxGZG2s-qX0pee3-uyvVdhq-s'}, 'paper_filed': True, 'type': 'SH06', 'pages': 8, 'barcode': 'YBJEXQ7U', 'transaction_id': 'MzM2MzUwMjMzN2FkaXF6a2N4'}, {'action_date': '2022-11-08', 'category': 'capital', 'date': '2023-01-03', 'description': 'capital-cancellation-shares', 'description_values': {'capital': [{'currency': 'GBP', 'figure': '12,706,252'}, {'figure': '4,839,188,723.50', 'currency': 'USD'}], 'date': '2022-11-08'}, 'links': {'self': '/company/00102498/filing-history/MzM2MzUwMjMzOGFkaXF6a2N4', 'document_metadata': 'https://frontend-doc-api.company-information.service.gov.uk/document/hX4_Z1O_rV2y6hrqQrxs9qIWNOhTwBlDajSien0XGjM'}, 'paper_filed': True, 'type': 'SH06', 'pages': 8, 'barcode': 'YBJEXQ83', 'transaction_id': 'MzM2MzUwMjMzOGFkaXF6a2N4'}, {'action_date': '2022-10-26', 'category': 'capital', 'date': '2023-01-03', 'description': 'capital-cancellation-shares', 'description_values': {'capital': [{'currency': 'GBP', 'figure': '12,706,252'}, {'figure': '4,855,749,979.50', 'currency': 'USD'}], 'date': '2022-10-26'}, 'links': {'self': '/company/00102498/filing-history/MzM2MzUwMjE0MWFkaXF6a2N4', 'document_metadata': 'https://frontend-doc-api.company-information.service.gov.uk/document/esS-R31w94VMTfbEvz1ak5wBWK-dZta5rzlTu6BMTQc'}, 'paper_filed': True, 'type': 'SH06', 'pages': 8, 'barcode': 'YBJEXPGJ', 'transaction_id': 'MzM2MzUwMjE0MWFkaXF6a2N4'}, {'action_date': '2022-10-28', 'category': 'capital', 'date': '2023-01-03', 'description': 'capital-cancellation-shares', 'description_values': {'date': '2022-10-28', 'capital': [{'figure': '12,706,252', 'currency': 'GBP'}, {'currency': 'USD', 'figure': '4,850,257,261'}]}, 'links': {'self': '/company/00102498/filing-history/MzM2MzUwMjE0NWFkaXF6a2N4', 'document_metadata': 'https://frontend-doc-api.company-information.service.gov.uk/document/tvtkhuXRyEhqol5f-NDu1b5D93tykfMTfIJQDWr_iyE'}, 'paper_filed': True, 'type': 'SH06', 'pages': 8, 'barcode': 'YBJEXPBU', 'transaction_id': 'MzM2MzUwMjE0NWFkaXF6a2N4'}, {'action_date': '2022-11-04', 'category': 'capital', 'date': '2023-01-03', 'description': 'capital-cancellation-shares', 'description_values': {'date': '2022-11-04', 'capital': [{'currency': 'GBP', 'figure': '12,706,252'}, {'currency': 'USD', 'figure': '4,843,953,910.25'}]}, 'links': {'self': '/company/00102498/filing-history/MzM2MzUwMjE0OWFkaXF6a2N4', 'document_metadata': 'https://frontend-doc-api.company-information.service.gov.uk/document/rJTbehtmiovQhzX625TilsiLBPLN0BtTQcQQPQyTz2g'}, 'paper_filed': True, 'type': 'SH06', 'pages': 8, 'barcode': 'YBJEXPCI', 'transaction_id': 'MzM2MzUwMjE0OWFkaXF6a2N4'}, {'action_date': '2022-10-31', 'category': 'capital', 'date': '2023-01-03', 'description': 'capital-cancellation-shares', 'description_values': {'capital': [{'figure': '12,706,252', 'currency': 'GBP'}, {'figure': '4,847,654,519', 'currency': 'USD'}], 'date': '2022-10-31'}, 'links': {'self': '/company/00102498/filing-history/MzM2MzUwMjE0OGFkaXF6a2N4', 'document_metadata': 'https://frontend-doc-api.company-information.service.gov.uk/document/vnZ4v6xLe61-aj509lCrSZyrciGWfd3JYArL0pJk-h0'}, 'paper_filed': True, 'type': 'SH06', 'pages': 8, 'barcode': 'YBJEXPC2', 'transaction_id': 'MzM2MzUwMjE0OGFkaXF6a2N4'}, {'action_date': '2022-10-25', 'category': 'capital', 'date': '2023-01-03', 'description': 'capital-cancellation-shares', 'description_values': {'date': '2022-10-25', 'capital': [{'figure': '12,706,252', 'currency': 'GBP'}, {'currency': 'USD', 'figure': '4,858,505,435.25'}]}, 'links': {'self': '/company/00102498/filing-history/MzM2MzUwMjAwNmFkaXF6a2N4', 'document_metadata': 'https://frontend-doc-api.company-information.service.gov.uk/document/NBMwIr_YjIXCPunxhAZDmZyKXFnMgvC-UCbEPIQYkQA'}, 'paper_filed': True, 'type': 'SH06', 'pages': 8, 'barcode': 'YBJEXOMA', 'transaction_id': 'MzM2MzUwMjAwNmFkaXF6a2N4'}, {'action_date': '2022-11-03', 'category': 'capital', 'date': '2023-01-03', 'description': 'capital-cancellation-shares', 'description_values': {'capital': [{'currency': 'GBP', 'figure': '12,706,252'}, {'currency': 'USD', 'figure': '4,846,406,664.25'}], 'date': '2022-11-03'}, 'links': {'self': '/company/00102498/filing-history/MzM2MzUwMjE0N2FkaXF6a2N4', 'document_metadata': 'https://frontend-doc-api.company-information.service.gov.uk/document/JlVWlMQwiT0uw0GsFDi5N3l6YuY6jpLMIIve7Sdxs7c'}, 'paper_filed': True, 'type': 'SH06', 'pages': 8, 'barcode': 'YBJEXPCA', 'transaction_id': 'MzM2MzUwMjE0N2FkaXF6a2N4'}, {'action_date': '2022-10-27', 'category': 'capital', 'date': '2023-01-03', 'description': 'capital-cancellation-shares', 'description_values': {'capital': [{'currency': 'GBP', 'figure': '12,706,252'}, {'figure': '4,852,987,329', 'currency': 'USD'}], 'date': '2022-10-27'}, 'links': {'self': '/company/00102498/filing-history/MzM2MzUwMjE0M2FkaXF6a2N4', 'document_metadata': 'https://frontend-doc-api.company-information.service.gov.uk/document/pA1ufNYMqxg1IqjVXcFx1v2aVCbu8Tt3HYP2jRezWRc'}, 'paper_filed': True, 'type': 'SH06', 'pages': 8, 'barcode': 'YBJEXPGR', 'transaction_id': 'MzM2MzUwMjE0M2FkaXF6a2N4'}, {'action_date': '2022-10-21', 'category': 'capital', 'date': '2023-01-03', 'description': 'capital-cancellation-shares', 'description_values': {'capital': [{'figure': '12,706,252', 'currency': 'GBP'}, {'figure': '4,864,093,329.75', 'currency': 'USD'}], 'date': '2022-10-21'}, 'links': {'self': '/company/00102498/filing-history/MzM2MzUwMjAwM2FkaXF6a2N4', 'document_metadata': 'https://frontend-doc-api.company-information.service.gov.uk/document/RXHig4q8GhyTAm8n6IQ2adQsYVK6tHOFwvx9j8KJ6dM'}, 'paper_filed': True, 'type': 'SH06', 'pages': 8, 'barcode': 'YBJEXOKI', 'transaction_id': 'MzM2MzUwMjAwM2FkaXF6a2N4'}, {'action_date': '2022-10-24', 'category': 'capital', 'date': '2023-01-03', 'description': 'capital-cancellation-shares', 'description_values': {'capital': [{'currency': 'GBP', 'figure': '12,706,252'}, {'currency': 'USD', 'figure': '4,861,296,627.75'}], 'date': '2022-10-24'}, 'links': {'self': '/company/00102498/filing-history/MzM2MzUwMjAwNWFkaXF6a2N4', 'document_metadata': 'https://frontend-doc-api.company-information.service.gov.uk/document/Xab3kKnqCQez6IkyCNR98_krrmTSNuR5usQ7ECT9oLY'}, 'paper_filed': True, 'type': 'SH06', 'pages': 8, 'barcode': 'YBJEXOM2', 'transaction_id': 'MzM2MzUwMjAwNWFkaXF6a2N4'}, {'action_date': '2022-10-14', 'category': 'capital', 'date': '2023-01-03', 'description': 'capital-cancellation-shares', 'description_values': {'capital': [{'currency': 'GBP', 'figure': '12,706,252'}, {'figure': '4,878,250,683.50', 'currency': 'USD'}], 'date': '2022-10-14'}, 'links': {'self': '/company/00102498/filing-history/MzM2MzUwMTY0MWFkaXF6a2N4', 'document_metadata': 'https://frontend-doc-api.company-information.service.gov.uk/document/J3Zv7zI7JWvJJX5SjVruBwthLEgx6KfM7PqRC_ahLnU'}, 'paper_filed': True, 'type': 'SH06', 'pages': 8, 'barcode': 'YBJEXN37', 'transaction_id': 'MzM2MzUwMTY0MWFkaXF6a2N4'}, {'action_date': '2022-10-18', 'category': 'capital', 'date': '2023-01-03', 'description': 'capital-cancellation-shares', 'description_values': {'date': '2022-10-18', 'capital': [{'currency': 'GBP', 'figure': '12,706,252'}, {'figure': '4,872,592,202.25', 'currency': 'USD'}]}, 'links': {'self': '/company/00102498/filing-history/MzM2MzUwMTc5OWFkaXF6a2N4', 'document_metadata': 'https://frontend-doc-api.company-information.service.gov.uk/document/Bpg0KCk-scjaq6aGkI_XXEi7Hq-ia34Yevn0AZN-x9s'}, 'paper_filed': True, 'type': 'SH06', 'pages': 8, 'barcode': 'YBJEXNVV', 'transaction_id': 'MzM2MzUwMTc5OWFkaXF6a2N4'}, {'action_date': '2022-10-12', 'category': 'capital', 'date': '2023-01-03', 'description': 'capital-cancellation-shares', 'description_values': {'date': '2022-10-12', 'capital': [{'figure': '12,706,252', 'currency': 'GBP'}, {'figure': '4,884,232,443.25', 'currency': 'USD'}]}, 'links': {'self': '/company/00102498/filing-history/MzM2MzUwMTYzN2FkaXF6a2N4', 'document_metadata': 'https://frontend-doc-api.company-information.service.gov.uk/document/rtHP4aZ76T2H9ufCF5saoxCTydKp9x-6b62xuyLTLeM'}, 'paper_filed': True, 'type': 'SH06', 'pages': 8, 'barcode': 'YBJEXN5F', 'transaction_id': 'MzM2MzUwMTYzN2FkaXF6a2N4'}, {'action_date': '2022-10-19', 'category': 'capital', 'date': '2023-01-03', 'description': 'capital-cancellation-shares', 'description_values': {'date': '2022-10-19', 'capital': [{'figure': '12,706,252', 'currency': 'GBP'}, {'figure': '4,869,745,587.75', 'currency': 'USD'}]}, 'links': {'self': '/company/00102498/filing-history/MzM2MzUwMTgwMGFkaXF6a2N4', 'document_metadata': 'https://frontend-doc-api.company-information.service.gov.uk/document/8wMFoih73gPzsW3cKAEHxpPHcNUUfWRc7_jdJCvJTo4'}, 'paper_filed': True, 'type': 'SH06', 'pages': 8, 'barcode': 'YBJEXO34', 'transaction_id': 'MzM2MzUwMTgwMGFkaXF6a2N4'}, {'action_date': '2022-10-13', 'category': 'capital', 'date': '2023-01-03', 'description': 'capital-cancellation-shares', 'description_values': {'date': '2022-10-13', 'capital': [{'figure': '12,706,252', 'currency': 'GBP'}, {'figure': '4,881,240,830.50', 'currency': 'USD'}]}, 'links': {'self': '/company/00102498/filing-history/MzM2MzUwMTYzOGFkaXF6a2N4', 'document_metadata': 'https://frontend-doc-api.company-information.service.gov.uk/document/ugvcHhgmk9x3IthyitanO5Vfk3wRf7q5V366uuMhfcU'}, 'paper_filed': True, 'type': 'SH06', 'pages': 8, 'barcode': 'YBJEXN6Z', 'transaction_id': 'MzM2MzUwMTYzOGFkaXF6a2N4'}, {'action_date': '2022-10-17', 'category': 'capital', 'date': '2023-01-03', 'description': 'capital-cancellation-shares', 'description_values': {'date': '2022-10-17', 'capital': [{'currency': 'GBP', 'figure': '12,706,252'}, {'figure': '4,875,370,334', 'currency': 'USD'}]}, 'links': {'self': '/company/00102498/filing-history/MzM2MzUwMTY0MmFkaXF6a2N4', 'document_metadata': 'https://frontend-doc-api.company-information.service.gov.uk/document/7qNdyy_cXHh2bD_kNvz6Tx4CCdLrjqZVuIdCo-ytSx0'}, 'paper_filed': True, 'type': 'SH06', 'pages': 8, 'barcode': 'YBJEXNC8', 'transaction_id': 'MzM2MzUwMTY0MmFkaXF6a2N4'}]",25,filing-history-available
12,0,"[{'action_date': '2022-09-23', 'category': 'confirmation-statement', 'date': '2022-09-30', 'description': 'confirmation-statement-with-no-updates', 'description_values': {'made_up_date': '2022-09-23'}, 'links': {'self': '/company/12098424/filing-history/MzM1MzcxMDIyOWFkaXF6a2N4', 'document_metadata': 'https://frontend-doc-api.company-information.service.gov.uk/document/wm97CdEs1DD7j6mZuW5lQg1Z9c0OINd9yTIotgAhqtI'}, 'type': 'CS01', 'pages': 3, 'barcode': 'XBDNG7WZ', 'transaction_id': 'MzM1MzcxMDIyOWFkaXF6a2N4'}, {'action_date': '2021-07-31', 'category': 'accounts', 'date': '2022-04-29', 'description': 'accounts-with-accounts-type-total-exemption-full', 'description_values': {'made_up_date': '2021-07-31'}, 'links': {'self': '/company/12098424/filing-history/MzMzNzg3MDQyMGFkaXF6a2N4', 'document_metadata': 'https://frontend-doc-api.company-information.service.gov.uk/document/4tOH4j8gi_Y20nCkuVkboyhIS6Lzj9hp-5AjXvRCmfE'}, 'type': 'AA', 'pages': 9, 'barcode': 'XB2XP2YY', 'transaction_id': 'MzMzNzg3MDQyMGFkaXF6a2N4'}, {'action_date': '2021-09-23', 'category': 'confirmation-statement', 'date': '2021-10-06', 'description': 'confirmation-statement-with-no-updates', 'description_values': {'made_up_date': '2021-09-23'}, 'links': {'self': '/company/12098424/filing-history/MzMxNjI0Njc4OGFkaXF6a2N4', 'document_metadata': 'https://frontend-doc-api.company-information.service.gov.uk/document/gejKaHDsYPaEReCs17xJu0zS4PNfMHP43FkjygYeslA'}, 'type': 'CS01', 'pages': 3, 'barcode': 'XAEKJKRD', 'transaction_id': 'MzMxNjI0Njc4OGFkaXF6a2N4'}, {'action_date': '2020-07-31', 'category': 'accounts', 'date': '2021-04-30', 'description': 'accounts-with-accounts-type-total-exemption-full', 'description_values': {'made_up_date': '2020-07-31'}, 'links': {'self': '/company/12098424/filing-history/MzI5OTY1MDE0NGFkaXF6a2N4', 'document_metadata': 'https://frontend-doc-api.company-information.service.gov.uk/document/G42SZuUKIYaMwqBTv4WFQ_46ITwTCso8JoMQmQBZ-qw'}, 'type': 'AA', 'pages': 9, 'barcode': 'XA3HJGOR', 'transaction_id': 'MzI5OTY1MDE0NGFkaXF6a2N4'}, {'action_date': '2020-09-23', 'category': 'confirmation-statement', 'date': '2020-10-09', 'description': 'confirmation-statement-with-no-updates', 'description_values': {'made_up_date': '2020-09-23'}, 'links': {'self': '/company/12098424/filing-history/MzI4MDA5MDQxMmFkaXF6a2N4', 'document_metadata': 'https://frontend-doc-api.company-information.service.gov.uk/document/RpF_HoJZui0F68sDrMM3H6hWW66MIevkwtRsGsRoVYg'}, 'type': 'CS01', 'pages': 3, 'barcode': 'X9FAGITS', 'transaction_id': 'MzI4MDA5MDQxMmFkaXF6a2N4'}, {'type': 'CS01', 'action_date': '2019-09-23', 'description_values': {'made_up_date': '2019-09-23'}, 'links': {'self': '/company/12098424/filing-history/MzI0NDg5NDUxM2FkaXF6a2N4', 'document_metadata': 'https://frontend-doc-api.company-information.service.gov.uk/document/Ekb8A2MZsDtRU7tiVXYx5sXLXuEbhf52kYZ8PdNz9lI'}, 'category': 'confirmation-statement', 'date': '2019-09-23', 'description': 'confirmation-statement-with-updates', 'pages': 4, 'barcode': 'X8EMUH23', 'transaction_id': 'MzI0NDg5NDUxM2FkaXF6a2N4'}, {'type': 'PSC01', 'subcategory': 'notifications', 'action_date': '2019-09-10', 'description_values': {'psc_name': 'Bartosz Pazik', 'notification_date': '2019-09-10'}, 'category': 'persons-with-significant-control', 'links': {'self': '/company/12098424/filing-history/MzI0NDg5MzYzNmFkaXF6a2N4', 'document_metadata': 'https://frontend-doc-api.company-information.service.gov.uk/document/rAlwZsYfyqfYAV4n_TlFX3c5hqyXmBnKakzKhDL3QL0'}, 'date': '2019-09-23', 'description': 'notification-of-a-person-with-significant-control', 'pages': 2, 'barcode': 'X8EMUDUJ', 'transaction_id': 'MzI0NDg5MzYzNmFkaXF6a2N4'}, {'description_values': {'officer_name': 'Mr Bartosz Pazik', 'appointment_date': '2019-09-10'}, 'category': 'officers', 'links': {'self': '/company/12098424/filing-history/MzI0NDg5MjYxOWFkaXF6a2N4', 'document_metadata': 'https://frontend-doc-api.company-information.service.gov.uk/document/tK86XekvMQmckMA9Xs8tE9yaBKnCyr0GxrrAcmGUYcU'}, 'date': '2019-09-23', 'description': 'appoint-person-director-company-with-name-date', 'subcategory': 'appointments', 'type': 'AP01', 'action_date': '2019-09-10', 'pages': 2, 'barcode': 'X8EMU9L6', 'transaction_id': 'MzI0NDg5MjYxOWFkaXF6a2N4'}, {'type': 'TM01', 'subcategory': 'termination', 'action_date': '2019-09-10', 'description_values': {'termination_date': '2019-09-10', 'officer_name': 'Lukasz Wojewodzic'}, 'links': {'self': '/company/12098424/filing-history/MzI0NDg5MDc0NGFkaXF6a2N4', 'document_metadata': 'https://frontend-doc-api.company-information.service.gov.uk/document/RANnOGoDeSbMP2kqvYY-fh2OyyUfGLqmIZfbgl215oE'}, 'category': 'officers', 'date': '2019-09-23', 'description': 'termination-director-company-with-name-termination-date', 'pages': 1, 'barcode': 'X8EMU1SY', 'transaction_id': 'MzI0NDg5MDc0NGFkaXF6a2N4'}, {'description_values': {'cessation_date': '2019-09-09', 'psc_name': 'Ducers Ltd'}, 'category': 'persons-with-significant-control', 'links': {'self': '/company/12098424/filing-history/MzI0NDg5MDU2OWFkaXF6a2N4', 'document_metadata': 'https://frontend-doc-api.company-information.service.gov.uk/document/HdTB7gx7881GBsfsDkfFTEbWnL5kSpnXrtk7k8Fqgz8'}, 'date': '2019-09-23', 'description': 'cessation-of-a-person-with-significant-control', 'subcategory': 'termination', 'type': 'PSC07', 'action_date': '2019-09-09', 'pages': 1, 'barcode': 'X8EMU1CW', 'transaction_id': 'MzI0NDg5MDU2OWFkaXF6a2N4'}, {'description': 'resolution', 'subcategory': 'certificate', 'type': 'RESOLUTIONS', 'description_values': {'description': 'Resolutions'}, 'date': '2019-09-21', 'links': {'self': '/company/12098424/filing-history/MzI0NDgzOTExNmFkaXF6a2N4', 'document_metadata': 'https://frontend-doc-api.company-information.service.gov.uk/document/1RFExUTiTyve6UBTLFym0OQqNVSQ2hecc_5EMI0Z7TU'}, 'category': 'resolution', 'associated_filings': [{'category': 'change-of-name', 'date': '2019-09-21', 'description': 'change-of-name-by-resolution', 'type': 'NM01'}], 'resolutions': [{'category': 'change-of-name', 'description': 'resolution-change-of-name', 'description_values': {'resolution_date': '2019-09-20'}, 'subcategory': 'resolution', 'type': 'RES15'}], 'pages': 3, 'barcode': 'X8EF5GMI', 'transaction_id': 'MzI0NDgzOTExNmFkaXF6a2N4'}, {'links': {'self': '/company/12098424/filing-history/MzIzOTA5NTI0M2FkaXF6a2N4', 'document_metadata': 'https://frontend-doc-api.company-information.service.gov.uk/document/nICiy7Oydb83beqjy1DHvNzoFSFLShsXcDaDnWLt3Cs'}, 'associated_filings': [{'action_date': 1562803200000, 'category': 'capital', 'date': '2019-07-11', 'description': 'statement-of-capital', 'description_values': {'capital': [{'currency': 'GBP', 'figure': '1'}], 'date': '2019-07-11'}, 'type': 'SH01'}, {'category': 'incorporation', 'date': '2019-07-11', 'description': 'model-articles-adopted', 'type': 'MODEL ARTICLES'}], 'date': '2019-07-11', 'category': 'incorporation', 'description': 'incorporation-company', 'type': 'NEWINC', 'pages': 10, 'barcode': 'X89I45EQ', 'transaction_id': 'MzIzOTA5NTI0M2FkaXF6a2N4'}]",25,filing-history-available
The csv is read via Pandas and then I try to flatten it.
import pandas as pd
#credit for this amazing function below and work goes to Serge de Gosson de Varennes
def flatten_nested_json_df(df):
df = df.reset_index()
s = (df.applymap(type) == list).all()
list_columns = s[s].index.tolist()
s = (df.applymap(type) == dict).all()
dict_columns = s[s].index.tolist()
while len(list_columns) > 0 or len(dict_columns) > 0:
new_columns = []
for col in dict_columns:
exploded = pd.json_normalize(df[col]).add_prefix(f'{col}.')
exploded.index = df.index
df = pd.concat([df, exploded], axis=1).drop(columns=[col])
new_columns.extend(exploded.columns) # inplace
for col in list_columns:
# print(f"exploding: {col}")
df = df.drop(columns=[col]).join(df[col].explode().to_frame())
new_columns.append(col)
s = (df[new_columns].applymap(type) == list).all()
list_columns = s[s].index.tolist()
s = (df[new_columns].applymap(type) == dict).all()
dict_columns = s[s].index.tolist()
return df
df1 = pd.read_csv('stackexample.csv')
df1.head() # screenshot highlights data before flattening attempt
Data before attempting to flatten JSON
#attempt at flattening
df2 = flatten_nested_json_df(df1)
df2.head()
Data after attempt to flatten JSON
Thank you for any advice on this matter! I have been stuck on this issue for many days looking at alternatives
Thank you for any suggestions
The function you are using is working, however it needs the data to be in the correct format. The data in column items is in string format, you need to convert it back to data structure (lists and dictionaries).
# https://www.geeksforgeeks.org/python-convert-a-string-representation-of-list-into-list/
from ast import literal_eval
df["items"] = df["items"].apply(literal_eval)
So just use the function in the DataFrame normally.
flatten_nested_json_df(df)
Output:
Hello I'm trying to get specific data out an API call from a website. This is the data I'm receiving
This is the data I'm recieving
{'type': 'NonStockItem', 'attributes': [], 'id': '1', 'description': 'Ikke lagerførte varer høy sats'}
{'type': 'NonStockItem', 'attributes': [], 'id': '2', 'description': 'Ikke lagerførte varer middels sats'}
{'type': 'NonStockItem', 'attributes': [], 'id': '3', 'description': 'Ikke lagerførte varer lav sats'}
{'type': 'NonStockItem', 'attributes': [], 'id': '4', 'description': 'Ikke lagerførte varer avgiftsfri'}
{'type': 'FinishedGoodItem', 'attributes': [{'attributeId': 'NETTBUTIKK', 'description': 'WEB', 'required': False, 'attributeType': 'Text', 'details': []}], 'id': '5', 'description': 'Lagerførte varer høy sats'}
{'type': 'FinishedGoodItem', 'attributes': [], 'id': '6', 'description': 'Lagerførte varer middels sats'}
{'type': 'FinishedGoodItem', 'attributes': [], 'id': '7', 'description': 'Lagerførte varer avgiftsfri'}
{'type': 'LaborItem', 'attributes': [], 'id': '8', 'description': 'Tjenester (prosjekt)'}
{'type': 'ExpenseItem', 'attributes': [], 'id': '9', 'description': 'Utgifter (Reise)'}
{'type': 'FinishedGoodItem', 'attributes': [{'attributeId': 'NETTBUTIKK', 'description': 'WEB', 'required': True, 'attributeType': 'Text', 'details': []}], 'id': 'ONLINE', 'description': 'Online'}
{'type': 'FinishedGoodItem', 'attributes': [{'attributeId': 'NETTBUTIKK', 'description': 'WEB', 'required': False, 'attributeType': 'Text', 'details': []}, {'attributeId': 'WEB2', 'description': 'tilgjengelighet i nettbutikk', 'required': True, 'attributeType': 'Combo', 'details': [{'id': 'Ikke Inne', 'description': 'Produktet er utsolgt.'}, {'id': 'Inne', 'description': 'tilgjengelig i nettbutikk'}]}], 'id': 'WEB', 'description': 'Tilgjengelig på nettbutikk.'}
This is the object fields
[
{
"type": "NonStockItem",
"attributes": [
{
"attributeId": "string",
"description": "string",
"sortOrder": 0,
"required": true,
"attributeType": "Text"
}
]
this is my code
if response.status_code == 200:
itemClass = json.loads(response.text)
for item in itemClass:
print(item["type"])
print(item["description"])
print(item["attributes"])
What I'm trying to do is to get only the attributes with an existing attributeId. I'm a bit stuck because the data inside the attributes array is a dict, how can I get the key values?
Current output:
NonStockItem
Ikke lagerførte varer høy sats
[]
NonStockItem
Ikke lagerførte varer middels sats
[]
NonStockItem
Ikke lagerførte varer lav sats
[]
NonStockItem
Ikke lagerførte varer avgiftsfri
[]
FinishedGoodItem
Lagerførte varer høy sats
[{'attributeId': 'NETTBUTIKK', 'description': 'WEB', 'required': False, 'attributeType': 'Text', 'details': []}]
FinishedGoodItem
Lagerførte varer middels sats
[]
FinishedGoodItem
Lagerførte varer avgiftsfri
[]
LaborItem
Tjenester (prosjekt)
[]
ExpenseItem
Utgifter (Reise)
[]
FinishedGoodItem
Online
[{'attributeId': 'NETTBUTIKK', 'description': 'WEB', 'required': True, 'attributeType': 'Text', 'details': []}]
FinishedGoodItem
Tilgjengelig på nettbutikk.
[{'attributeId': 'NETTBUTIKK', 'description': 'WEB', 'required': False, 'attributeType': 'Text', 'details': []}, {'attributeId': 'WEB2', 'description': 'tilgjengelighet i nettbutikk', 'required': True, 'attributeType': 'Combo', 'details': [{'id': 'Ikke Inne', 'description': 'Produktet er utsolgt.'}, {'id': 'Inne', 'description': 'tilgjengelig i nettbutikk'}]}]
I only want the types which contain an attributeId
I am assuming the list you are working on is accessible using lst[0]['attributes'].
Try the following, which uses list comprehension:
lst = [
{
"type": "NonStockItem",
"attributes": [
{
"attributeId": "string",
"description": "string",
"sortOrder": 0,
"required": True,
"attributeType": "Text"
},
{
# Note that it does not have attributeId
"description": "string",
"sortOrder": 0,
"required": True,
"attributeType": "Text"
}
]
}
]
attrs = lst[0]['attributes']
output = [d for d in attrs if 'attributeId' in d]
print(output)
Output:
[{'attributeId': 'string', 'description': 'string', 'sortOrder': 0, 'required': True, 'attributeType': 'Text'}]
Note that the output has only one element; in the input example I gave, the second dict does not have attributeId.
Pandas json_normalize could be used for this as well:
import json
import pandas as pd
response = '''[
{
"type": "NonStockItem",
"attributes": [
{
"attributeId": "string1",
"description": "string",
"sortOrder": 0,
"required": true,
"attributeType": "Text"
},
{
"attributeId": "string2",
"description": "string",
"sortOrder": 0,
"required": true,
"attributeType": "Text"
}]
},
{
"type": "NonStockItem",
"attributes":[]
},
{
"type": "NonStockItem",
"attributes": [
{
"attributeId": "string3",
"description": "string",
"sortOrder": 0,
"required": true,
"attributeType": "Text"
},
{
"attributeId": "string4",
"description": "string",
"sortOrder": 0,
"required": true,
"attributeType": "Text"
}]
}
]
'''
itemClass = json.loads(response)
print(pd.concat([pd.json_normalize(x["attributes"]) for x in itemClass],
ignore_index=True))
attributeId description sortOrder required attributeType
0 string1 string 0 True Text
1 string2 string 0 True Text
2 string3 string 0 True Text
3 string4 string 0 True Text
The best solution that I could think considering your data sample and output is to verify if item["attributes"] has values inside or no:
Code:
itemclass = [{'type': 'NonStockItem', 'attributes': [], 'id': '1', 'description': 'Ikke lagerførte varer høy sats'},
{'type': 'NonStockItem', 'attributes': [], 'id': '2', 'description': 'Ikke lagerførte varer middels sats'},
{'type': 'NonStockItem', 'attributes': [], 'id': '3', 'description': 'Ikke lagerførte varer lav sats'},
{'type': 'NonStockItem', 'attributes': [], 'id': '4', 'description': 'Ikke lagerførte varer avgiftsfri'},
{'type': 'FinishedGoodItem', 'attributes': [{'attributeId': 'NETTBUTIKK', 'description': 'WEB', 'required': False, 'attributeType': 'Text', 'details': []}], 'id': '5', 'description': 'Lagerførte varer høy sats'},
{'type': 'FinishedGoodItem', 'attributes': [], 'id': '6', 'description': 'Lagerførte varer middels sats'},
{'type': 'FinishedGoodItem', 'attributes': [], 'id': '7', 'description': 'Lagerførte varer avgiftsfri'},
{'type': 'LaborItem', 'attributes': [], 'id': '8', 'description': 'Tjenester (prosjekt)'},
{'type': 'ExpenseItem', 'attributes': [], 'id': '9', 'description': 'Utgifter (Reise)'},
{'type': 'FinishedGoodItem', 'attributes': [{'attributeId': 'NETTBUTIKK', 'description': 'WEB', 'required': True, 'attributeType': 'Text', 'details': []}], 'id': 'ONLINE', 'description': 'Online'},
{'type': 'FinishedGoodItem', 'attributes': [{'attributeId': 'NETTBUTIKK', 'description': 'WEB', 'required': False, 'attributeType': 'Text', 'details': []}, {'attributeId': 'WEB2', 'description': 'tilgjengelighet i nettbutikk', 'required': True, 'attributeType': 'Combo', 'details': [{'id': 'Ikke Inne', 'description': 'Produktet er utsolgt.'}, {'id': 'Inne', 'description': 'tilgjengelig i nettbutikk'}]}], 'id': 'WEB', 'description': 'Tilgjengelig på nettbutikk.'}]
for item in itemclass:
if item["attributes"]:
print(item["type"])
print(item["description"])
print(item["attributes"])
Output:
FinishedGoodItem
Lagerførte varer høy sats
[{'attributeId': 'NETTBUTIKK', 'description': 'WEB', 'required': False, 'attributeType': 'Text', 'details': []}]
FinishedGoodItem
Online
[{'attributeId': 'NETTBUTIKK', 'description': 'WEB', 'required': True, 'attributeType': 'Text', 'details': []}]
FinishedGoodItem
Tilgjengelig på nettbutikk.
[{'attributeId': 'NETTBUTIKK', 'description': 'WEB', 'required': False, 'attributeType': 'Text', 'details': []}, {'attributeId': 'WEB2', 'description': 'tilgjengelighet i nettbutikk', 'required': True, 'attributeType': 'Combo', 'details': [{'id': 'Ikke Inne', 'description': 'Produktet er utsolgt.'}, {'id': 'Inne', 'description': 'tilgjengelig i nettbutikk'}]}]
I am working on a nested dictionary and not able to put it in a dataframe.
This is what it looks like:
{'props': {'initialProps': {'statusCode': 0, '$isMobile': False, '$isIOS': None, '$isAndroid': False, '$host': 'www.tiktok.com', '$pageUrl': '/#alanwalkermusic/video/6816594007829859589', '$language': 'en', '$originalLanguage': 'en', '$languageList': [{'value': 'id', 'alias': 'id-ID', 'label': 'Bahasa Indonesia', 'children': [{'value': 'default', 'label': ''}]}, {'value': 'de', 'alias': 'de-DE', 'label': 'Deutsch', 'children': [{'value': 'default', 'label': ''}]}, {'value': 'en', 'alias': 'en', 'label': 'English', 'children': [{'value': 'default', 'label': ''}]}, {'value': 'es', 'alias': 'es', 'label': 'Español', 'children': [{'value': 'default', 'label': ''}]}, {'value': 'fr', 'alias': 'fr', 'label': 'Français', 'children': [{'value': 'default', 'label': ''}]}, {'value': 'it', 'alias': 'it-IT', 'label': 'Italiano', 'children': [{'value': 'default', 'label': ''}]}, {'value': 'pl', 'alias': 'pl-PL', 'label': 'Polski', 'children': [{'value': 'default', 'label': ''}]}, {'value': 'pt_BR', 'alias': 'pt-BR', 'label': 'Português', 'children': [{'value': 'default', 'label': ''}]}, {'value': 'vi', 'alias': 'vi-VN', 'label': 'Tiếng Việt', 'children': [{'value': 'default', 'label': ''}]}, {'value': 'tr', 'alias': 'tr-TR', 'label': 'Türkçe', 'children': [{'value': 'default', 'label': ''}]}, {'value': 'ru', 'alias': 'ru-RU', 'label': 'Русский', 'children': [{'value': 'default', 'label': ''}]}, {'value': 'hi', 'alias': 'hi-IN', 'label': 'हिन्दी', 'children': [{'value': 'default', 'label': ''}]}, {'value': 'ko', 'alias': 'ko-KR', 'label': '한국어', 'children': [{'value': 'default', 'label': ''}]}, {'value': 'ja', 'alias': 'ja-JP', 'label': '日本語', 'children': [{'value': 'default', 'label': ''}]}, {'value': 'zh_Hant', 'alias': 'zh-Hant-TW', 'label': '繁體中文', 'children': [{'value': 'default', 'label': ''}]}, {'value': 'ar', 'alias': 'ar', 'label': 'العربية', 'children': [{'value': 'default', 'label': ''}]}], '$region': 'DE', '$appId': 1233, '$os': 'windows', '$baseURL': 'm.tiktok.com', '$downloadLink': {'amazon': {'visible': True, 'normal': 'https://www.amazon.com/dp/B0117U0G3M/'}, 'google': {'visible': True, 'normal': 'https://www.tiktok.com/download-link/af/com.zhiliaoapp.musically'}, 'apple': {'visible': True, 'normal': 'https://www.tiktok.com/download-link/af/id835599320'}}, '$abTestVersion': {'clientParameters': '{}', 'clientVersionName': '', 'versionName': '1618717', 'parameters': '{}', 'startTime': '', 'endTime': ''}, '$appType': 'm', '$gray': {'upload': False, 'tea': False}, '$reflowType': 't', '$legalList': [{'title': 'TikTok.com Cookies Policy', 'key': 'tiktok-website-cookies-policy', 'href': 'https://www.tiktok.com/legal/tiktok-website-cookies-policy?lang=en'}, {'title': 'Open Source', 'key': 'open-source', 'href': 'https://www.tiktok.com/legal/open-source?lang=en'}, {'title': 'Virtual Items', 'key': 'virtual-items', 'href': 'https://www.tiktok.com/legal/virtual-items?lang=en'}, {'title': 'Intellectual Property Policy', 'key': 'copyright-policy', 'href': 'https://www.tiktok.com/legal/copyright-policy?lang=en'}, {'title': 'Law Enforcement', 'key': 'law-enforcement', 'href': 'https://www.tiktok.com/legal/law-enforcement?lang=en'}, {'title': 'Privacy Policy', 'key': 'privacy-policy', 'href': 'https://www.tiktok.com/legal/privacy-policy?lang=en'}, {'title': 'Terms of Service', 'key': 'terms-of-use', 'href': 'https://www.tiktok.com/legal/terms-of-use?lang=en'}], '$botType': 'others', '$config': {'covidBanner': {'open': True, 'url': 'https://www.tiktok.com/safety/resources/covid-19', 'background': 'rgba(125,136,227,1)'}}, '$wid': '6819614602332063233'}, 'pageProps': {'serverCode': 200, 'pageState': {'regionAppId': 1233, 'os': 'windows', 'region': 'DE', 'baseURL': 'm.tiktok.com', 'appType': 't', 'fullUrl': 'https://www.tiktok.com/node/share/video/#alanwalkermusic/6816594007829859589'}, 'videoData': {'itemInfos': {'id': '6816594007829859589', 'video': {'urls': ['https://v19.muscdn.com/afefda4362dccd13b93a4d8e79ab5338/5ea477c7/video/tos/useast2a/tos-useast2a-ve-0068c004/7f922f16b9534b80be6523cbb8ba2ce2/?a=1233&br=2652&bt=1326&cr=0&cs=0&dr=0&ds=3&er=&l=202004251147400101150770371E5160EE&lr=tiktok_m&qs=0&rc=MzY3cXc1OjxwdDMzNjczM0ApNDtnNTY1Ojw2N2k8NDU4ZWdoZXBmLjE0ZGBfLS0vMTZzczZjYDEvL2BgMDAzXy0uMC86Yw%3D%3D&vl=&vr='], 'videoMeta': {'width': 540, 'height': 960, 'ratio': 11, 'duration': 11}}, 'covers': ['https://p16-va-default.akamaized.net/obj/tos-maliva-p-0068/8187e5fe9a0f684529910363f5dbe428'], 'authorId': '84453035275386880', 'coversOrigin': ['https://p16-va-default.akamaized.net/obj/tos-maliva-p-0068/7dd18717d8aa4916abc5699c25c09b3e_1587111976'], 'text': 'I’m that friend who can’t stand still when taking photos', 'commentCount': 300, 'diggCount': 14145, 'playCount': 91447, 'shareCount': 29, 'createTime': '1587111974', 'isActivityItem': False, 'warnInfo': [], 'liked': False, 'commentStatus': 0, 'showNotPass': False}, 'authorInfos': {'verified': True, 'secUid': 'MS4wLjABAAAAhS4j-WG_Zq5WJfdV35QEqorNPzc_kkFVNPQmGM6mQIWEg6FOgjS-Dl9eISlYvPDc', 'uniqueId': 'alanwalkermusic', 'userId': '84453035275386880', 'nickName': 'Alan Walker', 'covers': ['https://p16.muscdn.com/img/musically-maliva-obj/1649466056705029~c5_100x100.jpeg']}, 'musicInfos': {'musicId': '6805465027164768258', 'musicName': 'Heading Home', 'authorName': 'Alan Walker, Ruben', 'covers': ['https://p16.muscdn.com/img/musically-maliva-obj/1649466056705029~c5_100x100.jpeg']}, 'authorStats': {'followerCount': 2147815, 'heartCount': '10903873'}, 'challengeInfoList': [], 'duetInfo': '0', 'textExtra': []}, 'shareUser': {'secUid': '', 'userId': '', 'uniqueId': '', 'nickName': '', 'signature': '', 'covers': [], 'coversMedium': [], 'coversLarger': [], 'isSecret': False}, 'shareMeta': {'title': 'Alan Walker on TikTok', 'desc': 'I’m that friend who can’t stand still when taking photos', 'image': {'url': 'http://p16-va-default.akamaized.net/img/tos-maliva-p-0068/8187e5fe9a0f684529910363f5dbe428~tplv-tiktok-play.image', 'width': 540, 'height': 960}}, 'statusCode': 0, 'langList': [{'value': 'id', 'alias': 'id-ID', 'label': 'Bahasa Indonesia', 'children': [{'value': 'default', 'label': ''}]}, {'value': 'de', 'alias': 'de-DE', 'label': 'Deutsch', 'children': [{'value': 'default', 'label': ''}]}, {'value': 'en', 'alias': 'en', 'label': 'English', 'children': [{'value': 'default', 'label': ''}]}, {'value': 'es', 'alias': 'es', 'label': 'Español', 'children': [{'value': 'default', 'label': ''}]}, {'value': 'fr', 'alias': 'fr', 'label': 'Français', 'children': [{'value': 'default', 'label': ''}]}, {'value': 'it', 'alias': 'it-IT', 'label': 'Italiano', 'children': [{'value': 'default', 'label': ''}]}, {'value': 'pl', 'alias': 'pl-PL', 'label': 'Polski', 'children': [{'value': 'default', 'label': ''}]}, {'value': 'pt_BR', 'alias': 'pt-BR', 'label': 'Português', 'children': [{'value': 'default', 'label': ''}]}, {'value': 'vi', 'alias': 'vi-VN', 'label': 'Tiếng Việt', 'children': [{'value': 'default', 'label': ''}]}, {'value': 'tr', 'alias': 'tr-TR', 'label': 'Türkçe', 'children': [{'value': 'default', 'label': ''}]}, {'value': 'ru', 'alias': 'ru-RU', 'label': 'Русский', 'children': [{'value': 'default', 'label': ''}]}, {'value': 'hi', 'alias': 'hi-IN', 'label': 'हिन्दी', 'children': [{'value': 'default', 'label': ''}]}, {'value': 'ko', 'alias': 'ko-KR', 'label': '한국어', 'children': [{'value': 'default', 'label': ''}]}, {'value': 'ja', 'alias': 'ja-JP', 'label': '日本語', 'children': [{'value': 'default', 'label': ''}]}, {'value': 'zh_Hant', 'alias': 'zh-Hant-TW', 'label': '繁體中文', 'children': [{'value': 'default', 'label': ''}]}, {'value': 'ar', 'alias': 'ar', 'label': 'العربية', 'children': [{'value': 'default', 'label': ''}]}], 'webId': '6819614602168567298', 'requestId': '23892479587815260433', 'videoObjectPageProps': {'videoProps': {'url': 'https://www.tiktok.com/#alanwalkermusic/video/6816594007829859589', 'name': 'Alan Walker(#alanwalkermusic) on TikTok I’m that friend who can’t stand still when taking photos', 'description': 'Alan Walker(#alanwalkermusic) has created a short video on TikTok with music Heading Home. I’m that friend who can’t stand still when taking photos', 'keywords': 'alanwalkermusic, Alan Walker,', 'thumbnailUrl': ['https://p16-va-default.akamaized.net/obj/tos-maliva-p-0068/8187e5fe9a0f684529910363f5dbe428', 'https://p16-va-default.akamaized.net/obj/tos-maliva-p-0068/7dd18717d8aa4916abc5699c25c09b3e_1587111976'], 'uploadDate': '2020-04-17T08:26:14.000Z', 'contentUrl': 'https://v19.muscdn.com/afefda4362dccd13b93a4d8e79ab5338/5ea477c7/video/tos/useast2a/tos-useast2a-ve-0068c004/7f922f16b9534b80be6523cbb8ba2ce2/?a=1233&br=2652&bt=1326&cr=0&cs=0&dr=0&ds=3&er=&l=202004251147400101150770371E5160EE&lr=tiktok_m&qs=0&rc=MzY3cXc1OjxwdDMzNjczM0ApNDtnNTY1Ojw2N2k8NDU4ZWdoZXBmLjE0ZGBfLS0vMTZzczZjYDEvL2BgMDAzXy0uMC86Yw%3D%3D&vl=&vr=', 'embedUrl': 'https://www.tiktok.com/embed/v2/6816594007829859589', 'commentCount': '300', 'duration': 'PT11S', 'audio': {'name': 'Heading Home - Alan Walker, Ruben', 'author': 'Alan Walker, Ruben', 'mainEntityOfPage': {'#type': 'ItemPage', '#id': 'https://www.tiktok.com/music/Heading-Home-6805465027164768258'}}, 'creator': {'#type': 'Person', 'name': 'Alan Walker', 'alternateName': 'alanwalkermusic', 'url': 'https://www.tiktok.com/#alanwalkermusic', 'interactionStatistic': [{'#type': 'InteractionCounter', 'interactionType': {'#type': 'http://schema.org/LikeAction'}, 'userInteractionCount': '10903873'}, {'#type': 'InteractionCounter', 'interactionType': {'#type': 'http://schema.org/FollowAction'}, 'userInteractionCount': 2147815}]}, 'width': 540, 'height': 960, 'interactionStatistic': [{'#type': 'InteractionCounter', 'interactionType': {'#type': 'http://schema.org/WatchAction'}, 'userInteractionCount': 91447}, {'#type': 'InteractionCounter', 'interactionType': {'#type': 'http://schema.org/LikeAction'}, 'userInteractionCount': 14145}, {'#type': 'InteractionCounter', 'interactionType': {'#type': 'http://schema.org/ShareAction'}, 'userInteractionCount': 29}]}, 'pageProps': {'type': 'ItemPage', 'id': 'https://www.tiktok.com/#alanwalkermusic/video/6816594007829859589'}, 'breadcrumbProps': {'urlList': [{'name': 'TikTok', 'url': 'https://www.tiktok.com'}, {'name': 'Alan Walker(#alanwalkermusic) Official | TikTok', 'url': 'https://www.tiktok.com/#alanwalkermusic'}, {'name': 'Alan Walker(#alanwalkermusic) on TikTok I’m that friend who can’t stand still when taking photos', 'url': 'https://www.tiktok.com/#alanwalkermusic/video/6816594007829859589'}]}}, 'metaParams': {'title': 'Alan Walker(#alanwalkermusic) on TikTok I’m that friend who can’t stand still when taking photos', 'keywords': 'alanwalkermusic, Alan Walker,', 'description': 'Alan Walker(#alanwalkermusic) has created a short video on TikTok with music Heading Home. I’m that friend who can’t stand still when taking photos', 'robotsContent': 'index, follow'}, 'isSSR': True, 'pageOptions': {'header': {'showUpload': True, 'type': 'webapp'}, 'headOptions': None}}, 'pathname': '/share/video'}, 'page': '/share/video', 'query': {'uniqueId': 'alanwalkermusic', 'id': '6816594007829859589', '$initialProps': {'$wid': '6819614602332063233'}, 'webtoken': 'tac=\'i+2gv2ay5eehis!i#piis"yZl!%s"l"u&kLs#l l#vr*charCodeAtx0[!cb^i$1em7b*0d#>>>s j\\uffeel s#0y\\xe8g,&qnfme|ms l vk.}l ,(dfijxdaamvk5}l ,(dfijxdaam,$lwcamk\\xadl ,(dfijxdaam,$lwcams!l!v,\\\'nfmosCkmx,*~bgyad>r}~[!c0b<k$t jql!v,\\\'nfmosCkmx,%cokm3[!c0d#===k$t jMl!v,\\\'nfmosCkmx,0xefc.:9&*.4+2-0.[!c0d#===k"t f z[ cb|1d<y\\u01ea,(|fY\\x7f~d`hs g,(lfi~ah`{ms!g,&qnfme|ms"g,)gk}ejo{\\x7fcms#,)|doikgauus$ul"d\\\',typeofl$d#===v!kA}l"vl mx[ c,/T\\x7fsxvwa6#qw~tk#d#!==v!k\\\\}gr&Object,)yxdxbzv`tml mvr$callxl"[!c,/T\\x7fsxvwa6#qw~tk#d#!==v!k4}ul!d\\\',typeofl$d#===v!kG}l!vl mx[ cv,\\\'nfmosCkmx,(Lfi~ah`{[!c0b<v!k4}ul#d\\\',typeofl$d#===v!kD}l#vl mx[ c,2I|v\\x7fstl9Tzjty~TNP~d#!==v!k=}ugr(locationd\\\',typeofl$d#===k"t fv!k4}l!,,hbmz}t|gYzrrm!!!kjg,\\\'oaz~d~tms%ul%d\\\',typeofl$d#===v!kB}l%vl mx[ c,0K~pyqvb7PpiosogBd#!==k"t f z[ cb|1d<y\\xe2g,&qnfme|ms l vk-}l ,\\\'dggyd`hmvk7}l ,\\\'dggyd`hm,\\\'aa{oiyjmk"t ugr.InstallTriggerd\\\',typeof,)|doikgauud#!==kwl vkn}l ,*e~xh|Xyuf{ml ,*cebh|Xyuf{mb-\\x94b>v!kF}l ,+dyyk}Xt{t|aml ,+bbck}Xt{t|amb-\\x94b>k"t f z[ cb|1d<y\\xa0g,&akgkkgms g,&Iebli\\x7fms!ul d\\\',typeof,)|doikgauud#!==vkh}l!,)yxdxbzv`tm,(|fY\\x7f~d`hmvr$callxl ,\\\'wzfin\\x7f~m[!c,0K~pyqvb7hkuxynmBd#=== z[ cb|1d<y\\u011eg,(lfi~ah`{ms g,)gk}ejo{\\x7fcms!g,&qnfme|ms"fv!k4}l ,,hbmz}t|gYzrrm!!!k\\xd7,\\\'wd|mbb~l!d"in!v!kI}l!,\\\'wd|mbb~mg,+[`xif~P`aulmd*instanceof!v!k1},(Wybjbyabl"d"inv!k4},+hmab_xp|g{xl"d"inv!k4},+TScghxe\\x7frfpl"d"inv!kP},%Dscafl"d"in,8[xtm}nLzNEGQMKAdGG^NTY\\x1ckl"d"inb< f z[ cb|1d<y\\u02a1g,&qnfme|ms g,)gk}ejo{\\x7fcms!g,&Iebli\\x7fms"l!,)~oih\\x7fgyucmk"t (\\x80,.jjvx|vDgyg}knbl"d"inkfl"v,.jjvx|vDgyg}knbmxl!,)~oih\\x7fgyucgr&Objectn vuq%valuevfq(writable[#c}) %{s#t ,4KJarz}hrjxl#EWCOQDRB,3LKfs{}wsnqB{iAMWBP#,;DCj{}DSKUAWyTK[C[XrHZ^RFZ[[,7HGn\\x7fyxowiES}PGWOW\\\\vL^BN,5JI`}{~iuk{m\\x7fRAQMURxNG,3LKsnsjpl~nB{iAMWBP#,2MLpg\\x7fa}kEnrjl~PQGG,5JI`}{~iuk{m\\x7fTLTVDVWMM,1NMwf|`rjF\\x7fm}qk~TD,4KJert|tripAjNVPBTUCC,4KJpo|ksmyoAjNVPBTUCC[+s#,)Vyn`h`fe|,,olbcCt~vz|cz,6ID}u\\x7fuuhs#ieg|v#EHZMOY[#s$l$*%s%l%u&k4s&l$l&ms\\\'l l\\\'mk"t j\\x06l#*%s%l%u&k?s&l#l&ms\\\'l ,(lfi~ah`{ml\\\'mk"t j\\ufffbl ,(lfi~ah`{m*%s%l%u&kls&l&vr%matchxgr&RegExp$*\\\\$[a-z]dc_$ n"[!cvk:}l ,(lfi~ah`{ml&m,&efkaoTmk"t j\\uffcef z[ cb|1d<y\\u024fg,&qnfme|ms gr&RegExp$+constructor$!in"vr$testxl ,+CX#BJ|t\\x7fvzam[!cv!k\\xb2}yZl vr(toStringx[ c,AzMAN#ES\\x08zKMM_G}U\\\\]GQ{YCQ_SX]IWP.\\x1cd#=== l ,&ufnhxbm!v!kd}ugr&safarid\\\',typeof,)|doikgauud#!==vk=}gr&safari,0`da{Zzb~~pyzhtqqm&k\\u010e(=l v,,c}kaTpfrvtermxzzzz[$c}) %{s!t (\\x85l ,.}jcb{|zFbxjx}~mvr\\\'setItemx,+xc`kDuhZvfp, ["c}l ,.}jcb{|zFbxjx}~mvr*removeItemx,+xc`kDuhZvfp[!c}) \\x7f{s!l!,$gjbbmgr,DOMException,2CF[AWH]AY^YY[[\\x7fdpqmd#===vkC}l ,.}jcb{|zFbxjx}~m,&jbfn~cm0d#===k"t f fv!k>}g,(lfi~ah`{m,,hbmz}t|gYzrrm!!k`l ,)`doiukkTSm!vkJ}l ,,\\\\bgadt`Vbpxcmv!k4}l ,.C\\\\#~{}`pdRn|tomk"t f z[ cb|1d<y\\u0165g,(lfi~ah`{ms g,)gk}ejo{\\x7fcms!,(|fY\\x7f~d`hs",\\\'nfmosCks#fv!k4}l ,,hbmz}t|gYzrrm!!!k\\u0113l v,-n|jqewVxp{rvmmx,&eff\\x7fkx[!cs$l$,)}eOmyoZB]mvl"mx[ cv,\\\'umyfjohmxgr&RegExp$#\\\\s*$!gn"$ ["cvl#mx,*djxdxjs~vv[!c0b<v!kh}l!l"mvl"mx[ cvr\\\'replacexgr&RegExp$#\\\\s*$!gn"$ ["cvl#mx,*djxdxjs~vv[!c0b<v!kP}l!,\\\'wd|mbb~mvl"mx[ c,4Ozt}}zn;LqkxIOcQVD_zd#!== f z[ cb|1d<1b|\'', '0fea6a13c52b4d4725368f24b045ca84': {}, 'aa59d67c2123f094d0d6798ffe651c4d': {}}, 'buildId': '1.0.1.1363', 'assetPrefix': '//s16.tiktokcdn.com/tiktok/falcon', 'isFallback': False, 'customServer': True}
I can reach any values by writing so:
import json
parsed = json.loads(source_code)
props = parsed['props']['pageProps']['videoData']
follower = props['authorStats']['followerCount']
heartcount = props['authorStats']['heartCount']
AuthorID = props['itemInfos']['authorId']
Commentcount = pandas.DataFrame(props['itemInfos']['commentCount']
CreateTime = props['itemInfos']['createTime']
But I was not able to put all of them into a dataframe with one row and all of those follower,heartcount etc as columns.
Thank you for your help!
This question already has answers here:
How can I convert JSON to CSV?
(26 answers)
Closed 3 years ago.
I am trying to write my JSON output to CSV, but I'm not sure how to separate my values into individual columns
This is my current code
with open('dict.csv', 'w') as csv_file:
writer = csv.writer(csv_file)
for key, value in response.json().items():
writer.writerow([value])
print(value)
This is the csv file I am getting:
current csv file
This is the desired csv file/output I want to get:
desired output
This is an example of my JSON Output
[{'id': '123', 'custom_id': '12', 'company': 28, 'company_name': 'Sunshine'}, {'id': '224', 'custom_id': '14', 'company': 38, 'company_name': 'Flowers'},
{'id': '888', 'custom_id': '10', 'company': 99, 'company_name': 'Fields'}]
how about this JSON format? (a more complicated one)
[{'id': '777', 'custom_id': '000112', 'company': 28, 'company_name':
'Weddings Inc', 'delivery_address': '25 olive park terrace, 61234', 'delivery_timeslot': {'lower': '2019-12-06T10:00:00Z', 'upper': '2019-12-06T13:00:00Z', 'bounds': '[)'}, 'sender_name': 'Joline', 'sender_email': '', 'sender_contact': '91234567', 'removed': None, 'recipient_name': 'Joline', 'recipient_contact': '91866655', 'notes': '', 'items': [{'id': 21668, 'name': 'Loose hair flowers', 'quantity': 1, 'metadata': {}, 'removed': None}, {'id': 21667, 'name': "Groom's Boutonniere", 'quantity': 1, 'metadata': {}, 'removed': None}, {'id': 21666, 'name': 'Bridal Bouquet', 'quantity': 1, 'metadata': {}, 'removed': None}], 'latitude': '1.1234550920764211111', 'longitude': '103.864352476201000000', 'created': '2019-08-15T05:40:30.385467Z', 'updated': '2019-08-15T05:41:27.930110Z', 'status': 'pending', 'verbose_status': 'Pending', 'logs': [{'id': 56363, 'order': '50c402', 'order_custom_id': '000112', 'order_delivery_address': '25 olive park terrace, 61234', 'order_delivery_timeslot': {'lower': '2019-12-06T10:00:00Z', 'upper': '2019-12-06T13:00:00Z', 'bounds': '[)'}, 'message': 'Order was created.', 'failure_reason': None, 'success_code': None, 'success_description': None, 'created': '2019-08-15T05:40:30.431790Z', 'removed': None}, {'id': 56364, 'order': '50c402d8-7c76-45b5-b883-e2fb887a507e', 'order_custom_id': 'INV-000112', 'order_delivery_address': '25 olive park terrace, 61234', 'order_delivery_timeslot': {'lower': '2019-12-06T10:00:00Z', 'upper': '2019-12-06T13:00:00Z', 'bounds': '[)'}, 'message': 'Order is pending.', 'failure_reason': None, 'success_code': None, 'success_description': None, 'created': '2019-08-15T05:40:30.433139Z', 'removed': None}], 'reschedule_requests': [], 'signature': None},
{'id': '241', 'custom_id': '000123', 'company': 22, 'company_name': 'Pearl Pte Ltd', 'delivery_address': '90 Merchant Road, Hotel Royal, 223344', 'delivery_timeslot': {'lower': '2019-11-29T10:00:00Z', 'upper': '2019-11-29T13:00:00Z', 'bounds': '[)'}, 'sender_name': 'Vera Smith', 'sender_email': '', 'sender_contact': '81234567', 'removed': None, 'recipient_name': 'Vera Smith', 'recipient_contact': '81234561', 'notes': '', 'items': [{'id': 22975, 'name': 'Custom wrapped bouquet', 'quantity': 2, 'metadata': {}, 'removed': None}, {'id': 22974, 'name': "Parents' boutonniere x 3", 'quantity': 1, 'metadata': {}, 'removed': None}, {'id': 22973, 'name': "Groom's boutonniere", 'quantity': 1, 'metadata': {}, 'removed': None}, {'id': 22972, 'name': 'Loose hair flowers', 'quantity': 1, 'metadata': {}, 'removed': None}, {'id': 22971, 'name': 'Bridal Bouquet', 'quantity': 1, 'metadata': {}, 'removed': None}], 'latitude': '1.28821802835873000000', 'longitude': '103.84569230314800000000', 'created': '2019-08-30T03:20:17.477528Z', 'updated': '2019-08-30T03:29:25.307856Z', 'status': 'pending', 'verbose_status': 'Pending', 'logs': [{'id': 59847, 'order': '24117085-9104-4442-841b-4a734f801d39', 'order_custom_id': 'INV-000123', 'order_delivery_address': '90 Merchant Road, Hotel Royal, 223344', 'order_delivery_timeslot': {'lower': '2019-11-29T10:00:00Z', 'upper': '2019-11-29T13:00:00Z', 'bounds': '[)'}, 'message': 'Order was created.', 'failure_reason': None, 'success_code': None, 'success_description': None, 'created': '2019-08-30T03:20:17.511250Z', 'removed': None}, {'id': 59848, 'order': '24117085-9104-4442-841b-4a734f801d39', 'order_custom_id': 'INV-000123', 'order_delivery_address': '90 Merchant Road, Hotel Royal, 223344', 'order_delivery_timeslot': {'lower': '2019-11-29T10:00:00Z', 'upper': '2019-11-29T13:00:00Z', 'bounds': '[)'}, 'message': 'Order is pending.', 'failure_reason': None, 'success_code': None, 'success_description': None, 'created': '2019-08-30T03:20:17.513132Z', 'removed': None}], 'reschedule_requests': [], 'signature': None}]
Use pandas library:
df.to_csv() - Write object to a comma-separated values (csv) file.
Ex.
import pandas as pd
data = [{'id': '123', 'custom_id': '12', 'company': 28, 'company_name': 'Sunshine'},
{'id': '224', 'custom_id': '14', 'company': 38, 'company_name': 'Flowers'},
{'id': '888', 'custom_id': '10', 'company': 99, 'company_name': 'Fields'}]
df = pd.DataFrame(data)
df.to_csv('sample.csv')
Try:
import csv
csv_file = 'my_file.csv'
csv_columns = ['id', 'custom_id', 'company', 'company_name']
dict_data = [{'id': '123', 'custom_id': '12', 'company': 28, 'company_name': 'Sunshine'}, {'id': '224', 'custom_id': '14', 'company': 38, 'company_name': 'Flowers'}, {'id': '888', 'custom_id': '10', 'company': 99, 'company_name': 'Fields'}]
try:
with open(csv_file, 'w') as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=csv_columns)
writer.writeheader()
for data in dict_data:
writer.writerow(data)
except IOError:
print("I/O error")
Given your response data in json format
response = [{'id': '123', 'custom_id': '12', 'company': 28, 'company_name': 'Sunshine'},
{'id': '224', 'custom_id': '14', 'company': 38, 'company_name': 'Flowers'},
{'id': '888', 'custom_id': '10', 'company': 99, 'company_name': 'Fields'}]
You can convert it to a list of lists using
header = [response[0].keys()]
data = [row.values() for row in response]
csv_list = header + data
And then save it to csv using
with open('dict.csv', "w") as f:
for row in csv_list:
f.write("%s\n" % ','.join(str(col) for col in row))
This should yield your desired output
I'm confused. None of the stuff I tried from other related questions worked and I can't seem to find it by googling.
I want to filter a json api response with Python, without just sorting the datafeed I get back - that's not much good to me. I also want to explicitly select one field, rather than deleting all the other fields, because sometimes there are more fields than other times. It has to be accomplished with the structure of the request, not by modifying anything after the fact.
I use the following code to get a list of sports from the matchbook api, but how would I just show the names of those sports, filtering out the id and types?
r7 = s.get('https://api.matchbook.com/edge/rest/lookups/sports')
r8 = r7.json()
pprint.pprint (r8)
This is what I get back:
{'offset': 0,
'per-page': 20,
'sports': [{'id': 112, 'name': 'Australian Rules', 'type': 'SPORT'},
{'id': 13, 'name': 'Auto Racing', 'type': 'SPORT'},
{'id': 3, 'name': 'Baseball', 'type': 'SPORT'},
{'id': 4, 'name': 'Basketball', 'type': 'SPORT'},
{'id': 14, 'name': 'Boxing', 'type': 'SPORT'},
{'id': 110, 'name': 'Cricket', 'type': 'SPORT'},
{'id': 11, 'name': 'Current Events', 'type': 'SPORT'},
{'id': 115, 'name': 'Cycling', 'type': 'SPORT'},
{'id': 116, 'name': 'Darts', 'type': 'SPORT'},
{'id': 117, 'name': 'Gaelic Football', 'type': 'SPORT'},
{'id': 8, 'name': 'Golf', 'type': 'SPORT'},
{'id': 241798357140019,
'name': 'Greyhound Racing',
'type': 'SPORT'},
{'id': 24735152712200, 'name': 'Horse Racing', 'type': 'SPORT'},
{'id': 222109340250019,
'name': 'Horse Racing (Ante Post)',
'type': 'SPORT'},
{'id': 231138347942400,
'name': 'Horse Racing Beta',
'type': 'SPORT'},
{'id': 118, 'name': 'Hurling', 'type': 'SPORT'},
{'id': 6, 'name': 'Ice Hockey', 'type': 'SPORT'},
{'id': 126, 'name': 'MMA', 'type': 'SPORT'},
{'id': 5, 'name': 'NCAA Basketball', 'type': 'SPORT'},
{'id': 2, 'name': 'NCAA Football', 'type': 'SPORT'}],
'total': 28}
401
[Finished in 5.3s]
How would I get rid of the 'id' and 'type' fields?
This is an extract of a large set of json data:
{'events': [{'allow-live-betting': True,
'category-id': [9,
297063445660036,
410444698700010,
410468520880009,
477274740480010,
486941505790009],
'id': 488243069570009,
'in-running-flag': True,
'markets': [{'allow-live-betting': True,
'back-overround': 102.04249,
'event-id': 488243069570009,
'id': 488243069830010,
'in-running-flag': True,
'lay-overround': 97.95751,
'live': True,
'market-type': 'money_line',
'name': 'Moneyline',
'runners': [{'event-id': 488243069570009,
'event-participant-id': 488243069720009,
'id': 488243069860009,
'market-id': 488243069830010,
'name': 'G Mager',
'prices': [{'available-amount': 48.42639,
'currency': 'GBP',
'decimal-odds': 1.53191,
'exchange-type': 'back-lay',
'odds': 1.53191,
'odds-type': 'DECIMAL',
'side': 'back'},
{'available-amount': 473.49579,
'currency': 'GBP',
'decimal-odds': 1.51546,
'exchange-type': 'back-lay',
'odds': 1.51546,
'odds-type': 'DECIMAL',
'side': 'back'},
{'available-amount': 96.87754,
'currency': 'GBP',
'decimal-odds': 1.40322,
'exchange-type': 'back-lay',
'odds': 1.40322,
'odds-type': 'DECIMAL',
'side': 'back'},
{'available-amount': 710.74,
'currency': 'GBP',
'decimal-odds': 1.5814,
'exchange-type': 'back-lay',
'odds': 1.5814,
'odds-type': 'DECIMAL',
'side': 'lay'},
{'available-amount': 163.74192,
'currency': 'GBP',
'decimal-odds': 1.76336,
'exchange-type': 'back-lay',
'odds': 1.76336,
'odds-type': 'DECIMAL',
'side': 'lay'},
{'available-amount': 151.55072,
'currency': 'GBP',
'decimal-odds': 1.9434,
'exchange-type': 'back-lay',
'odds': 1.9434,
'odds-type': 'DECIMAL',
'side': 'lay'}],
'status': 'open',
'volume': 6955.72965},
{'event-id': 488243069570009,
'event-participant-id': 488243069660010,
'id': 488243069890010,
'market-id': 488243069830010,
'name': 'A Bedene',
'prices': [{'available-amount': 413.22093,
'currency': 'GBP',
'decimal-odds': 2.72,
'exchange-type': 'back-lay',
'odds': 2.72,
'odds-type': 'DECIMAL',
'side': 'back'},
{'available-amount': 124.99383,
'currency': 'GBP',
'decimal-odds': 2.31,
'exchange-type': 'back-lay',
'odds': 2.31,
'odds-type': 'DECIMAL',
'side': 'back'},
{'available-amount': 142.97238,
'currency': 'GBP',
'decimal-odds': 2.06,
'exchange-type': 'back-lay',
'odds': 2.06,
'odds-type': 'DECIMAL',
'side': 'back'},
{'available-amount': 25.75872,
'currency': 'GBP',
'decimal-odds': 2.88,
'exchange-type': 'back-lay',
'odds': 2.88,
'odds-type': 'DECIMAL',
'side': 'lay'},
{'available-amount': 244.07,
'currency': 'GBP',
'decimal-odds': 2.94,
'exchange-type': 'back-lay',
'odds': 2.94,
'odds-type': 'DECIMAL',
'side': 'lay'},
{'available-amount': 39.06352,
'currency': 'GBP',
'decimal-odds': 3.48,
'exchange-type': 'back-lay',
'odds': 3.48,
'odds-type': 'DECIMAL',
'side': 'lay'}],
'status': 'open',
'volume': 16033.12548}],
'start': '2017-05-15T11:05:00.000Z',
'status': 'open',
'type': 'binary',
'volume': 22988.85514}],
'meta-tags': [{'id': 9,
'name': 'Tennis',
'type': 'SPORT',
'url-name': 'tennis'},
{'id': 297063445660036,
'name': 'R1',
'type': 'DATE',
'url-name': 'r1'},
{'id': 410444698700010,
'name': 'Italy',
'type': 'COUNTRY',
'url-name': 'italy'},
{'id': 410468520880009,
'name': 'Live Betting',
'type': 'OTHER',
'url-name': 'live-betting'},
{'id': 477274740480010,
'name': 'May 15th 2017',
'type': 'DATE',
'url-name': 'may-15th-2017'},
{'id': 486941505790009,
'name': 'ATP Rome',
'type': 'COMPETITION',
'url-name': 'ATP-Rome'}],
'name': 'G Mager vs A Bedene',
'sport-id': 9,
'start': '2017-05-15T11:05:00.000Z',
'status': 'open',
'volume': 23098.72333},
'offset': 0,
'per-page': 20,
'total': 223}
I want to find the event-id within this data and print "'event-id': 488243069570009"
I managed to do this with:
r11 = s.get('https://api.matchbook.com/edge/rest/events')
r12 = r11.json()
for each in r12['events'][0]['markets']:
print(each['event-id'])
but it only prints the first event-id on the complete set of json data
You can simply create a new array using shorthand
data = {"offset":0,"per-page":20,"sports":[{"id":112,"name":"Australian Rules","type":"SPORT"},{"id":13,"name":"Auto Racing","type":"SPORT"},{"id":3,"name":"Baseball","type":"SPORT"},{"id":4,"name":"Basketball","type":"SPORT"},{"id":14,"name":"Boxing","type":"SPORT"},{"id":110,"name":"Cricket","type":"SPORT"},{"id":11,"name":"Current Events","type":"SPORT"},{"id":115,"name":"Cycling","type":"SPORT"},{"id":116,"name":"Darts","type":"SPORT"},{"id":117,"name":"Gaelic Football","type":"SPORT"},{"id":8,"name":"Golf","type":"SPORT"},{"id":241798357140019,"name":"Greyhound Racing","type":"SPORT"},{"id":24735152712200,"name":"Horse Racing","type":"SPORT"},{"id":222109340250019,"name":"Horse Racing (Ante Post)","type":"SPORT"},{"id":231138347942400,"name":"Horse Racing Beta","type":"SPORT"},{"id":118,"name":"Hurling","type":"SPORT"},{"id":6,"name":"Ice Hockey","type":"SPORT"},{"id":126,"name":"MMA","type":"SPORT"},{"id":5,"name":"NCAA Basketball","type":"SPORT"},{"id":2,"name":"NCAA Football","type":"SPORT"}],"total":28}
# print(data['sports'])
# for element in data['sports']:
# del element['id']
# del element['type']
# print(data['sports'])
new_arr = [_['name'] for _ in data['sports']];
print(new_arr)
UPDATE
Check the online demo
You can't do what you're asking the way you're asking. The matchbook API just doesn't work like that.
You could do something silly, like add a proxy server in between you and their API that does the filtering for you, so you aren't the one doing the filtering, it's the proxy, but that's just playing with the semantics.
Agam Banga's example does exactly what you stated you want:
how would I just show the names of those sports, filtering out the id and types?
Here's a full example that you can literally copy, paste, and run, and it will print out all the sports that matchbook supports:
import requests
r = requests.get('https://api.matchbook.com/edge/rest/lookups/sports')
names = [_['name'] for _ in r.json()['sports']]
for name in names:
print('Sport Name:', name)
I ran it, and here's what it output:
Sport Name: Australian Rules
Sport Name: Auto Racing
Sport Name: Baseball
Sport Name: Basketball
Sport Name: Boxing
Sport Name: Cricket
Sport Name: Current Events
Sport Name: Cycling
Sport Name: Darts
Sport Name: Gaelic Football
Sport Name: Golf
Sport Name: Greyhound Racing
Sport Name: Horse Racing
Sport Name: Horse Racing (Ante Post)
Sport Name: Horse Racing Beta
Sport Name: Hurling
Sport Name: Ice Hockey
Sport Name: MMA
Sport Name: NCAA Basketball
Sport Name: NCAA Football