How to decode server response

I am bit confused I send this request http://192.168.172.3:2001/detail?Id=1111 in postman and then I get following response from server in postman

json format

{
    "USER": "IDS",
	
    "Byte": {
        "public_id": 62,
        "communication": {
            "user_id": 24,
            "range": [
                {
                    "no_1": "111"
                },
                {
                    "no_2": "222"
                },
                {
                    "no_3": "333"
                }
            ]
        },
        "Internet": {
            "user_id": 115,
            "range": [
                {
                    "no_1": "144"
                },
                {
                    "card_1": "1.1"
                }
            ]
        }
    },
    "InternetData": [
        {
            "id_1": 1,
            "name_1": "rock",
            "wait_1": 1
        },
        {
            "id_2": 2,
            "name_2": "devid",
            "wait_2": 1
        },
        {
            "id_3": 3,
            "name_3": "dave",
            "wait_3": 5
        },
        {
            "id_4": 4,
            "name_4": "stev",
            "wait_4": 3
        }
    ]
}

request header

// send HTTP request header
client.println("GET" + " " + PATH_NAME +  String S("?Id=1111");" HTTP/1.1");
client.println("Host: " + String(HOST_NAME));
client.println("Connection: close");
client.println(); // end HTTP request header

I don't understand How to decode this type of server response ? Do I only need to
Deserialize ?

You may want to have a look at one of the JSON libraries. E.g.,

Deserializing is a start. The ArduinoJson 'Assistant' will show you how.

// Stream& input;

StaticJsonDocument<768> doc;

DeserializationError error = deserializeJson(doc, input);

if (error) {
  Serial.print(F("deserializeJson() failed: "));
  Serial.println(error.f_str());
  return;
}

const char* USER = doc["USER"]; // "IDS"

JsonObject Byte = doc["Byte"];
int Byte_public_id = Byte["public_id"]; // 62

int Byte_communication_user_id = Byte["communication"]["user_id"]; // 24

JsonArray Byte_communication_range = Byte["communication"]["range"];

const char* Byte_communication_range_0_no_1 = Byte_communication_range[0]["no_1"]; // "111"

const char* Byte_communication_range_1_no_2 = Byte_communication_range[1]["no_2"]; // "222"

const char* Byte_communication_range_2_no_3 = Byte_communication_range[2]["no_3"]; // "333"

int Byte_Internet_user_id = Byte["Internet"]["user_id"]; // 115

const char* Byte_Internet_range_0_no_1 = Byte["Internet"]["range"][0]["no_1"]; // "144"

const char* Byte_Internet_range_1_card_1 = Byte["Internet"]["range"][1]["card_1"]; // "1.1"

JsonArray InternetData = doc["InternetData"];

JsonObject InternetData_0 = InternetData[0];
int InternetData_0_id_1 = InternetData_0["id_1"]; // 1
const char* InternetData_0_name_1 = InternetData_0["name_1"]; // "rock"
int InternetData_0_wait_1 = InternetData_0["wait_1"]; // 1

JsonObject InternetData_1 = InternetData[1];
int InternetData_1_id_2 = InternetData_1["id_2"]; // 2
const char* InternetData_1_name_2 = InternetData_1["name_2"]; // "devid"
int InternetData_1_wait_2 = InternetData_1["wait_2"]; // 1

JsonObject InternetData_2 = InternetData[2];
int InternetData_2_id_3 = InternetData_2["id_3"]; // 3
const char* InternetData_2_name_3 = InternetData_2["name_3"]; // "dave"
int InternetData_2_wait_3 = InternetData_2["wait_3"]; // 5

JsonObject InternetData_3 = InternetData[3];
int InternetData_3_id_4 = InternetData_3["id_4"]; // 4
const char* InternetData_3_name_4 = InternetData_3["name_4"]; // "stev"
int InternetData_3_wait_4 = InternetData_3["wait_4"]; // 3

It's here
Select your options, then paste your response : 2 clicks after, you get the code. Just keep the info you're looking for.

Do I need to add the code of Deserialization in this way ?

#include <SPI.h>
#include <Ethernet.h>
#include <ArduinoJson.h>

const char* USER;

JsonObject Byte;
int Byte_public_id;

int Byte_communication_user_id ;

JsonArray Byte_communication_range;

const char* Byte_communication_range_0_no_1;

const char* Byte_communication_range_1_no_2;
const char* Byte_communication_range_2_no_3;

int Byte_Internet_user_id;

const char* Byte_Internet_range_0_no_1;

const char* Byte_Internet_range_1_card_1;

JsonArray InternetData;
JsonObject InternetData_0 ;
int InternetData_0_id_1;
const char* InternetData_0_name_1;
int InternetData_0_wait_1;

JsonObject InternetData_1;
int InternetData_1_id_2 ;
const char* InternetData_1_name_2 ;
int InternetData_1_wait_2;

JsonObject InternetData_2;
int InternetData_2_id_3 ;
const char* InternetData_2_name_3;
int InternetData_2_wait_3;

JsonObject InternetData_3;
int InternetData_3_id_4;
const char* InternetData_3_name_4;
int InternetData_3_wait_4;
// replace the MAC address below by the MAC address printed on a sticker on the Arduino Shield 2
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

EthernetClient client;
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);

}

void loop() {
  // put your main code here, to run repeatedly:

  while (client.available())
  {
    // read an incoming byte from the server and print them to serial monitor:
    char c = client.read();
    Serial.print(c);

    // Stream& input;

    StaticJsonDocument<768> doc;

    DeserializationError error = deserializeJson(doc, client);

    if (error) {
      Serial.print(F("deserializeJson() failed: "));
      Serial.println(error.f_str());
      return;
    }
    USER = doc["USER"]; // "IDS"

    JsonObject Byte = doc["Byte"];
    Byte_public_id = Byte["public_id"]; // 62

    Byte_communication_user_id = Byte["communication"]["user_id"]; // 24

    JsonArray Byte_communication_range = Byte["communication"]["range"];

    Byte_communication_range_0_no_1 = Byte_communication_range[0]["no_1"]; // "111"

    Byte_communication_range_1_no_2 = Byte_communication_range[1]["no_2"]; // "222"

    Byte_communication_range_2_no_3 = Byte_communication_range[2]["no_3"]; // "333"

    Byte_Internet_user_id = Byte["Internet"]["user_id"]; // 115

    Byte_Internet_range_0_no_1 = Byte["Internet"]["range"][0]["no_1"]; // "144"

    Byte_Internet_range_1_card_1 = Byte["Internet"]["range"][1]["card_1"]; // "1.1"

    JsonArray InternetData = doc["InternetData"];

    JsonObject InternetData_0 = InternetData[0];
    int InternetData_0_id_1 = InternetData_0["id_1"]; // 1
    const char* InternetData_0_name_1 = InternetData_0["name_1"]; // "rock"
    int InternetData_0_wait_1 = InternetData_0["wait_1"]; // 1

    JsonObject InternetData_1 = InternetData[1];
    InternetData_1_id_2 = InternetData_1["id_2"]; // 2
    InternetData_1_name_2 = InternetData_1["name_2"]; // "devid"
    InternetData_1_wait_2 = InternetData_1["wait_2"]; // 1

    JsonObject InternetData_2 = InternetData[2];
    InternetData_2_id_3 = InternetData_2["id_3"]; // 3
    InternetData_2_name_3 = InternetData_2["name_3"]; // "dave"
    InternetData_2_wait_3 = InternetData_2["wait_3"]; // 5

    JsonObject InternetData_3 = InternetData[3];
    InternetData_3_id_4 = InternetData_3["id_4"]; // 4
    InternetData_3_name_4 = InternetData_3["name_4"]; // "stev"
    InternetData_3_wait_4 = InternetData_3["wait_4"]; // 3



  }

}

why Byte_communication_range_0_no_1 variable printing 0 instead 111

#include <SPI.h>
#include <Ethernet.h>
#include <ArduinoJson.h>

const char* USER;

JsonObject Byte;
int Byte_public_id;

int Byte_communication_user_id ;

JsonArray Byte_communication_range;

const char* Byte_communication_range_0_no_1;

const char* Byte_communication_range_1_no_2;
const char* Byte_communication_range_2_no_3;

int Byte_Internet_user_id;

const char* Byte_Internet_range_0_no_1;

const char* Byte_Internet_range_1_card_1;

JsonArray InternetData;
JsonObject InternetData_0 ;
int InternetData_0_id_1;
const char* InternetData_0_name_1;
int InternetData_0_wait_1;

JsonObject InternetData_1;
int InternetData_1_id_2 ;
const char* InternetData_1_name_2 ;
int InternetData_1_wait_2;

JsonObject InternetData_2;
int InternetData_2_id_3 ;
const char* InternetData_2_name_3;
int InternetData_2_wait_3;

JsonObject InternetData_3;
int InternetData_3_id_4;
const char* InternetData_3_name_4;
int InternetData_3_wait_4;
// replace the MAC address below by the MAC address printed on a sticker on the Arduino Shield 2
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

EthernetClient client;
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);

}

void loop() {
  // put your main code here, to run repeatedly:

  while (client.available())
  {
    // read an incoming byte from the server and print them to serial monitor:
    char c = client.read();
    Serial.print(c);

    // Stream& input;

    StaticJsonDocument<768> doc;

    DeserializationError error = deserializeJson(doc, client);

    if (error) {
      Serial.print(F("deserializeJson() failed: "));
      Serial.println(error.f_str());
      return;
    }
    USER = doc["USER"]; // "IDS"

    JsonObject Byte = doc["Byte"];
    Byte_public_id = Byte["public_id"]; // 62

    Byte_communication_user_id = Byte["communication"]["user_id"]; // 24

    JsonArray Byte_communication_range = Byte["communication"]["range"];

    Byte_communication_range_0_no_1 = Byte_communication_range[0]["no_1"]; // "111"
    Serial.println(Byte_communication_range_0_no_1);
  }

}

What does it means ?

18:40:33.215 -> connected
18:40:33.308 -> HdeserializeJson() failed: InvalidInput
18:40:33.400 -> 1deserializeJson() failed: InvalidInput
18:40:33.447 -> 2deserializeJson() failed: InvalidInput
18:40:33.447 -> OdeserializeJson() failed: InvalidInput
18:40:33.540 -> 
18:40:33.540 -> deserializeJson() failed: InvalidInput
18:40:33.540 ->  deserializeJson() failed: InvalidInput
18:40:33.587 ->  deserializeJson() failed: InvalidInput
18:40:33.634 -> SdeserializeJson() failed: InvalidInput
18:40:33.680 -> 2deserializeJson() failed: InvalidInput
18:40:33.726 -> 1deserializeJson() failed: InvalidInput
18:40:33.773 -> 1deserializeJson() failed: InvalidInput
18:40:33.819 -> 5deserializeJson() failed: InvalidInput
18:40:33.866 -> GdeserializeJson() failed: InvalidInput
18:40:33.912 -> 

Looks like what you are passing to deserialize() is not a valid JSON document. If you send the characters you read from ‘client’ to Serial Monitor, does it look like a JSON document?

1 Like

Does the error here show that the deserialization has not been successful?

I am attaching two test's result of serial monitor
test 1

18:40:33.215 -> connected
18:40:33.308 -> HdeserializeJson() failed: InvalidInput
18:40:33.400 -> 1deserializeJson() failed: InvalidInput
18:40:33.447 -> 2deserializeJson() failed: InvalidInput
18:40:33.447 -> OdeserializeJson() failed: InvalidInput
18:40:33.540 -> 
18:40:33.540 -> deserializeJson() failed: InvalidInput
18:40:33.540 ->  deserializeJson() failed: InvalidInput
18:40:33.587 ->  deserializeJson() failed: InvalidInput
18:40:33.634 -> SdeserializeJson() failed: InvalidInput
18:40:33.680 -> 2deserializeJson() failed: InvalidInput
18:40:33.726 -> 1deserializeJson() failed: InvalidInput
18:40:33.773 -> 1deserializeJson() failed: InvalidInput
18:40:33.819 -> 5deserializeJson() failed: InvalidInput
18:40:33.866 -> GdeserializeJson() failed: InvalidInput
18:40:33.912 -> 
18:40:33.912 -> deserializeJson() failed: InvalidInput
18:40:33.912 ->  deserializeJson() failed: InvalidInput
18:40:34.004 -> 
18:40:34.004 -> deserializeJson() failed: InvalidInput
18:40:34.050 ->  deserializeJson() failed: InvalidInput
18:40:34.050 -> 
18:40:34.050 -> deserializeJson() failed: InvalidInput
18:40:34.144 ->  deserializeJson() failed: InvalidInput
18:40:34.144 -> jdeserializeJson() failed: InvalidInput
18:40:34.190 -> 
18:40:34.237 -> true 
18:40:34.237 -> 2
18:40:34.237 -> name 

Test2



18:41:31.264 -> connected
18:41:31.358 -> HdeserializeJson() failed: InvalidInput
18:41:31.403 -> 1deserializeJson() failed: InvalidInput
18:41:31.449 -> 2deserializeJson() failed: InvalidInput
18:41:31.449 -> OdeserializeJson() failed: InvalidInput
18:41:31.542 -> 
18:41:31.542 -> deserializeJson() failed: InvalidInput
18:41:31.587 ->  deserializeJson() failed: InvalidInput
18:41:31.587 ->  deserializeJson() failed: InvalidInput
18:41:31.634 -> SdeserializeJson() failed: InvalidInput
18:41:31.681 -> 2deserializeJson() failed: InvalidInput
18:41:31.728 -> 1deserializeJson() failed: InvalidInput
18:41:31.774 -> 1deserializeJson() failed: InvalidInput
18:41:31.820 -> 4deserializeJson() failed: InvalidInput
18:41:31.866 -> GdeserializeJson() failed: InvalidInput
18:41:31.912 -> 
18:41:31.912 -> deserializeJson() failed: InvalidInput
18:41:31.959 ->  deserializeJson() failed: InvalidInput
18:41:32.005 -> 
18:41:32.005 -> deserializeJson() failed: InvalidInput
18:41:32.052 ->  deserializeJson() failed: InvalidInput
18:41:32.052 -> 
18:41:32.052 -> deserializeJson() failed: InvalidInput
18:41:32.145 ->  deserializeJson() failed: InvalidInput
18:41:32.191 -> jdeserializeJson() failed: InvalidInput
18:41:32.191 -> 
18:41:32.238 -> true
18:41:32.238 -> 2
18:41:32.238 -> name

Yes. It says “failed: Invalid Input”.

1 Like

I am getting confused because I am getting the value along with the error message

18:41:32.238 -> true
18:41:32.238 -> 2
18:41:32.238 -> name

what message should be displayed after deserialization?

It looks like you are calling deserialize twenty times with invalid JSON before you call it with valid JSON.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.