Issues with .JSON file conversion and CSV manipulation in Python

Tiffany

sorry for the long post! I'm a bit Python-illiterate, so please bear with me:

I am working on a project that uses extracted Fitbit resting heart-rate data to compare heart-rate values between a series of years. The fitbit data exports as a .json file that I am attempting to convert to .csv for further analysis. I pulled a script from github that converts .json files to .csv-formatted files, however when inputing the resting heart rate data I am running into a few troubles.

Sample lines from .json:

[{
"dateTime" : "09/30/16 00:00:00",
"value" : {
"date" : "09/30/16",
"value" : 76.83736383927637,
"error" : 2.737363838373737
}

Section of GitHub code that transforms nested frame into columns:

# reading json into dataframes
resting_hr_df = get_json_to_df(file_list=resting_hr_file_list).reset_index()

# Heart rate contains a sub json that are explicitly converted into column
resting_hr_df['date'] = resting_hr_df['value'].transform(lambda x: make_new_df_value(x, 'date'))
resting_hr_df['value'] = resting_hr_df['value'].transform(lambda x: make_new_df_value(x, 'value'))
resting_hr_df['error'] = resting_hr_df['value'].transform(lambda x: make_new_df_value(x, 'error'))
resting_hr_df = resting_hr_df.drop(['value', 'index'], axis=1)

There are two variables named 'value' and I think this is causing the issue.

When using the transform function in pandas to assign variable names for the nested dataframe keys, the second ‘value’ values store as 0 in the .csv file. How should I store the values?

RJ Adriaansen

The problem is that this is a nested json file. The solution is to load the json file with json and then load it into pandas with json_normalize

import json
import pandas as pd

with open('filename.json') as data_file:    
    data = json.load(data_file)  

resting_hr_df = pd.json_normalize(data)
resting_hr_df

Output resting_hr_df:

|    | dateTime          | value.date   |   value.value |   value.error |
|---:|:------------------|:-------------|--------------:|--------------:|
|  0 | 09/30/16 00:00:00 | 09/30/16     |       76.8374 |       2.73736 |

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Opening A large JSON file in Python with no newlines for csv conversion Python 2.6.6

From Dev

datetime conversion and manipulation in python

From Dev

CSV Manipulation in python lists

From Dev

Python 3 - Read csv file with type conversion

From Dev

File Manipulation in Python

From Dev

binary file manipulation in python

From Dev

JSON conversion and app compatibility issues

From Dev

JSON conversion and app compatibility issues

From Dev

Issues with exporting a csv file

From Dev

Python issues output. String manipulation

From Dev

UnicodeEncodeError in Python CSV manipulation script

From Dev

manipulation of csv format in python files

From Dev

Parquet file to CSV conversion

From Dev

Parquet file to CSV conversion

From Dev

Json to CSV Conversion

From Dev

Facing issues when writing multiple rows to a csv file in python 3.6

From Dev

Python 3 Encoding Issues when converting to CSV from JSON

From Dev

python csv contactenation issues

From Dev

CSV to JSON python - error in created JSON file

From Dev

String Manipulation To Get An Item In The Middle Of A CSV File

From Dev

perl regex CSV file and column header manipulation

From Dev

python - xml to csv conversion

From Dev

Downloading GitHub issues to .csv file

From Dev

Issues in converting CSV file in to XLS

From Dev

Bulk Insert issues with CSV file

From Dev

JSON TO CSV Conversion Notice: Array to string conversion in

From Dev

Celsius to Fahrenheit conversion in CSV file

From Dev

convert whole csv to json file- python

From Dev

converting json file to csv in Python returns nothing?