user73081: I'm new to Arduino and I'm building a project that fetches data from a website (using ESP8266) and then transfers them to my Arduino UNO via serial port.
Data coming from ESP8266 every 20s - [{"x": 0,"y": 0,"rgb": [255, 200, 174]},{"x": 1,"y": 0,"rgb": [255, 200, 174]},{"x": 2,"y": 0,"rgb": [255, 200, 174]}]
But when I print the data like this

void loop() {
  
  if (Serial.available()) {
    Serial.write(Serial.read()); // This line prints only one character out of whole string
    
    data =  Serial.read(); // so this doesn't work

    DeserializationError error = deserializeJson(response, data);
    if (error) {
      Serial.print(F("deserializeJson() failed: "));
      Serial.println(error.f_str());
      return;
    }
    for (JsonObject elem : response.as<JsonArray>()) {
      int x = elem["x"]; // 2, 2, 2
      int y = elem["y"]; // 2, 2, 2
      JsonArray rgb = elem["rgb"];
    
      int rgb_0 = rgb[0]; // 255, 255, 255
      int rgb_1 = rgb[1]; // 255, 255, 255
      int rgb_2 = rgb[2]; // 255, 255, 255
      //Serial.print(x);
    }
  }
  delay(1000);   //then it waits 1 second and goes from the start
}

How can I make it to get the whole string and save it into a variable?
Thanks, much for answering.

user70020: I was going to put this as a comment, but maybe it belongs as an answer:
For what it's worth, ArduinoJSON seems to be able to deserialize directly from a stream, i.e. Serial, which may obviate the question as asked.  Presumably it knows where to stop deserializing, because it knows when it has received an entire JSON object; being a proper JSON parser, it can probably work this out better than you can without, you know, writing a JSON parser.
The following was just thrown together, now the author is here you should probably just follow their direction, but here's the code anyway:
#include <ArduinoJson.h>


void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  Serial.begin(9600);
  Serial.setTimeout(30000);
}


void print_transmission_counter( {
    static unsigned long received_object_counter;
    Serial.println(F("\n\n\n\n"));
    for (int i = 0; i < 30; ++i) {
        Serial.write('-');
    }
    Serial.write('[');
    Serial.print(received_object_counter++);
    Serial.println(']');
}


void loop() {
    static StaticJsonDocument<256> json_doc;
    static bool led_state;

    print_transmission_counter();

    const auto deser_err = deserializeJson(json_doc, Serial);
    if (deser_err) {
        Serial.print(F("Failed to deserialize, reason: \""));
        Serial.print(deser_err.c_str());
        Serial.println('"');
    } else  {
        Serial.print(F("Recevied valid json document with "));
        Serial.print(json_doc.size());
        Serial.println(F(" elements."));
        Serial.println(F("Pretty printed back at you:"));
        serializeJsonPretty(json_doc, Serial);
        Serial.println();
    }


    // Just give some visual indication that the loop is progressing
    led_state = !led_state;
    digitalWrite(LED_BUILTIN, led_state);
}


//  vim:sw=2:ts=2:et:nowrap:ft=cpp:


73081: <p>I'm new to Arduino and I'm building a project that fetches data from a website (using ESP8266) and then transfers them to my <code>Arduino UNO</code> via serial port.</p>
<p>Data coming from ESP8266 every 20s - <code>[{&quot;x&quot;: 0,&quot;y&quot;: 0,&quot;rgb&quot;: [255, 200, 174]},{&quot;x&quot;: 1,&quot;y&quot;: 0,&quot;rgb&quot;: [255, 200, 174]},{&quot;x&quot;: 2,&quot;y&quot;: 0,&quot;rgb&quot;: [255, 200, 174]}]</code></p>
<p>But when I print the data like this</p>
<pre><code>
void loop() {
  
  if (Serial.available()) {
    Serial.write(Serial.read()); // This line prints only one character out of whole string
    
    data =  Serial.read(); // so this doesn't work

    DeserializationError error = deserializeJson(response, data);
    if (error) {
      Serial.print(F(&quot;deserializeJson() failed: &quot;));
      Serial.println(error.f_str());
      return;
    }
    for (JsonObject elem : response.as&lt;JsonArray&gt;()) {
      int x = elem[&quot;x&quot;]; // 2, 2, 2
      int y = elem[&quot;y&quot;]; // 2, 2, 2
      JsonArray rgb = elem[&quot;rgb&quot;];
    
      int rgb_0 = rgb[0]; // 255, 255, 255
      int rgb_1 = rgb[1]; // 255, 255, 255
      int rgb_2 = rgb[2]; // 255, 255, 255
      //Serial.print(x);
    }
  }
  delay(1000);   //then it waits 1 second and goes from the start
}
</code></pre>
<p>How can I make it to get the whole string and save it into a variable?</p>
<p>Thanks, much for answering.</p>
 70020: <p>I was going to put this as a comment, but maybe it belongs as an answer:</p>
<p>For what it's worth, ArduinoJSON seems to be able to <a href="https://arduinojson.org/v6/api/json/deserializejson/" rel="nofollow noreferrer">deserialize directly from a stream</a>, i.e. <code>Serial</code>, which may obviate the question <em>as asked</em>.  Presumably it knows where to stop deserializing, because it knows when it has received an entire JSON object; being a proper JSON parser, it can probably work this out better than you can without, you know, writing a JSON parser.</p>
<p>The following was just thrown together, now the author is here you should probably just follow their direction, but here's the code anyway:</p>
<pre class="lang-cpp prettyprint-override"><code>#include &lt;ArduinoJson.h&gt;


void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  Serial.begin(9600);
  Serial.setTimeout(30000);
}


void print_transmission_counter( {
    static unsigned long received_object_counter;
    Serial.println(F(&quot;\n\n\n\n&quot;));
    for (int i = 0; i &lt; 30; ++i) {
        Serial.write('-');
    }
    Serial.write('[');
    Serial.print(received_object_counter++);
    Serial.println(']');
}


void loop() {
    static StaticJsonDocument&lt;256&gt; json_doc;
    static bool led_state;

    print_transmission_counter();

    const auto deser_err = deserializeJson(json_doc, Serial);
    if (deser_err) {
        Serial.print(F(&quot;Failed to deserialize, reason: \&quot;&quot;));
        Serial.print(deser_err.c_str());
        Serial.println('&quot;');
    } else  {
        Serial.print(F(&quot;Recevied valid json document with &quot;));
        Serial.print(json_doc.size());
        Serial.println(F(&quot; elements.&quot;));
        Serial.println(F(&quot;Pretty printed back at you:&quot;));
        serializeJsonPretty(json_doc, Serial);
        Serial.println();
    }


    // Just give some visual indication that the loop is progressing
    led_state = !led_state;
    digitalWrite(LED_BUILTIN, led_state);
}


//  vim:sw=2:ts=2:et:nowrap:ft=cpp:
</code></pre>
