question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Problem outputting nested stringified data to JSON.

See original GitHub issue

In the current implementation of jsPsych, there is a problem handling trials with complex nested data. This is most easily illustrated in the jspsych-instructions plugin via the view_history property in trial_data.

The current solution implemented is to stringify the view_history array using { view_history: JSON.stringify(view_history)... }, which allows for sensible conversion to CSV. Without stringifying view_history, those view_history data would show up in the CSV as [object Object]. However, if you attempt to get the JSON data for the experiment, it cannot later be parsed with JSON.parse (or the json library in Python) due to problems resulting from attempting to parse a stringified JSON object within a stringified JSON object.

My current solution is to use the parse function from the json2csv library (available on npm) to replace the JSON2CSV helper function in jspsych.js. This allows me to have both sensible CSV and JSON representations of the data.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
jodeleeuwcommented, Feb 13, 2021

@becky-gilbert I went ahead and worked on this in the feature-objects-arrays-in-data branch.

It changes a core design idea: now we store objects and arrays in the data directly, instead of JSON versions of them. I updated all of the plugins that this affects, wrote a simple test to check the output of both .json() and .csv() methods in jsPsych.data, and updated the CSV conversion function.

Things that should probably happen before a merge:

  • Add tests to the new test files for a few other plugins that generate arrays/objects in the data.
  • Check the examples folder to see if any of the examples use JSON.parse() on data objects that are now not JSON.
  • Check the docs to see what needs to be updated to reflect this change.
0reactions
stefanuddenbergcommented, Nov 12, 2020

Sorry for not seeing this sooner; Github does not do a good job of notifying me about activity on posts it would seem! That’s precisely the approach that I took in my own fork of the project; I’m using the json2csv library to handle the csv-ification of the jsPsych data.

Read more comments on GitHub >

github_iconTop Results From Across the Web

JSON.stringify does not stringify nested arrays - Stack Overflow
JSON.stringify() seems to parse it without any issues: working example: var o1 = [{ "name":"string", "active":"bool", "data":{ ...
Read more >
JSON.stringify() - JavaScript - MDN Web Docs
The JSON.stringify() method converts a JavaScript value to a JSON string, optionally replacing values if a replacer function is specified or ...
Read more >
What you didn't know about JSON.Stringify - CodeKraft
This post shows you how to achieve much more with JavaScript's native JSON.Stringify method. A quick refresher about JSON and JavaScript:.
Read more >
How do we deal with JSON Stringify for nested objects?
The problem seems to be .toString() somewhere that's converting an object to a string named "object" somewhere in the code. function getUserInfo ...
Read more >
Solve common issues with JSON in SQL Server
Prevent double-escaped JSON in FOR JSON output ... Question. I have JSON text stored in a table column. I want to include it...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found