Jatos export results in JSON format
We use jsPsych to run our experiments on a JATOS server. jsPsych outputs results in JSON form, which is more robust than using CSV since there is no conversion to CSV. But when we export all the results as a single file from JATOS, the JSON file is unreadable using R's fromJSON function from jsonlite . Importing a single participants data works perfectly. Does anyone know of a way around this or would this require changing JATOS itself? Or is there something we can do on the jsPsych side? Having output in JSON becomes unusable if we have to save each file individually.

Comments
Hi Amunn,
No, there's no need to save each file individually, you just need a little bit of data wrangling in R.
There are a couple of old posts asking similar questions, I couldn't find them just now. Could you post the error message you get when trying to use
fromJSON?Here's an example for reading JSON data from several participants. It wasn't jsPsych, but if you say that reading a single participant works, this might already help, or at least give you a hint to where to begin.
#load necessary libraries library(jsonlite) #reads in json strings library(ggplot2) library(Hmisc) library(here) library(tidyverse) # Take all exported text files from JATOS. # This can either be a single .txt file with all the study results exported at once # Or a list of individual results # They can come from either the component or the study results view, but they should contain data from the task component alonedataFiles <- list.files(path = here(), pattern = ".txt") allJSONs <- readChar(here(dataFiles), file.info(here(dataFiles))$size) singleJSONs <- Hmisc::string.break.line(allJSONs) nSubj <- length(singleJSONs[[1]]) for (subj in 1:nSubj) { if (length(dataFiles) == 1) { resultData <- jsonlite::fromJSON(singleJSONs[[1]][subj]) } else { resultData <- jsonlite::fromJSON(here(dataFiles[subj])) } # Do whatever you need with your data }Hope this helps, otherwise feel free to send a minimal version of your data and the code you're using to read it. I'll try to have a look at it
Best
Elisa
Thanks, Elisa. This didn't seem to do the trick for me. Here's a sample output file from a test experiment containing two runs:
[{"response":{"test_date":"2022-07-24","experimenter_name":"Sara"},"trial_type":"survey-html-form","trial_index":0,"subject_id":"1","test_date":"2022-07-24","experimenter_name":"Sara"},{"stimulus":"<p>What about this</p>","response":0,"trial_type":"html-button-response","trial_index":1,"subject_id":"1","test_date":"2022-07-24","experimenter_name":"Sara"},{"stimulus":"<p>What about that</p>","response":1,"trial_type":"html-button-response","trial_index":2,"subject_id":"1","test_date":"2022-07-24","experimenter_name":"Sara"},{"stimulus":"The task is over","response":" ","trial_type":"html-keyboard-response","trial_index":3,"subject_id":"1","test_date":"2022-07-24","experimenter_name":"Sara"}] [{"response":{"test_date":"2022-07-24","experimenter_name":"Alan"},"trial_type":"survey-html-form","trial_index":0,"subject_id":"1","test_date":"2022-07-24","experimenter_name":"Alan"},{"stimulus":"<p>What about this</p>","response":0,"trial_type":"html-button-response","trial_index":1,"subject_id":"1","test_date":"2022-07-24","experimenter_name":"Alan"},{"stimulus":"<p>What about that</p>","response":0,"trial_type":"html-button-response","trial_index":2,"subject_id":"1","test_date":"2022-07-24","experimenter_name":"Alan"},{"stimulus":"The task is over","response":" ","trial_type":"html-keyboard-response","trial_index":3,"subject_id":"1","test_date":"2022-07-24","experimenter_name":"Alan"}]If I try to simply load this file using
fromJSON('test.txt')I get the following error:Error in parse_con(txt, bigint_as_char) : parse error: trailing garbage "experimenter_name":"Sara"}] [{"response":{"test_date":"2022 (right here) ------^When I apply your R code to this, the resulting dataframe has 4 observations of 7 variables, instead of 8 observations of 7 variables.
I've written a python script to convert the data to a format that
fromJSONcan read. It can be found [here](https://github.com/amunn/fixjatos) but I'd be interested in a pure R solution too.Thanks
Alan
Hi Alan,
I had a look at your GitHub page. I not too familiar with python but that seems like an overkill! Here is a solution in R that takes about 3 lines of code
library(stringr) library(readtext) library(jsonlite) library(here) #not strictly necessary. this is for me to load the file data <- readtext(here("testData.txt")) #This is wrong and reproduces your error fromJSON(data$text) data_better <- str_replace(data$text, fixed("}]\n[{"), "},{") #This is correct (as far as I can tell) and gives you the 8 observations of 7 columns/variables data_df <- fromJSON(data_better) dim(data_df)Hope this helps but, again, do let me know if this doesn't solve your problem. You might need to include an additional replacement for those cases where you don't have a line break between the end and start of the square brackets.
Best
Elisa
Hi Elisa, this helps a lot!
And I agree that the python code is overkill, but I also find that snippets of R code are harder to use over and over, and since I like reusable code, it works for me especially since I need tools that can be distributed to students in my lab. I guess I need to learn how to make an R package.
What would be even nicer is if JATOS could output the collected runs as valid JSON itself, so that no post-processing is needed whether in R or through a script.
Alan
Hi amunn,
What would be even nicer is if JATOS could output the collected runs as valid JSON itself, so that no post-processing is needed whether in R or through a script.
The thing is that JATOS does not know about the format of your result data. They might be CSV, XML, or JSON (or any other text format). Some people already encoded binary data with base64 to text. It would be no problem for JATOS to turn the format you are using in your experiment (one JSON object per line or JSONL / NDJSON) into JSON (just add [ and ] in the beginning and end and a comma instead of line-breaks), but JATOS does not know that you have JSON. Actually, in other cases like the study log, JATOS does this already.
All that said we might add a auto-recognition mechanism to JATOS in one of the next releases. We are discussing this currently whether that makes sense. If we decide to implement this feature JATOS will recognize the JSON format of your single result data and auto-convert the JSONL format of the current result data export file into proper JSON.
Best,
Kristian
Hi Kristian,
I appreciate the problem, and I don't even think that autodetection is necessary. It would help even to have this as an export option chosen by the user when results are exported. I don't know if that would be an easier solution, but it would certainly be perfectly acceptable. Or alternatively some hooks within
jatos.jsthat users could use as needed.Alan