Skip to content

Commit ef9cb84

Browse files
authored
Merge pull request #7 from Chester-King/JSON-value-extractor
Added a new Example : JSON Value Extractor
2 parents f4e17ef + 5151c6c commit ef9cb84

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
JSON Value Extractor
3+
4+
This sketch demonstrates how to use some features
5+
of the Official Arduino JSON library to traverse through all the
6+
key value pair in the object and the nested objects.
7+
Can be very helpful when searching for a specific data in a key
8+
which is nested at multiple levels
9+
The sketch actually use recursion to traverse all the keys in
10+
a given JSON.
11+
12+
Example originally added on 24-03-2020
13+
by Madhur Dixit https://github.com/Chester-King
14+
15+
This example code is in the public domain.
16+
*/
17+
18+
#include <Arduino_JSON.h>
19+
20+
void setup() {
21+
22+
Serial.begin(9600);
23+
while (!Serial);
24+
valueExtractor();
25+
26+
}
27+
28+
void loop() {
29+
}
30+
31+
void valueExtractor() {
32+
33+
Serial.println("object");
34+
Serial.println("======");
35+
JSONVar myObject;
36+
37+
// Making a JSON Object
38+
myObject["foo"] = "bar";
39+
myObject["blah"]["abc"] = 42;
40+
myObject["blah"]["efg"] = "pod";
41+
myObject["blah"]["cde"]["pan1"] = "King";
42+
myObject["blah"]["cde"]["pan2"] = 3.14;
43+
myObject["jok"]["hij"] = "bar";
44+
45+
Serial.println(myObject);
46+
Serial.println();
47+
Serial.println("Extracted Values");
48+
Serial.println("======");
49+
50+
objRec(myObject);
51+
52+
}
53+
54+
void objRec(JSONVar myObject) {
55+
Serial.println("{");
56+
for (int x = 0; x < myObject.keys().length(); x++) {
57+
if ((JSON.typeof(myObject[myObject.keys()[x]])).equals("object")) {
58+
Serial.print(myObject.keys()[x]);
59+
Serial.println(" : ");
60+
objRec(myObject[myObject.keys()[x]]);
61+
}
62+
else {
63+
Serial.print(myObject.keys()[x]);
64+
Serial.print(" : ");
65+
Serial.println(myObject[myObject.keys()[x]]);
66+
}
67+
}
68+
Serial.println("}");
69+
}

0 commit comments

Comments
 (0)