Python - Get Nested Data from Multiple Levels - python
Wasn't sure how to title this question but I am working with the Quickbooks Online API and when querying a report like BalanceSheet or GeneralLedger the API returns data rows in multiple nested levels which is quite frustrating to parse through.
Example of the BalanceSheet return included below. I am only interested in the data from "Row" objects but as you can see that can be returned in 1, 2, 3 or more different levels of data. I am thinking of going through each level to check for Rows and then get each Row but that seems overly complex as I would need multiple for loops for each level.
I'm wondering if there is a better way to get each "Row" in that data without regard to which level it is on? Any ideas would be appreciated!
Here's an example of a return from their sandbox data:
{
"Header": {
"Time": "2021-04-28T14:12:17-07:00",
"ReportName": "BalanceSheet",
"DateMacro": "this calendar year-to-date",
"ReportBasis": "Accrual",
"StartPeriod": "2021-01-01",
"EndPeriod": "2021-04-28",
"SummarizeColumnsBy": "Month",
"Currency": "USD",
"Option": [
{
"Name": "AccountingStandard",
"Value": "GAAP"
},
{
"Name": "NoReportData",
"Value": "false"
}
]
},
"Columns": {
"Column": [
{
"ColTitle": "",
"ColType": "Account",
"MetaData": [
{
"Name": "ColKey",
"Value": "account"
}
]
},
{
"ColTitle": "Jan 2021",
"ColType": "Money",
"MetaData": [
{
"Name": "StartDate",
"Value": "2021-01-01"
},
{
"Name": "EndDate",
"Value": "2021-01-31"
},
{
"Name": "ColKey",
"Value": "Jan 2021"
}
]
},
{
"ColTitle": "Feb 2021",
"ColType": "Money",
"MetaData": [
{
"Name": "StartDate",
"Value": "2021-02-01"
},
{
"Name": "EndDate",
"Value": "2021-02-28"
},
{
"Name": "ColKey",
"Value": "Feb 2021"
}
]
},
{
"ColTitle": "Mar 2021",
"ColType": "Money",
"MetaData": [
{
"Name": "StartDate",
"Value": "2021-03-01"
},
{
"Name": "EndDate",
"Value": "2021-03-31"
},
{
"Name": "ColKey",
"Value": "Mar 2021"
}
]
},
{
"ColTitle": "Apr 1-28, 2021",
"ColType": "Money",
"MetaData": [
{
"Name": "StartDate",
"Value": "2021-04-01"
},
{
"Name": "EndDate",
"Value": "2021-04-28"
},
{
"Name": "ColKey",
"Value": "Apr 1-28, 2021"
}
]
}
]
},
"Rows": {
"Row": [
{
"Header": {
"ColData": [
{
"value": "ASSETS"
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
}
]
},
"Rows": {
"Row": [
{
"Header": {
"ColData": [
{
"value": "Current Assets"
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
}
]
},
"Rows": {
"Row": [
{
"Header": {
"ColData": [
{
"value": "Bank Accounts"
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
}
]
},
"Rows": {
"Row": [
{
"ColData": [
{
"value": "Checking",
"id": "35"
},
{
"value": "1201.00"
},
{
"value": "1201.00"
},
{
"value": "1201.00"
},
{
"value": "1201.00"
}
],
"type": "Data"
},
{
"ColData": [
{
"value": "Savings",
"id": "36"
},
{
"value": "800.00"
},
{
"value": "800.00"
},
{
"value": "800.00"
},
{
"value": "800.00"
}
],
"type": "Data"
}
]
},
"Summary": {
"ColData": [
{
"value": "Total Bank Accounts"
},
{
"value": "2001.00"
},
{
"value": "2001.00"
},
{
"value": "2001.00"
},
{
"value": "2001.00"
}
]
},
"type": "Section",
"group": "BankAccounts"
},
{
"Header": {
"ColData": [
{
"value": "Accounts Receivable"
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
}
]
},
"Rows": {
"Row": [
{
"ColData": [
{
"value": "Accounts Receivable (A/R)",
"id": "84"
},
{
"value": "5281.52"
},
{
"value": "5281.52"
},
{
"value": "5281.52"
},
{
"value": "5281.52"
}
],
"type": "Data"
}
]
},
"Summary": {
"ColData": [
{
"value": "Total Accounts Receivable"
},
{
"value": "5281.52"
},
{
"value": "5281.52"
},
{
"value": "5281.52"
},
{
"value": "5281.52"
}
]
},
"type": "Section",
"group": "AR"
},
{
"Header": {
"ColData": [
{
"value": "Other Current Assets"
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
}
]
},
"Rows": {
"Row": [
{
"ColData": [
{
"value": "Inventory Asset",
"id": "81"
},
{
"value": "596.25"
},
{
"value": "596.25"
},
{
"value": "596.25"
},
{
"value": "596.25"
}
],
"type": "Data"
},
{
"ColData": [
{
"value": "Undeposited Funds",
"id": "4"
},
{
"value": "2062.52"
},
{
"value": "2062.52"
},
{
"value": "2062.52"
},
{
"value": "2062.52"
}
],
"type": "Data"
}
]
},
"Summary": {
"ColData": [
{
"value": "Total Other Current Assets"
},
{
"value": "2658.77"
},
{
"value": "2658.77"
},
{
"value": "2658.77"
},
{
"value": "2658.77"
}
]
},
"type": "Section",
"group": "OtherCurrentAssets"
}
]
},
"Summary": {
"ColData": [
{
"value": "Total Current Assets"
},
{
"value": "9941.29"
},
{
"value": "9941.29"
},
{
"value": "9941.29"
},
{
"value": "9941.29"
}
]
},
"type": "Section",
"group": "CurrentAssets"
},
{
"Header": {
"ColData": [
{
"value": "Fixed Assets"
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
}
]
},
"Rows": {
"Row": [
{
"Header": {
"ColData": [
{
"value": "Truck",
"id": "37"
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
}
]
},
"Rows": {
"Row": [
{
"ColData": [
{
"value": "Original Cost",
"id": "38"
},
{
"value": "13495.00"
},
{
"value": "13495.00"
},
{
"value": "13495.00"
},
{
"value": "13495.00"
}
],
"type": "Data"
}
]
},
"Summary": {
"ColData": [
{
"value": "Total Truck"
},
{
"value": "13495.00"
},
{
"value": "13495.00"
},
{
"value": "13495.00"
},
{
"value": "13495.00"
}
]
},
"type": "Section"
}
]
},
"Summary": {
"ColData": [
{
"value": "Total Fixed Assets"
},
{
"value": "13495.00"
},
{
"value": "13495.00"
},
{
"value": "13495.00"
},
{
"value": "13495.00"
}
]
},
"type": "Section",
"group": "FixedAssets"
}
]
},
"Summary": {
"ColData": [
{
"value": "TOTAL ASSETS"
},
{
"value": "23436.29"
},
{
"value": "23436.29"
},
{
"value": "23436.29"
},
{
"value": "23436.29"
}
]
},
"type": "Section",
"group": "TotalAssets"
},
{
"Header": {
"ColData": [
{
"value": "LIABILITIES AND EQUITY"
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
}
]
},
"Rows": {
"Row": [
{
"Header": {
"ColData": [
{
"value": "Liabilities"
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
}
]
},
"Rows": {
"Row": [
{
"Header": {
"ColData": [
{
"value": "Current Liabilities"
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
}
]
},
"Rows": {
"Row": [
{
"Header": {
"ColData": [
{
"value": "Accounts Payable"
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
}
]
},
"Rows": {
"Row": [
{
"ColData": [
{
"value": "Accounts Payable (A/P)",
"id": "33"
},
{
"value": "1602.67"
},
{
"value": "1602.67"
},
{
"value": "1602.67"
},
{
"value": "1602.67"
}
],
"type": "Data"
}
]
},
"Summary": {
"ColData": [
{
"value": "Total Accounts Payable"
},
{
"value": "1602.67"
},
{
"value": "1602.67"
},
{
"value": "1602.67"
},
{
"value": "1602.67"
}
]
},
"type": "Section",
"group": "AP"
},
{
"Header": {
"ColData": [
{
"value": "Credit Cards"
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
}
]
},
"Rows": {
"Row": [
{
"ColData": [
{
"value": "Mastercard",
"id": "41"
},
{
"value": "157.72"
},
{
"value": "157.72"
},
{
"value": "157.72"
},
{
"value": "157.72"
}
],
"type": "Data"
}
]
},
"Summary": {
"ColData": [
{
"value": "Total Credit Cards"
},
{
"value": "157.72"
},
{
"value": "157.72"
},
{
"value": "157.72"
},
{
"value": "157.72"
}
]
},
"type": "Section",
"group": "CreditCards"
},
{
"Header": {
"ColData": [
{
"value": "Other Current Liabilities"
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
}
]
},
"Rows": {
"Row": [
{
"ColData": [
{
"value": "Arizona Dept. of Revenue Payable",
"id": "89"
},
{
"value": "0.00"
},
{
"value": "0.00"
},
{
"value": "0.00"
},
{
"value": "0.00"
}
],
"type": "Data"
},
{
"ColData": [
{
"value": "Board of Equalization Payable",
"id": "90"
},
{
"value": "370.94"
},
{
"value": "370.94"
},
{
"value": "370.94"
},
{
"value": "370.94"
}
],
"type": "Data"
},
{
"ColData": [
{
"value": "Loan Payable",
"id": "43"
},
{
"value": "4000.00"
},
{
"value": "4000.00"
},
{
"value": "4000.00"
},
{
"value": "4000.00"
}
],
"type": "Data"
}
]
},
"Summary": {
"ColData": [
{
"value": "Total Other Current Liabilities"
},
{
"value": "4370.94"
},
{
"value": "4370.94"
},
{
"value": "4370.94"
},
{
"value": "4370.94"
}
]
},
"type": "Section",
"group": "OtherCurrentLiabilities"
}
]
},
"Summary": {
"ColData": [
{
"value": "Total Current Liabilities"
},
{
"value": "6131.33"
},
{
"value": "6131.33"
},
{
"value": "6131.33"
},
{
"value": "6131.33"
}
]
},
"type": "Section",
"group": "CurrentLiabilities"
},
{
"Header": {
"ColData": [
{
"value": "Long-Term Liabilities"
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
}
]
},
"Rows": {
"Row": [
{
"ColData": [
{
"value": "Notes Payable",
"id": "44"
},
{
"value": "25000.00"
},
{
"value": "25000.00"
},
{
"value": "25000.00"
},
{
"value": "25000.00"
}
],
"type": "Data"
}
]
},
"Summary": {
"ColData": [
{
"value": "Total Long-Term Liabilities"
},
{
"value": "25000.00"
},
{
"value": "25000.00"
},
{
"value": "25000.00"
},
{
"value": "25000.00"
}
]
},
"type": "Section",
"group": "LongTermLiabilities"
}
]
},
"Summary": {
"ColData": [
{
"value": "Total Liabilities"
},
{
"value": "31131.33"
},
{
"value": "31131.33"
},
{
"value": "31131.33"
},
{
"value": "31131.33"
}
]
},
"type": "Section",
"group": "Liabilities"
},
{
"Header": {
"ColData": [
{
"value": "Equity"
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
}
]
},
"Rows": {
"Row": [
{
"ColData": [
{
"value": "Opening Balance Equity",
"id": "34"
},
{
"value": "-9337.50"
},
{
"value": "-9337.50"
},
{
"value": "-9337.50"
},
{
"value": "-9337.50"
}
],
"type": "Data"
},
{
"ColData": [
{
"value": "Retained Earnings",
"id": "2"
},
{
"value": "1642.46"
},
{
"value": "1642.46"
},
{
"value": "1642.46"
},
{
"value": "1642.46"
}
],
"type": "Data"
},
{
"ColData": [
{
"value": "Net Income"
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
},
{
"value": ""
}
],
"type": "Data",
"group": "NetIncome"
}
]
},
"Summary": {
"ColData": [
{
"value": "Total Equity"
},
{
"value": "-7695.04"
},
{
"value": "-7695.04"
},
{
"value": "-7695.04"
},
{
"value": "-7695.04"
}
]
},
"type": "Section",
"group": "Equity"
}
]
},
"Summary": {
"ColData": [
{
"value": "TOTAL LIABILITIES AND EQUITY"
},
{
"value": "23436.29"
},
{
"value": "23436.29"
},
{
"value": "23436.29"
},
{
"value": "23436.29"
}
]
},
"type": "Section",
"group": "TotalLiabilitiesAndEquity"
}
]
}
}
Related
Python - trying to convert time from utc to cst in api response
Below is code I am using to get data from an api. And below that is the response. I am trying to convert datetime from UTC to CST and then present the data with that time zone instead. But I am having trouble isolating datetime import requests import json weather = requests.get('...') j = json.loads(weather.text) print (json.dumps(j, indent=2)) Response: { "metadata": null, "data": [ { "datetime": "2022-12-11T05:00:00Z", "is_day_time": false, "icon_code": 5, "weather_text": "Clear with few low clouds and few cirrus", "temperature": { "value": 45.968, "units": "F" }, "feels_like_temperature": { "value": 39.092, "units": "F" }, "relative_humidity": 56, "precipitation": { "precipitation_probability": 4, "total_precipitation": { "value": 0.0, "units": "in" } }, "wind": { "speed": { "value": 5.144953471725125, "units": "mi/h" }, "direction": 25 }, "wind_gust": { "value": 9.014853256979242, "units": "mi/h" }, "pressure": { "value": 29.4171829577118, "units": "inHg" }, "visibility": { "value": 6.835083114610673, "units": "mi" }, "dew_point": { "value": 31.01, "units": "F" }, "cloud_cover": 31 }, { "datetime": "2022-12-11T06:00:00Z", "is_day_time": false, "icon_code": 4, "weather_text": "Clear with few low clouds", "temperature": { "value": 45.068, "units": "F" }, "feels_like_temperature": { "value": 38.066, "units": "F" }, "relative_humidity": 56, "precipitation": { "precipitation_probability": 5, "total_precipitation": { "value": 0.0, "units": "in" } }, "wind": { "speed": { "value": 5.167322834645669, "units": "mi/h" }, "direction": 27 }, "wind_gust": { "value": 8.724051539012168, "units": "mi/h" }, "pressure": { "value": 29.4213171559632, "units": "inHg" }, "visibility": { "value": 5.592340730136005, "units": "mi" }, "dew_point": { "value": 30.2, "units": "F" }, "cloud_cover": 13 }, { "datetime": "2022-12-11T07:00:00Z", "is_day_time": false, "icon_code": 4, "weather_text": "Clear with few low clouds", "temperature": { "value": 44.33, "units": "F" }, "feels_like_temperature": { "value": 37.364, "units": "F" }, "relative_humidity": 56, "precipitation": { "precipitation_probability": 4, "total_precipitation": { "value": 0.0, "units": "in" } }, "wind": { "speed": { "value": 4.988367931281317, "units": "mi/h" }, "direction": 28 }, "wind_gust": { "value": 8.254294917680744, "units": "mi/h" }, "pressure": { "value": 29.4165923579616, "units": "inHg" }, "visibility": { "value": 7.456454306848007, "units": "mi" }, "dew_point": { "value": 29.714, "units": "F" }, "cloud_cover": 22 } ], "error": null
I am assuming what you mean is that you want to present the data in the current time of the Central Time zone. As of the date this question was asked, that would be CST (Central Standard Time). At another time it will be CDT (Central Daylight Time) based on daylight savings time rules that are followed in the Country/City for the time zone for which you wish to localize the data. The rules are all nicely kept in the IANA Timezone Database. So the trick is that you pick your Country/City from the Timezone DB that follows the rules as they apply to your current time zone. For Central Time, America/Chicago usually works but YMMV. There are a lot of ways to do this. This example is inefficiently iterating through the dictionary created by json.loads and replacing the time string with a converted string. The key is using the dateutil library to parse the timestamp string and convert using the proper UTC offset as defined for the time zone in the IANA database. Hopefully this example has enough pieces you can copy and adapt to your own needs. from dateutil.parser import parse from dateutil import tz import json j = json.loads(weather) # Loop through each data entry, reformatting the time for entry in j["data"]: if "datetime" in entry.keys(): parsed_dt = parse(entry["datetime"]) converted = parsed_dt.astimezone(tz.gettz("America/Chicago")) entry["datetime"] = converted.isoformat() print (json.dumps(j, indent=2)) The resulting JSON has datetime fields that contain an ISO timestamp for the CST time. { "metadata": null, "data": [{ "datetime": "2022-12-10T23:00:00-06:00", "is_day_time": false, "icon_code": 5, "weather_text": "Clear with few low clouds and few cirrus", "temperature": { "value": 45.968, "units": "F" }, "feels_like_temperature": { "value": 39.092, "units": "F" }, "relative_humidity": 56, "precipitation": { "precipitation_probability": 4, "total_precipitation": { "value": 0.0, "units": "in" } }, "wind": { "speed": { "value": 5.144953471725125, "units": "mi/h" }, "direction": 25 }, "wind_gust": { "value": 9.014853256979242, "units": "mi/h" }, "pressure": { "value": 29.4171829577118, "units": "inHg" }, "visibility": { "value": 6.835083114610673, "units": "mi" }, "dew_point": { "value": 31.01, "units": "F" }, "cloud_cover": 31 }, { "datetime": "2022-12-11T00:00:00-06:00", "is_day_time": false, "icon_code": 4, "weather_text": "Clear with few low clouds", "temperature": { "value": 45.068, "units": "F" }, "feels_like_temperature": { "value": 38.066, "units": "F" }, "relative_humidity": 56, "precipitation": { "precipitation_probability": 5, "total_precipitation": { "value": 0.0, "units": "in" } }, "wind": { "speed": { "value": 5.167322834645669, "units": "mi/h" }, "direction": 27 }, "wind_gust": { "value": 8.724051539012168, "units": "mi/h" }, "pressure": { "value": 29.4213171559632, "units": "inHg" }, "visibility": { "value": 5.592340730136005, "units": "mi" }, "dew_point": { "value": 30.2, "units": "F" }, "cloud_cover": 13 }, { "datetime": "2022-12-11T01:00:00-06:00", "is_day_time": false, "icon_code": 4, "weather_text": "Clear with few low clouds", "temperature": { "value": 44.33, "units": "F" }, "feels_like_temperature": { "value": 37.364, "units": "F" }, "relative_humidity": 56, "precipitation": { "precipitation_probability": 4, "total_precipitation": { "value": 0.0, "units": "in" } }, "wind": { "speed": { "value": 4.988367931281317, "units": "mi/h" }, "direction": 28 }, "wind_gust": { "value": 8.254294917680744, "units": "mi/h" }, "pressure": { "value": 29.4165923579616, "units": "inHg" }, "visibility": { "value": 7.456454306848007, "units": "mi" }, "dew_point": { "value": 29.714, "units": "F" }, "cloud_cover": 22 } ], "error": null }
Extract data from JSON index loaded file
My JSON file looks like: { "numAccounts": xxxx, "filtersApplied": { "accountIds": "All", "checkIds": "All", "categories": [ "cost_optimizing" ], "statuses": "All", "regions": "All", "organizationalUnitIds": [ "yyyyy" ] }, "categoryStatusMap": { "cost_optimizing": { "statusMap": { "RULE_ERROR": { "name": "Blue", "count": 11 }, "ERROR": { "name": "Red", "count": 11 }, "OK": { "name": "Green", "count": 11 }, "WARN": { "name": "Yellow", "count": 11 } }, "name": "Cost Optimizing", "monthlySavings": 1111 } }, "accountStatusMap": { "xxxxxxxx": { "cost_optimizing": { "statusMap": { "OK": { "name": "Green", "count": 1111 }, "WARN": { "name": "Yellow", "count": 111 } }, "name": "Cost Optimizing", "monthlySavings": 1111 } }, Which I load into memory using pandas: df = pd.read_json('file.json', orient='index') I find the index orient the most suitable because it gives me: print(df) 0 numAccounts 125 filtersApplied {'accountIds': 'All', 'checkIds': 'All', 'cate... categoryStatusMap {'cost_optimizing': {'statusMap': {'RULE_ERROR... accountStatusMap {'xxxxxxx': {'cost_optimizing': {'statusM... Now, how can I access the accountStatusMap entry? I tried account_status_map = df['accountStatusMap'] which gives me a KeyError: 'accountStatusMap' Is there something specific to the index orientation in how to access specific entries in a dataframe?
How to group child objects in python list
I have the parent child structure like following which is generated from the database entries by iterating rows data and append childrens "path_data": [ { "action": "PAGE_VIEW", "children": [ { "action": "PAGE_VIEW", "children": [ { "action": "PAGE_VIEW", "children": [ { "action": "PAGE_VIEW", "children": [ { "action": "PAGE_VIEW", "children": [ { "action": "PAGE_VIEW", "children": [], "count": 1, "id": "", "key": "0", "name": "Untagged" } ], "count": 1, "id": "", "key": "0", "name": "Untagged" } ], "count": 1, "id": "", "key": "0", "name": "Untagged" } ], "count": 1, "id": "", "key": "0", "name": "Untagged" } ], "count": 1, "id": "", "key": "0", "name": "Untagged" } ], "count": 1, "id": 1, "key": "0", "name": "Home" }, { "action": "PAGE_VIEW", "children": [ { "action": "PAGE_VIEW", "children": [ { "action": "PAGE_VIEW", "children": [ { "action": "PAGE_VIEW", "children": [ { "action": "PAGE_VIEW", "children": [ { "action": "PAGE_VIEW", "children": [], "count": 1, "id": "", "key": "0", "name": "Untagged" } ], "count": 1, "id": "", "key": "0", "name": "Untagged" } ], "count": 1, "id": "", "key": "0", "name": "Untagged" } ], "count": 1, "id": "", "key": "0", "name": "Untagged" } ], "count": 1, "id": 2, "key": "0", "name": "About Us" } ], "count": 1, "id": 1, "key": "0", "name": "Home" }, { "action": "PAGE_VIEW", "children": [ { "action": "PAGE_VIEW", "children": [ { "action": "PRODUCT_VIEW", "children": [ { "action": "PAGE_VIEW", "children": [], "count": 1, "id": 1, "key": "0", "name": "Home" } ], "count": 1, "id": "", "key": "0", "name": "Untagged" } ], "count": 1, "id": 2, "key": "0", "name": "About Us" } ], "count": 1, "id": 1, "key": "0", "name": "Home" }, { "action": "PAGE_VIEW", "children": [ [] ], "count": 1, "id": 1, "key": "0", "name": "Home" } ], Expected output need to convert this by grouping the same level child & increase count of it like following. "path_data": [ { "action": "PAGE_VIEW", "children": [ { "action": "PAGE_VIEW", "children": [ { "action": "PAGE_VIEW", "children": [ { "action": "PAGE_VIEW", "children": [ { "action": "PAGE_VIEW", "children": [ { "action": "PAGE_VIEW", "children": [], "count": 1, "id": "", "key": "0", "name": "Untagged" } ], "count": 1, "id": "", "key": "0", "name": "Untagged" } ], "count": 1, "id": "", "key": "0", "name": "Untagged" } ], "count": 1, "id": "", "key": "0", "name": "Untagged" } ], "count": 1, "id": "", "key": "0", "name": "Untagged" },{ "action": "PAGE_VIEW", "children": [ { "action": "PAGE_VIEW", "children": [ { "action": "PAGE_VIEW", "children": [ { "action": "PAGE_VIEW", "children": [ { "action": "PAGE_VIEW", "children": [], "count": 1, "id": "", "key": "0", "name": "Untagged" } ], "count": 1, "id": "", "key": "0", "name": "Untagged" } ], "count": 1, "id": "", "key": "0", "name": "Untagged" }, { "action": "PAGE_VIEW", "children": [], "count": 1, "id": 1, "key": "0", "name": "Home" } ], "count": 2, "id": "", "key": "0", "name": "Untagged" } ], "count": 2, "id": 2, "key": "0", "name": "About Us" } ], "count": 4, "id": 1, "key": "0", "name": "Home" }, ], Is there any inbuilt functions or library to do this? This code is written in python 3.8
Dictionary data is not seperated into columns in Pandas DataFrame
I have created a variable that stores my json data. It looks like this: datasett = ''' { "data": { "trafficRegistrationPoints": [ { "id": "99100B1687283", "name": "Menstad sykkeltellepunkt", "location": { "coordinates": { "latLon": { "lat": 59.173876, "lon": 9.641772 } } } }, { "id": "11101B1800681", "name": "Garpa - sykkel", "location": { "coordinates": { "latLon": { "lat": 63.795114, "lon": 11.494511 } } } }, { "id": "30961B1175469", "name": "STENMALEN-SYKKEL", "location": { "coordinates": { "latLon": { "lat": 59.27665, "lon": 10.411814 } } } }, { "id": "53749B1700621", "name": "TUNEVANNET SYKKEL", "location": { "coordinates": { "latLon": { "lat": 59.292846, "lon": 11.084058 } } } }, { "id": "80565B1689290", "name": "Nenset sykkeltellepunkt", "location": { "coordinates": { "latLon": { "lat": 59.168377, "lon": 9.634257 } } } }, { "id": "24783B2045151", "name": "Orstad sykkel- begge retn.", "location": { "coordinates": { "latLon": { "lat": 58.798377, "lon": 5.72743 } } } }, { "id": "46418B2616452", "name": "Elgeseter bru sykkel øst", "location": { "coordinates": { "latLon": { "lat": 63.425015, "lon": 10.393928 } } } }, { "id": "35978B1700571", "name": "Tune kirke nord", "location": { "coordinates": { "latLon": { "lat": 59.292626, "lon": 11.084066 } } } }, { "id": "21745B1996708", "name": "Munkedamsveien Sykkel", "location": { "coordinates": { "latLon": { "lat": 59.911198, "lon": 10.725568 } } } }, { "id": "33443B2542097", "name": "KANALBRUA-SYKKEL", "location": { "coordinates": { "latLon": { "lat": 59.261823, "lon": 10.416349 } } } }, { "id": "77570B384357", "name": "HAVRENESVEGEN (SYKKEL)", "location": { "coordinates": { "latLon": { "lat": 61.598202, "lon": 5.016999 } } } }, { "id": "95959B971385", "name": "JELØGATA SYKKEL", "location": { "coordinates": { "latLon": { "lat": 59.43385, "lon": 10.65388 } } } }, { "id": "61523B971803", "name": "ST.HANSFJELLET SYKKEL", "location": { "coordinates": { "latLon": { "lat": 59.218978, "lon": 10.93455 } } } }, } } } ] } } ''' Next, I have used json.loads() to turn it into a dictionary in Python, using the following code: dict = json.loads(datasett) Because the result I get is a nested dictionary,I we want to move further into the nest. movedDict = dict['data'] I then want to this into a Pandas DataFrame df = pd.DataFrame.from_dict(movedDict) However, when I print this. The data is not seperated into unique columns. What do I do wrong?
You can use json_normalize here, I also removed some extra } from your JSON: data = json.loads(datasett) df = pd.json_normalize(data, record_path=['data', 'trafficRegistrationPoints']) print(df) id name location.coordinates.latLon.lat location.coordinates.latLon.lon 0 99100B1687283 Menstad sykkeltellepunkt 59.173876 9.641772 1 11101B1800681 Garpa - sykkel 63.795114 11.494511 2 30961B1175469 STENMALEN-SYKKEL 59.276650 10.411814 3 53749B1700621 TUNEVANNET SYKKEL 59.292846 11.084058 4 80565B1689290 Nenset sykkeltellepunkt 59.168377 9.634257 5 24783B2045151 Orstad sykkel- begge retn. 58.798377 5.727430 6 46418B2616452 Elgeseter bru sykkel øst 63.425015 10.393928 7 35978B1700571 Tune kirke nord 59.292626 11.084066 8 21745B1996708 Munkedamsveien Sykkel 59.911198 10.725568 9 33443B2542097 KANALBRUA-SYKKEL 59.261823 10.416349 10 77570B384357 HAVRENESVEGEN (SYKKEL) 61.598202 5.016999 11 95959B971385 JELØGATA SYKKEL 59.433850 10.653880 12 61523B971803 ST.HANSFJELLET SYKKEL 59.218978 10.934550
when use from_dict the dict should look like this: data = {'col_1': [3, 2, 1, 0], 'col_2': ['a', 'b', 'c', 'd']} pd.DataFrame.from_dict(data) col_1 col_2 0 3 a 1 2 b 2 1 c 3 0 d in your case: data = {'trafficRegistrationPoints':[.....]} save the 'trafficRegistrationPoints' as a list and then create the dataFrame
The values for the data key in your dict are not individual dicts but rather a list of dicts under trafficRegistrationPoints key, so you need to move further into that key: df = pd.DataFrame.from_dict(movedDict['trafficRegistrationPoints'])
Combine 2 JSON files into 1 file in Node or Python (i.e. longitude and latitude)
I want to append the longitude to a latitude stored in 2 separated json files The result should be stored in a 3rd file How can I do that on Python OR Javascript/Node? Many thanks for your support, LATITUDE { "tags": [{ "name": "LATITUDE_deg", "results": [{ "groups": [{ "name": "type", "type": "number" }], "values": [ [1123306773000, 46.9976859318, 3], [1123306774000, 46.9976859319, 3] ], "attributes": { "customer": ["Acme"], "host": ["server1"] } }], "stats": { "rawCount": 2 } }] } LONGITUDE { "tags": [{ "name": "LONGITUDE_deg", "results": [{ "groups": [{ "name": "type", "type": "number" }], "values": [ [1123306773000, 36.9976859318, 3], [1123306774000, 36.9976859317, 3] ], "attributes": { "customer": ["Acme"], "host": ["server1"] } }], "stats": { "rawCount": 2 } }] } Expected result: LATITUDE_AND_LONGITUDE { "tags": [{ "name": "LATITUDE_AND_LONGITUDE_deg", "results": [{ "groups": [{ "name": "type", "type": "number" }], "values": [ [1123306773000, 46.9976859318, 36.9976859318, 3], [1123306774000, 46.9976859319, 36.9976859317, 3] ], "attributes": { "customer": ["Acme"], "host": ["server1"] } }], "stats": { "rawCount": 2 } }] }
I have written the solution with a colleague, find the source code on github: https://gist.github.com/Abdelkrim/715eb222cc318219196c8be293c233bf