Last active
May 30, 2018 23:31
-
-
Save vsoch/76d8933e3ff7e080883362b8baa4a164 to your computer and use it in GitHub Desktop.
Expfactory Result JSON Extraction Examples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# parse.py | |
# Run this script on downloaded .json files to extract the "inner" json for an experiment, | |
# given that the experiment exports data in json. You can do this for one or more files at once | |
# after you download the script, and python 2 and python 3 should be supported. | |
# The result will be written in the present working directory with a file named equivalently | |
# prefixed with "parsed." For example: | |
# stroop-results.json --> parsed_stroop-results.json | |
# Usage | |
# python parse.py stroop-results.json | |
# python parse.py stroop-results.json go-no-go-task-results.json | |
import json | |
import sys | |
import os | |
def usage(): | |
print('Usage Examples:') | |
print('python parse.py stroop.json') | |
print('python parse.py stroop.json feedback.json') | |
if len(sys.argv) < 2: | |
print('Please provide the json files to parse!') | |
usage() | |
sys.exit(1) | |
json_files = sys.argv[1:] | |
for json_file in json_files: | |
if os.path.exists(json_file): | |
# We load the initial json, which has one key "data" | |
with open(json_file , 'r') as fh: | |
content = json.load(fh) | |
# Then we load the "data" again with json. We do this because | |
# it could be the case that the experiment doesn't export json! | |
parsed = json.loads(content['data']) | |
# Save to file, the same name prefixed with "parsed" | |
json_output = "parsed_%s" % os.path.basename(json_file) | |
print('Writing output to %s' %json_output) | |
with open(json_output, 'w') as fh: | |
fh.writelines(json.dumps(parsed, sort_keys=True, indent=4)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Here is an equivalent (interactive) version to parse with R, and two methods (using rjson and jsonlite) are shown. | |
install.packages('rjson') | |
install.packages('jsonlite') | |
# Load the package required to read JSON files. | |
library("rjson") | |
library("jsonlite") | |
# Give the input file name to the function. | |
result <- rjson::fromJSON(file = "stroop-results.json") | |
# This needs to be loaded again. | |
print(result[['data']]) | |
# This method generates a data structure | |
rjson::fromJSON(result[['data']]) | |
# Here we load again with jsonlite, more of a data frame! | |
jsonlite::fromJSON(result[['data']]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment