Arduino JSON for serial stream

Im using the BLuno arduino, Its connecting fine to my iOS phone, sending and receiving messages.

Now I want to use arduino json to parse messages from my phone,

The serial connection receives data fine, but Im doing something wrong taking the stream to parse with the json library…

I get the error:

deserializeJson() failed: InvalidInput

The code:

#include <ArduinoJson.h>
StaticJsonDocument<400> doc;
void setup() {
Serial.begin(115200); 
}

void loop()
{
  if (Serial.available() > 0)
  {
    String s = Serial.readStringUntil("#");   // Until CR (Carriage Return)
    s.replace("#", "");
    Serial.println(s);

    DeserializationError error = deserializeJson(doc, s);
    if (error) {
    Serial.print(F("deserializeJson() failed: "));
    Serial.println(error.c_str());
    return;
   }
  int value = doc["value"];
Serial.println(Serial.println(value));

IS

deserializeJson(doc, s);

correct?, I try to send different values, from the phone:

{'a':'b'} "{"a":"b"}" {\"a\":\"b\"}

Where is my problem, please?

Where is my problem, please?

My first thought is that your anonymous printing sucks.

When printing anything, I always define what it is I'm printing.

Serial.print("I got: ");
Serial.println(someVariable);

If the variable is a string (or a String), I use delimiters before and after the string that are not likely to be in the string:

Serial.print("I got: @");
Serial.print(someVariable);
Serial.println("@");

Only when you KNOW what the data you are trying to parse actually looks like can you successfully parse the data.

So, what ARE you trying to parse?

Thanks Paul,

Im trying to parse the incoming data message from serial that is arriving from my phone BTLE

here the simplified code

void jsonealo()
{
    StaticJsonDocument<200> doc;

String s = Serial.readStringUntil('\n');

  //que haces?
  s.replace("\n", "NUL, 0");
  //    String s = Serial.readStringUntil("#");   // Until CR (Carriage Return)
  //    s.replace("#", "");

  //  Serial.println("s:" + s);
  Serial.print("I got: @");
  Serial.print(s);
  Serial.println("@");

    DeserializationError error = deserializeJson(doc, s);

  // Test if parsing succeeds.
  if (error) {
    Serial.print(F("deserializeJson() failed: "));
    Serial.println(error.c_str());
    return;
  }

    const char* sensor = doc["a"];
  Serial.println(sensor);

now what I want to parse:
{“a”:“b”}

I tried in different ways, but it only seems to accept “{}”

I got: @{}@

I got: @“{}”@
deserializeJson() failed: InvalidInput
I got: @{“a”:”b”}@
deserializeJson() failed: InvalidInput
I got: @{\“a\”:\”b\⸮@
deserializeJson() failed: InvalidInput
I got: @{\“a\”}@
deserializeJson() failed: InvalidInput
I got: @{“a”}@
deserializeJson() failed: InvalidInput
I got: @{‘a’}@
deserializeJson() failed: InvalidInput
I got: @{‘’}@
deserializeJson() failed: InvalidInput
I got: @{\‘\’}@
deserializeJson() failed: InvalidInput
I got: @a:b@
deserializeJson() failed: InvalidInput
I got: @“a”@
deserializeJson() failed: InvalidInput
I got: @[]@

I got: @[{}]@

I got: @[{1}]@
deserializeJson() failed: InvalidInput
I got: @[{“1”}]@
deserializeJson() failed: InvalidInput

So what should I be sending from the phone? int it JSON?

thanks!

int it JSON?

If that was meant to be "Isn't it JSON", then the answer is yes and no. Yes, you appear to be attempting to deal with JSON. But, why?

The php script is just as able to output a string like "[14, On]" as it is to output a string like "{"id":"14","status":"On"}".

Parsing the first string is trivial. Parsing the second one is not.

Why are you trying to make life harder than it needs to be?

hi Paul, I might be making it complex as I don't know what easy is?

what string, format, object should I be sending from the phone, that will be then ingested via serial?

please tell me how to format:

{"a":"b"}

so the json parser will like it?

should I use other scape for characters?, pack it in some format?

thanks

please tell me how to format:

{"a":"b"}

so the json parser will like it?

No. I HATE JSON. The fact that every name must be in quotes, despite the fact that only a name can be in that position just annoys the hell out of me. I will do anything in order to avoid using JSON.

There is no reason for you to need to encrypt the data in JSON format.

If you just absolutely insist, invoke that php script from a browser, and copy/paste the code into a JSON validator (google will find plenty of them). The first step is to determine whether the output of the php script is correctly formatted. If it is, then make sure that the device that is getting that data is sending it to your Arduino correctly (that is that what you get on the Arduino matches what the browser gets.

If what the Arduino gets matches what the browser gets, and the JSON validators successfully parse the data but the Arduino library doesn't, you're up shit creek without a paddle.

Your sketch in your initial post does not compile due to several errors so I fixed it.
As your sending over network/serial you are reading to soon before all characters have arrived and also stream contains extra characters like that get picked up and placed onto the start of the next read. Because of this I empty the Serial buffer after reading the String.

#include <ArduinoJson.h>
StaticJsonDocument<400> doc;
void setup() 
{
  Serial.begin(115200);
  Serial.println("Ready");
}

void loop()
{
  if (Serial.available() > 0)
  {
    delay(10);
    String s = Serial.readStringUntil('#');
    // String s = "{\"a\":\"b\"}";
    
    while(Serial.available() > 0) {Serial.read();}
    Serial.print("I got: @");
    Serial.print(s);
    Serial.println("@");
    
    DeserializationError error = deserializeJson(doc, s);
    if (error) 
    {
      Serial.print(F("deserializeJson() failed: "));
      Serial.println(error.c_str());
      return;
    }
    Serial.println(error.c_str());
    
    const char* value = doc["a"];
    Serial.println(value);
  }
}
// {\"a\":\"b\"}#
// {"a":"b"}#