Escaping curly braces

How do you escape curly braces? Im writing a code that outputs the values from the analog inputs (six of) then formatting them in JSON style. Doing so gives theses errors:

sketch_jul20a.cpp: In function 'void loop()':
sketch_jul20a:49: error: expected primary-expression before '{' token
sketch_jul20a:86: error: expected primary-expression before '{' token

The source to have a look:

int fader1 = 0;
int fader2 = 0;
int fader3 = 0;
int fader4 = 0;
int fader5 = 0;
int fader6 = 0;
int up_button = 0;
int down_button = 0;
int go_button = 0;
    
void setup()  {
  Serial.begin(9600);
  pinMode(A1, INPUT);
  pinMode(A2, INPUT);
  pinMode(A3, INPUT);
  pinMode(A4, INPUT);
  pinMode(A5, INPUT);
  pinMode(A6, INPUT);
  pinMode(7, INPUT);
  pinMode(8, INPUT); 
  pinMode(9, INPUT);      
}

void loop()  {
 int fader1 = analogRead(0);
fader1 = map(fader1, 0, 1023, 0, 255);
Serial.print(fader1, 0);  

int fader2 = analogRead(1);
fader2 = map(fader2, 0, 1023, 0, 255);
Serial.print(fader2, 0);

int fader3 = analogRead(2);
fader3 = map(fader3, 0, 1023, 0, 255);
Serial.print(fader3, 0);

int fader4 = analogRead(3);
fader4 = map(fader4, 0, 1023, 0, 255);
Serial.print(fader4, 0);

int fader5 = analogRead(4);
fader5 = map(fader5, 0, 1023, 0, 255);
Serial.print(fader5, 0);

int fader6 = analogRead(5);
fader6 = map(fader6, 0, 1023, 0, 255);
Serial.print(fader6, 0);

Serial.print(
{
  "usb": "subout", 
  "subs": [
    {{
      "faderid": "1", 
      "value": "fader1", 
    }, 
     {
      "faderid": "2", 
      "value": "fader2", 
    },
     {
      "faderid": "3", 
      "value": "fader3", 
    }, 
     {
      "faderid": "4", 
      "value": "fader4", 
    }, 
     {
      "faderid": "5", 
      "value": "fader5", 
    }, 
     {
      "faderid": "6", 
      "value": "fader6", 
    } 
  ]
}
);

// Buttons
 down_button = digitalRead(7);  // read input value
 up_button = digitalRead(8);  // read input value
 go_button = digitalRead(9);  // read input value

 Serial.print(
{
  "usb": "buttons", 
  "but": [
    {
      "buttonid": "down", 
      "value": "down_button", 
    }, 
     {
      "buttonid": "up", 
      "value": "up_button", 
    },
     {
      "buttonid": "go", 
      "value": "go_button", 
    }
  ]
}
);
}

Any help or improvements would be greatly appreciated. :)

CB

Brush up on the Serial.print() function. It does not take the stuff you are passing it as an argument. It takes at most two arguments. What that stuff is supposed to be escapes me.

think you need to add some \ to escape the " and put " 's around the whole string

A minimized version of what I think you need … (rest is more of the same)

Serial.print("
{
 \"usb\": \"subout\", 
}
")

PaulS: Brush up on the Serial.print() function. It does not take the stuff you are passing it as an argument. It takes at most two arguments. What that stuff is supposed to be escapes me.

Its a cheats way of putting my data into JSON formatting...

robtillaart: think you need to add some \ to escape the " and put " 's around the whole string

A minimized version of what I think you need ... (rest is more of the same)

Serial.print("
{
 \"usb\": \"subout\", 
}
")

Still didn't help. Threw up even more errors! I appreciate the help.

sketch_jul20a:50: error: stray '\' in program
sketch_jul20a:50: error: missing terminating " character
sketch_jul20a:51: error: stray '\' in program
sketch_jul20a:51: error: missing terminating " character
sketch_jul20a:53: error: stray '\' in program
sketch_jul20a:53: error: missing terminating " character
sketch_jul20a:54: error: stray '\' in program
sketch_jul20a:54: error: missing terminating " character
sketch_jul20a:57: error: stray '\' in program
sketch_jul20a:57: error: missing terminating " character
sketch_jul20a:58: error: stray '\' in program
sketch_jul20a:58: error: missing terminating " character
sketch_jul20a:61: error: stray '\' in program
sketch_jul20a:61: error: missing terminating " character
sketch_jul20a:62: error: stray '\' in program
sketch_jul20a:62: error: missing terminating " character
sketch_jul20a:65: error: stray '\' in program
sketch_jul20a:65: error: missing terminating " character
sketch_jul20a:66: error: stray '\' in program
sketch_jul20a:66: error: missing terminating " character
sketch_jul20a:69: error: stray '\' in program
sketch_jul20a:69: error: missing terminating " character
sketch_jul20a:70: error: stray '\' in program
sketch_jul20a:70: error: missing terminating " character
sketch_jul20a:73: error: stray '\' in program
sketch_jul20a:73: error: missing terminating " character
sketch_jul20a:74: error: stray '\' in program
sketch_jul20a:74: error: missing terminating " character
sketch_jul20a:85: error: stray '\' in program
sketch_jul20a:85: error: missing terminating " character
sketch_jul20a:87: error: stray '\' in program
sketch_jul20a:87: error: missing terminating " character
sketch_jul20a:88: error: stray '\' in program
sketch_jul20a:88: error: missing terminating " character
sketch_jul20a:90: error: stray '\' in program
sketch_jul20a:90: error: missing terminating " character
sketch_jul20a:91: error: stray '\' in program
sketch_jul20a:91: error: missing terminating " character
sketch_jul20a:94: error: stray '\' in program
sketch_jul20a:94: error: missing terminating " character
sketch_jul20a:95: error: stray '\' in program
sketch_jul20a:95: error: missing terminating " character
sketch_jul20a:98: error: stray '\' in program
sketch_jul20a:98: error: missing terminating " character
sketch_jul20a:99: error: stray '\' in program
sketch_jul20a:99: error: missing terminating " character
sketch_jul20a:102: error: stray '\' in program
sketch_jul20a:102: error: missing terminating " character
sketch_jul20a.cpp: In function 'void loop()':
sketch_jul20a:49: error: expected primary-expression before '{' token
sketch_jul20a:104: error: expected `;' at end of input
sketch_jul20a:104: error: expected `}' at end of input

Its a cheats way of putting my data into JSON formatting...

Kind of difficult to cheat the compiler, kind of like trying to win an argument with the wife. ;)

Lefty

Then it becomes a job for sprintf() something like

char buf[80];
sprintf(buf, "\n{\n\t\"usb\":\"subout\",\n}\n");
Serial.print(buf);

Still not compiling right. Back to the drawing board I think here!

jiggles1992:
Still not compiling right. Back to the drawing board I think here!

Provide us the non-compiling code after your modification.

Also make a mock-up output that you want to see. All we see is a non-working code and have no idea what it is intended to do if it’s corrected.

In C/C++, you can get away with displaying this text, as long as you make sure that every text line has two valid double quotes, one before the data, and one after. So,

Serial.print(
"{"
"  "usb": "buttons", "
"  "but": ["
"    {"
"      "buttonid": "down", "
"      "value": "down_button", "
"    }, "
"     {"
"      "buttonid": "up", "
"      "value": "up_button", "
"    },"
"     {"
"      "buttonid": "go", "
"      "value": "go_button", "
"    }"
"  ]"
"}"
);

C is smart enough to splice all these lines to make one long data line.

However, you still have the problem of embedded double quotes - as already pointed out, you'll need to escape these with a slash, leading to this:

Serial.print(
"{"
"  \"usb\": \"buttons\", "
"  \"but\": ["
"    {"
"      \"buttonid\": \"down\", "
"      \"value\": \"down_button\", "
"    }, "
"     {"
"      \"buttonid\": \"up\", "
"      \"value\": \"up_button\", "
"    },"
"     {"
"      \"buttonid\": \"go\", "
"      \"value\": \"go_button\", "
"    }"
"  ]"
"}"
);

This compiles and displays more or less the way you want.

One more thing- if you have any '\' in the code before you start, you'll need to change them to \ since slashes, like double quotes, need to be escaped here.

Thanks David for this solution, looks more readable than the sprintf().

  • the \n 's for the newlines must be inserted too to get the layout right...

The next question is that part of it are variables I think: "value": "down_button", .. I assume that "down_button" needs to be replaced by the contents of the var down_button?

maybe the stream library - http://arduiniana.org/2009/04/new-streaming-library/ - is easier for this ...

jiggles1992: How do you escape curly braces?

Sadly, I was only trying to solve that problem of braces - the 'forest' surrounding the 'trees' is much more serious:

  • There's only so much string space (or anything space) in Arduino - using it to format a huge string like that is just asking to run out of room.

  • Using printf like this on large strings takes about 2X the space - one for the original string, one for the buffer - so you need to work with very small strings.

  • Prettifying the display also takes up valuable memory - I count about 80 characters of leading space in the example I sent you (the smallest of the two!)

I'm thinking what you will need to do is either a) skip the JSON format and just output minimal data (say with a comma separator only), or b) keep the JSON but use a loop and minimal printf to output the data.

Does either direction interest you?

is the indentation of json mandatory, or does a json parser accept all as long as the tree is valid?

use tab \t iso spaces ? (factor 4 less string:)

Thanks guys.

@David Pankhurst: Thanks that works perfectly! I don't know yet. My Arduino is still yet to arrive along with the slide pots and buttons!

@robtillaart: Yes they would have to be replaced. Seems thats just made things far far far more complex! Ugh!

Thanks for this help! Really helping me with how the code works and such. :)