Skip to content

Instantly share code, notes, and snippets.



Last active May 30, 2018
What would you like to do?
Expfactory Result JSON Extraction Examples
#!/usr/bin/env python
# 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 stroop-results.json
# python stroop-results.json go-no-go-task-results.json
import json
import sys
import os
def usage():
print('Usage Examples:')
print('python stroop.json')
print('python stroop.json feedback.json')
if len(sys.argv) < 2:
print('Please provide the json files to parse!')
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))
# Here is an equivalent (interactive) version to parse with R, and two methods (using rjson and jsonlite) are shown.
# Load the package required to read JSON files.
# Give the input file name to the function.
result <- rjson::fromJSON(file = "stroop-results.json")
# This needs to be loaded again.
# This method generates a data structure
# Here we load again with jsonlite, more of a data frame!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.