Hi i'm trying to parse JSON resposne from webserver in my arduino in order to turn on and off a LED light. I'm using the wifi client repeating example to make a get request to my server:


Here is what I get printed back from the serial port

HTTP/1.1 200 OK
Date: Sun, 06 Apr 2014 01:14:37 GMT
Server: Apache
X-Powered-By: PHP/5.5.10
Cache-Control: no-cache
X-Frame-Options: SAMEORIGIN
Set-Cookie: expires=Sun, 06-Apr-2014 03:14:37 GMT; Max-Age=7200; path=/; httponly
Connection: close
Transfer-Encoding: chunked
Content-Type: application/json


How do i parse the JSON portion of this response only so that I can use it to control my LED?



I'm aware of a few JSON parsing libraries for Arduino.

I've never used any of these but I did do some simple JSON parsing in a project I'm working on so I'll show you how I did that.

NOTE: I'm reading serial data using the software serial library. You'll need to change this code to work for you. This will only work on very simple JSON strings. It's very limited but if that's all you're parsing then it'll work.

Example of JSON response from server:


First, only read data between curly braces.

String response = "";
bool begin = false;
while (SIM900.available() || !begin) {

    char in = SIM900.read();

    if (in == '{') {
        begin = true;

    if (begin) response += (in);

    if (in == '}') {


This code reads data one byte at a time and once it gets an open brace, it starts saving it into response. When it gets a closing brace, it ends it. So here's a clear limitation, you can only have one set of opening/closing braces in your string.

Once I have the string, I use indexOf and substring to extract relevant information:

start = response.indexOf("id\":\"") + 5;
end = start + 5;
nodeId = response.substring(start, end);

This code sets start to the beginning of id":" + 5 characters in the string. It's +5 because that's how long id":" is. So start points to TEST1 in the JSON string. In my system, the ID is always going to be 5 characters long so end is start + 5. I then use substring to extract that.

Again, before anyone starts down voting me for this horrible solution: if you know exactly what you're working with, and understand the limitations of this code, then this is not a bad solution. It's a solution that gets the job done.

| improve this answer | |
  • The JSON example you give does not look like "good" JSON, e.g. numeric values should normally not be quoted as strings. – jfpoilpret Apr 6 '14 at 5:55
  • +1 for the list of potentially usable libraries for JSON. I am surprised no library is based on a callback function approach, that would be called during parsing (much more efficient memory-wise). – jfpoilpret Apr 6 '14 at 5:59
  • At least you're conscious that your code sample is horrible:-) Of course it gets the job done, but it is not readable (by someone else qnd even by yourself in 6 months) and hence not maintainable. – jfpoilpret Apr 6 '14 at 6:03
  • @jfpoilpret I'm out of memory on the chip so I've had to cut corners in some places. At least it's heavily commented. – sachleen Apr 6 '14 at 6:40
  • I understand; but I wonder if String usage does not make the memory situation worse (yes, String in Arduino is my pet peeve). – jfpoilpret Apr 6 '14 at 6:44

I have programmed a class that will receive char by char the JSON document. It only will store in memory a few bytes for known JSON structure using a state machine and the results you need. So you can query the class for the results you want and will process the JSON.

Its ideal for your purpose. I have used for connecting to a weather service that returns a json:

static const char* queries[] = { "list.0.deg", "list.0.weather.0.main"};
StreamJsonReader jsonreader(queries, 2); // 2 queries    
while(char c  =  read()){

cout << jsonreader.results[0] << endl;
cout << jsonreader.results[1] << endl;

Check this blog post I just wrote: http://web.biicode.com/blog/big-json-on-arduino.

Source code is here in Biicode.

| improve this answer | |

I wrote a parser for the PIC that has very low ram use because it works directly with char pointers and does not build a tree structure. If you ask to get item N of a list, it gives you an actual pointer into the direct JSON file right at the start of item N of the list. Then you can ask what type of object the pointer represents, etc. It's buggy and doesn't support floats and I think has a few other limitations(twas a long time ago) but it's all on github:https://github.com/EternityForest/OpenFortune-fortune-like-text-generator You'll need to look in libfortune which is part of the C version.

| improve this answer | |

I wrote a simple JSON-RPC library (on top of aJson) with example of how to turn a led on with JSON-RPC procedure call:

| improve this answer | |
  • 2
    Can you add some more information into your answer in case the link to the code/documentation changes... – sachleen Jul 16 '14 at 16:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.