# How to packed JSON object over serial port (Serialise JSON object)

**URL:** https://discourse.processing.org/t/how-to-packed-json-object-over-serial-port-serialise-json-object/34686
**Category:** Electronics (Arduino, etc.)
**Created:** [January 17, 2022, 1:42am UTC](https://discourse.processing.org/t/how-to-packed-json-object-over-serial-port-serialise-json-object/34686 "2022-01-17T01:42:18Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![avong](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@avong](https://discourse.processing.org/u/avong)
#### Post date: [January 17, 2022, 1:42am UTC](https://discourse.processing.org/t/how-to-packed-json-object-over-serial-port-serialise-json-object/34686/1 "2022-01-17T01:42:18Z")

</div>

> please format code with \</\> button \* [homework policy](https://discourse.processing.org/faq/#homework) \* [asking questions](https://discourse.processing.org/t/2147)  
> I wish to set PC to communicate with my microcontroller over serial port.  
> The data format i use JSON.  
> The microcontroller to PC is working well but i can not send from the PC to the controller.  
> write function has no support for JSON object data type.

How can I achieve that?

> \<  
> JSONObject payLoad=null;  
> payLoad.setBoolean(“POWER”, true);  
> payLoad.setBoolean(“PUMP”, true);  
> payLoad.setInt(“VALUE”, 67);

```
// write(payLoad); // This will work. 
 >
  how should i handle this?

```

---

<div class="post-metadata">

### Author: ![SomeOne](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/someone/32/8639_2.png) [@SomeOne](https://discourse.processing.org/u/SomeOne)
#### Post date: [January 17, 2022, 2:40pm UTC](https://discourse.processing.org/t/how-to-packed-json-object-over-serial-port-serialise-json-object/34686/2 "2022-01-17T14:40:50Z")

</div>

I assume write function supports text. JSON is almost always transferred as text. JSON-objects are constructs of programming languages to help handling of JSON. Anyway payLoad.tostring() will give you your JSON-object as a string

---

<div class="post-metadata">

### Author: ![avong](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@avong](https://discourse.processing.org/u/avong)
#### Post date: [January 18, 2022, 5:21pm UTC](https://discourse.processing.org/t/how-to-packed-json-object-over-serial-port-serialise-json-object/34686/3 "2022-01-18T17:21:24Z")

</div>

I did as recommended. .  
But I am left another challenge.  
No error report but no data is transmitted.

I did it this way.

String str\_payload=payload.toString();  
write (str\_payload);

---

<div class="post-metadata">

### Author: ![avong](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@avong](https://discourse.processing.org/u/avong)
#### Post date: [January 18, 2022, 9:17pm UTC](https://discourse.processing.org/t/how-to-packed-json-object-over-serial-port-serialise-json-object/34686/4 "2022-01-18T21:17:44Z")

</div>

One other thing i notice:  
anytime I am packing of JSON object inside the function call to SerialPortWrite.  
I get null pointer to the json object instance.

Now to solve it my own way.  
I use String to manually pack the JSON struct and then forward it to the serial write.  
Something like:  
String payload ="{";  
payload +=“key:”;  
payload +=str(value);  
payload +="}";

write(payload);

This truly is crook way but since the data size is small from the PC to my microcontroller. I dont mine.

I still open to learn should someone know better way to handle that.

Something is did not mention before.

A call to Serialport is handle by thread, something like thread (“SerialPortWrite”)  
Anytime i need to send data to microcontroller will create the  
thread(“SerialPortWrite”);  
could this be the reason?

---

<div class="post-metadata">

### Author: ![SomeOne](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/someone/32/8639_2.png) [@SomeOne](https://discourse.processing.org/u/SomeOne)
#### Post date: [January 19, 2022, 3:27am UTC](https://discourse.processing.org/t/how-to-packed-json-object-over-serial-port-serialise-json-object/34686/5 "2022-01-19T03:27:19Z")

</div>

What does documentation tell you?

Quite often print() happens instantly where write() puts stuff to a buffer and you need another function call to to send or process the buffer.

---

<div class="post-metadata">

### Author: ![avong](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@avong](https://discourse.processing.org/u/avong)
#### Post date: [January 20, 2022, 5:44am UTC](https://discourse.processing.org/t/how-to-packed-json-object-over-serial-port-serialise-json-object/34686/6 "2022-01-20T05:44:01Z")

</div>

If my understanding is is right.

"write " function send data to the serial port. It send data out immediately they are loaded in it

while “print/println” send data to the console.

These are truly different.
