How to write into a JSON file via input-box?

Hi there. I’d like to know the following: Is it possible to save user-input straightly into a specific array position in a JSON file?

Example:
The user get’s a little screen in Processing. It states the question: “How much gravity has the planet you’re on?” and the user types 1.6 and then that specific answer, whether it’s a number or a text, gets saved in the last slot for gravity. After that the user would be able to click to the next question and it would just go down the list. Please let me know if this is possible and how I could achieve this. Thank you!

{
	"universe": [
		{"type": "A6", "atmosphere": "none", "gravity": 1.6},
		{"type": "C2", "atmosphere": "oxygen", "gravity": 2.4},
		{"type": "K5", "atmosphere": "sulfur", "gravity": 5.1},
	]
}

step one is file IO

from your idea, the provided record would be like
"this would be the correct answer "

but anyhow basic is

  • read file
  • write file on key [s]
// JSONArray json_a;   ////  json_a = loadJSONArray(infile);   // saveJSONArray(json_a,outfile);
JSONObject json_o;

String infile  = "data/universe.json";
String outfile = "data/universe_answers.json";

void setup() {
  json_o = loadJSONObject(infile);
  println("use:\n [s] to save data to file");
}

void draw() {
}

void keyPressed() {
  if ( key == 's' ) { 
    saveJSONObject(json_o, outfile);
    println("save to "+outfile);
  }
}

seems JSON is very forgiving, it ignored your [TAB]'s and your last [,]


next step
ok question:
what input box you talk about?

Thanks for taking your time.

The input box is actually just a an occasion where the user can type their answer.
Here’s the code I initially used, it has the input box included.

String str = "Universe size is: ";
String str2 = "The amount of stars is: "; 

int sceneCounter;

void setup(){
  size(900,400);
  sceneCounter = 0;
}
 
void draw(){
  background(0);
  fill(255);
  textAlign(CENTER, CENTER);
  textSize(25);
  
  if (sceneCounter == 0) {
    text("Give the size of the universe", width/2, 80);
    text(str,width/2, 150);
    //fill(128);
    if( over() ){
      fill(150);
    }
    rectMode(CENTER);
    rect(width/2,height-100,200,70);
  }
  
  if (sceneCounter > 0) {
    text("second page", width/2, 80);
    text(str2,width/2, 150);
    //fill(128);
    if( over() ){
      fill(150);
    }
    rectMode(CENTER);
    rect(width/2,height-100,200,70);
  }
}
 
 
boolean over(){
  return(mouseX > 350 && mouseX < 550 && mouseY > height-140 );
}

 
void keyPressed(){
  if( keyCode == DELETE || keyCode == BACKSPACE ){
    if( str.length() > 0) {
      str = str.substring(0, str.length()-1);
    }
  } else {
    if( key != CODED ){
      str += key;
    }
  }  
}
 
 
void mousePressed(){
  if( over() ){
    
    sceneCounter = sceneCounter++;
  if (sceneCounter > 3) {
    sceneCounter = 0;
  }
  println("sceneCounter " + sceneCounter++);
  
    saveIt();
  }
}
 
void saveIt(){
  String[] strs = { str, str2 };
  saveStrings( "data.json", strs);
}

ok, thanks
it does save a file but please

  • use the /data/filename.extension
    path and not the sketch path.
  • better use filename string variables
  • you save a string array with saveStrings, that is not JSON

but it does work, so i must ask

if you want to learn that you again start with my first example and
try to get the array and data ( out of the from file already loaded JSON object )
try:

JSONObject json_o;

String infile  = "data/universe.json";
String outfile = "data/universe_answers.json";

void setup() {
  println("load from "+infile);
  json_o = loadJSONObject(infile);
  get_content();
  println("use:\n [s] to save data to file");
}

void get_content() {
  JSONArray universe = json_o.getJSONArray("universe");
  for ( int i=0; i < universe.size(); i++ ) {
    JSONObject planet  = universe.getJSONObject(i);
    String type =  planet.getString("type");
    String atmo =  planet.getString("atmosphere");
    Float  grav =  planet.getFloat("gravity");
    println ("#i: "+i+" planet type: "+ type +" atmosphere: "+ atmo +" gravity: "+ grav);
  }
}

void draw() {
}

void keyPressed() {
  if ( key == 's' ) { 
    saveJSONObject(json_o, outfile);
    println("save to "+outfile);
  }
}

shows:

load from data/universe.json
#i: 0 planet type: A6 atmosphere: none gravity: 1.6
#i: 1 planet type: C2 atmosphere: oxygen gravity: 2.4
#i: 2 planet type: K5 atmosphere: sulfur gravity: 5.1
use:
 [s] to save data to file

and change the values according any user input.

so just to go more into the question / answer idea

? what you think of the idea to have 2 value fields /

  • the user answer
  • the correct value

so the structure of read and save file can be same. just the “loadfile” has a empty user answer field
and the answer check coding ( you need to do later gets more easy )

( about the input window and the click button can talk later… )
could look like
SNAG-0068

I think it’s not a quiz, it’s about entering data

See

https://processing.org/reference/JSONObject_setJSONArray_.html

That is correct. I’d also like to add that the link you sent is exactly what I’m looking for. However, I want to know if a user could write the info straight into the array, that’s then saved into a JSON file.

In my previous post (and code) I had this screen pop up to the user, where they could answer the questions and click the button to move on to the next one.

Hopefully I’m clear enough. So, to state the question again: Is it possible to write into a box, like the image below shows, and have that answer be saved into the array of a JSON file?

Yes

When Enter is pressed, fill it in the json using setString

When starting entering a new planet look at the link above

Chrisir

here is an example with an userInput :

String userInput = ""; 
int i; 

// list of planets 
JSONArray values = new JSONArray();

void setup() {

  size (700, 700);

  println("Attention, file gets overwritten"); 

  userInput = "1.6";

  addOneElement(userInput);  

  makeCompleteJsonObjectAndSave();
}

void draw() {
}

// -------------------------------------------------

void addOneElement(String userInput_) {

  // this can be executed when adding a new planet to values (collect user input before)

  JSONObject animal = new JSONObject();

  animal.setInt("id", i);
  animal.setString("species", userInput_);
  animal.setString("name", "Test 7");

  values.setJSONObject(i, animal);

  println (i
    +" :  "
    +values);

  i++;
}

void makeCompleteJsonObjectAndSave() {

  // this can be executed when leaving the program / you want to save all

  JSONObject json;

  json = new JSONObject();
  json.setJSONArray("animals", values);

  saveJSONObject(json, "data/new.json");
}
1 Like

That looks much better than I had in mind.

As to answer your question about why it has to be JSON, it’s because I want to gather user data so I could import it into After Effects (which supports JSON files), where I can then link the data to Visual FX and animation parameters.

This may not be fully logical, since you could also enter that data manually in After Effects, but the whole driver behind this little project is the fact that it’s a school project that I have to finish in 1.5 weeks - and I’m kinda running low on time

2 Likes

esp. if you make a data file used in another system ( computer or program )
your database structure should be good.

++ your start with the first JSON object including a array using good descriptors
? sorry i not know the correct name for a field like “universe” ?
( JSON has its own wording i think )
already.
still any program using this ( incl. the processing program we talk about here )
would need to have the question text “Give the size of the universe” hardcoded
and the other system, reading the answers, would not know the questions!!

also what i mentioned already,
to have the user answers in one field and the correct answers
like for

  • atmosphere ?==? oxygen
  • gravity ?==? 2.4

in a second field makes sense.
here an example to check and score on that: ( score is a database field too ! )

    int newscore = 0;
    if ( question.getString("q1user").equals( question.getString("q1check") ) ) newscore++;
    if ( question.getFloat("q2user") == question.getFloat("q2check") ) newscore++;
    question.setInt("score",newscore);