HTTP get -> Json Parser

Hey guys, I am relatively new to the Particle environment / IoT so I am struggling with some of the stuff.

I got the HttpClient “get” function working which returns a JSON String (response.body) from my works API.

for example
[{“id”:2003,“name”:“item1”,“description”:null,“status”:“current”},{“id”:13213,“name”:“item2”,“description”:null,“status”:“current”}]

I am finding it hard to parse this and store them as objects.(which then ill display on my tft screen) Could someone point me in the right direction?

Kind Regards,
Particle Fan 1

Have you had a look at this library?
https://build.particle.io/libs/JsonParserGeneratorRK/0.0.5

2 Likes

Yes, I have I am able to Parse Json string using @rickkas7 (really good library, easy to understand) . However it is unsuccessful when parsing an array of JSON [{“id”:2003,“name”:“item1”},{“id”:13213,“name”:“item2"]

Here is my code, any help would be greatly appreciated

#include "Particle.h"

#include "JsonParserGeneratorRK.h"

const unsigned long TEST_RUN_PERIOD_MS = 10000;
unsigned long lastRun = 0;

void runTest();

const char * const test2 = "[{\"id\":2003,\"name\":\"item1\",\"description\":null,\"status\":\"current\"},{\"id\":13213,\"name\":\"item1\",\"description\":null,\"status\":\"current\"}]";

// Global parser that supports up to 256 bytes of data and 20 tokens
JsonParser parser1;

void setup() {
	Serial.begin(9600);
}

void loop() {
	if (millis() - lastRun >= TEST_RUN_PERIOD_MS) {
		lastRun = millis();
		runTest();
	}
}

void runTest() {
	// Clear the parser state, add the string test2, and parse it
	parser1.clear();
	parser1.addString(test2);
	if (!parser1.parse()) {
		Serial.println("parsing failed test2");
		return;
	}

  int intValue;
  if (!parser1.getOuterValueByKey("id", intValue)) {
  	Serial.println("failed to get test2 id");
  	return;
  }
  Serial.println(intValue);
  if (intValue != 100858) {
  	Serial.printlnf("wrong value test2 t2 was %d", intValue);
  	return;
  }
  intValue = -1;

	
}

That's not valid JSON. It should be:

[{"id":2003,"name":"item1"},{"id":13213,"name":"item2"}]

You're missing a } before the last ]

Sorry I have that in the code, I must of accidentally deleted when "bolding" it for this forum. Still seems to fail when parsing.

Since the outer object is an array, not an object, the first thing you need is an index so it knows which element of the array you want:

parser.getReference().index(0).key("id").valueInt()
2 Likes

OMG you are an awesome man!

//Here is my code for those stuck and are in the position I was. thanks @rickkas7

#include "Particle.h"

#include "JsonParserGeneratorRK.h"
const unsigned long TEST_RUN_PERIOD_MS = 10000;
unsigned long lastRun = 0;

void runTest();

const char * const test2 = "[{\"id\":2003,\"name\":\"item1\"},{\"id\":13213,\"name\":\"item2\"}]";

// Global parser that supports up to 256 bytes of data and 20 tokens
JsonParser parser1;

void setup() {
Serial.begin(9600);
}

void loop() {
if (millis() - lastRun >= TEST_RUN_PERIOD_MS) {
lastRun = millis();
runTest();
}
}

void runTest() {
// Clear the parser state, add the string test2, and parse it
parser1.clear();
parser1.addString(test2);
if (!parser1.parse()) {
Serial.println("parsing failed test2");
return;
}

int intValue;
intValue = parser1.getReference().index(0).key("id").valueInt();
Serial.print("The first id is ");
Serial.println(intValue);

intValue = parser1.getReference().index(1).key("id").valueInt();
Serial.print("The second id is ");
Serial.println(intValue);

String strValue;
strValue = parser1.getReference().index(0).key("name").valueString();
Serial.print("The first name is ");
Serial.println(strValue);

strValue = parser1.getReference().index(1).key("name").valueString();
Serial.print("The second name is ");
Serial.println(strValue);


intValue = -1;

}

Hey @rickkas7, Followup question: Is there a way for the parser to return the number of index(s) . I tried to look through the library and couldnt find any function for it. Any help from anyone would be appreciated

In your example above, the size of the outer array would be:

parser1.getReference().size();

That would return 2.

4 Likes

There’s new documentation for JsonParserGeneratorRK now!

Every function is now documented in the browsable online documentation.

6 Likes

LEGEND! <3