JsonParserGeneratorRK How to build an array

I need to build a json array such as:

{  
   "data":{  
      [  
         {  
            "input":"I1",
            "state":false,
            "channel":0,
            "module_name":"OE_IOT_1016A"
         },
         {  
            "input":"I2",
            "state":false,
            "channel":1,
            "module_name":"OE_IOT_1016A"
         },
         {  
            "input":"I3",
            "state":false,
            "channel":2,
            "module_name":"OE_IOT_1"
         }
      ],
      "ttl":60,
      "published_at":"2018-12-02T15:48:29.699Z",
      "coreid":"2c003f000c47363339343638"
   }
}

The form of the json array builder is as follows:
The block_cnt is to batch output Pub’s before the payload reached 256 (Hand calculated for now)

       JsonWriterStatic<256> root;
       root.startArray();
       int block_cnt = 0;
        for (int i = 0; i<15;i++){

                // Crearte record
               root.startObject();
                // Crearte record
                //root.insertKeyObject("rec");
                root.insertKeyValue("input",inputs_alias[i]);
            	root.insertKeyValue("state", input_status[i]);
            	root.insertKeyValue("channel", i);
            	root.insertKeyValue("module_name", module_name);
            	root.finishObjectOrArray();

               block_cnt++;
                if (block_cnt == 3) {
                    root.finishObjectOrArray();
                    Particle.publish("inputs", root.getBuffer(),60,PRIVATE);
                    root.startArray();
                    block_cnt = 0;
                }
        }
    root.finishObjectOrArray();
    Particle.publish("inputs", root.getBuffer(),60,PRIVATE);

This outputs:
event: inputs
data: {“data”:"[{“input”:“I1”,“state”:false,“channel”:0,“module_name”:“OE_IOT_1016A”}{“input”:“I2”,“state”:false,“channel”:1,“module_name”:“OE_IOT_1016A”}{“input”:“I3”,“state”:false,“channel”:2,“module_name”:“OE_IOT_1016A”}][{“input”:“I4”,“state”:false,“channel”:3,“modu”,“ttl”:60,“published_at”:“2018-12-02T16:37:55.969Z”,“coreid”:“2c003f000c47363339343638”}

NO COMMAS’s BETWEEN OBJECTS
SOME RANDOM DATA AFTER THE ARRAY [{“input”:“I4”,“state”:false,“channel”:3,"modu"
ONLY THE FIRST 3 RECORDS AROU EVER OUTPUT AND REPEATS AGAIN

Can anyone set me on the right path, using the JsonParserGeneratorRK library ?

I create JSON structured (and compliant) event messages using snprintf();

I define a global char array dataStr[MAXDATA+2]; large enough to hold the biggest data load.
And the structuring strings:

const char* const mName           =    "{\"N\":\"";
const char* const mValue          =    "\",\"V\":\"";
    dataLen = snprintf(dataStr, MAXDATA, "%sF%s%s\"},%sV%s%s\"},",mName,mValue,firmware,mName,mValue,softwarebuild);

then

Particle.publish(“inputs”, (const char*) dataStr, 60, PRIVATE);

Call insertCheckSeparator() before your startObject() inside your loop.

That’s kind of a bug, there should probably be a function that does that automatically like insertArrayValue does, maybe startArrayObject or something like that, but adding that one line will solve your problem for now.

	// Writer test - array of objects
	{
		JsonWriterStatic<256> jw;

		jw.startArray();

		for(int ii = 0; ii < 5; ii++) {
			jw.insertCheckSeparator();
			jw.startObject();
			jw.insertKeyValue("ii", ii);

			jw.finishObjectOrArray();
		}

		jw.finishObjectOrArray();

		// printf("'%s'\n", jw.getBuffer());

		assertJsonWriterBuffer(jw, "[{\"ii\":0},{\"ii\":1},{\"ii\":2},{\"ii\":3},{\"ii\":4}]");

	}

Thanks rickkas7 and amor.
rickkas7 That worked, and my output is as follows:

{“data”:"[{“input”:“I1”,“state”:false,“channel”:0,“module_name”:“OE_IOT_1016A”},{“input”:“I2”,“state”:false,“channel”:1,“module_name”:“OE_IOT_1016A”},{“input”:“I3”,“state”:false,“channel”:2,“module_name”:“OE_IOT_1016A”}][{“input”:“I4”,“state”:false,“channel”:3,“mo”,“ttl”:60,“published_at”:“2018-12-02T17:28:52.648Z”,“coreid”:“2c003f000c47363339343638”}

For some reason after the array closes, there is a remnant of the next record:[{“input”:“I4”,“state”:false,“channel”:3,“mo”,“ttl”:60,“published_at”:"2018-12-

Is there some way to flush the buffer between blocks JsonWriterStatic<256> jw;
Also is there some way to tell the payload size of jw ?

In the block_cnt == 3 test, before you call root.startArray(), call root.init(). That’s necessary if you reuse a JsonWriterStatic to remove the old contents of the buffer.

Thanks, that worked.
Is there a way to tell what buffer space has been used, so I can dynamically determine when to upload a block ?

root.getOffset() will return the offset currently being written to. But leave a some extra bytes, because that doesn’t include the closing terminators for any open arrays or objects that will get written when you call finishObjectOrArray.

Thanks, its working great now !