user888: 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:
http://arduino.cc/en/Tutorial/WiFiWebClientRepeating
Here is what I get printed back from the serial port
connecting...
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

19
{"lightstatus":"on"}
0

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

user3186: 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:

Code: https://github.com/cloud-rocket/arduino-json-rpc
Documentation: http://www.cloud-rocket.com/2014/03/serial-json-rpc-server-arduino/


user168: 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.

user655: 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()){
      jsonreader.process_char(c);
}

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.

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

aJSON
Arduino JSON Parser
JSON 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:
{"id":"TEST1","lat":"38.56050207","lng":"-121.42158374","total":"3","available":"2"}

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 == '}') {
        break;
    }

    delay(1);
}

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.

888: <p>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:</p>

<p><a href="http://arduino.cc/en/Tutorial/WiFiWebClientRepeating">http://arduino.cc/en/Tutorial/WiFiWebClientRepeating</a></p>

<p>Here is what I get printed back from the serial port</p>

<pre><code>connecting...
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

19
{"lightstatus":"on"}
0
</code></pre>

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

<p>Thanks</p>
 3186: <p>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:</p>

<ul>
<li>Code: <a href="https://github.com/cloud-rocket/arduino-json-rpc" rel="nofollow">https://github.com/cloud-rocket/arduino-json-rpc</a></li>
<li>Documentation: <a href="http://www.cloud-rocket.com/2014/03/serial-json-rpc-server-arduino/" rel="nofollow">http://www.cloud-rocket.com/2014/03/serial-json-rpc-server-arduino/</a></li>
</ul>
 168: <p>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:<a href="https://github.com/EternityForest/OpenFortune-fortune-like-text-generator" rel="nofollow">https://github.com/EternityForest/OpenFortune-fortune-like-text-generator</a>
You'll need to look in libfortune which is part of the C version.</p>
 655: <p>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.</p>

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

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

cout &lt;&lt; jsonreader.results[0] &lt;&lt; endl;
cout &lt;&lt; jsonreader.results[1] &lt;&lt; endl;
</code></pre>

<p>Check this blog post I just wrote: <a href="http://web.biicode.com/blog/big-json-on-arduino">http://web.biicode.com/blog/big-json-on-arduino</a>.</p>

<p>Source code is <a href="https://www.biicode.com/lasote/blocks/lasote/stream_json_reader/branches/master">here</a> in Biicode.</p>
 11: <p>I'm aware of a few JSON parsing libraries for Arduino.</p>

<ul>
<li><a href="https://github.com/interactive-matter/aJson">aJSON</a></li>
<li><a href="https://github.com/bblanchon/ArduinoJsonParser">Arduino JSON Parser</a></li>
<li><a href="https://github.com/not404/json-arduino">JSON Arduino</a></li>
</ul>

<p>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.</p>

<p>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 <em>very</em> limited but if that's all you're parsing then it'll work.</p>

<p>Example of JSON response from server:</p>

<pre><code>{"id":"TEST1","lat":"38.56050207","lng":"-121.42158374","total":"3","available":"2"}
</code></pre>

<p>First, only read data between curly braces.</p>

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

    char in = SIM900.read();

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

    if (begin) response += (in);

    if (in == '}') {
        break;
    }

    delay(1);
}
</code></pre>

<p>This code reads data one byte at a time and once it gets an open brace, it starts saving it into <code>response</code>. 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.</p>

<p>Once I have the string, I use <code>indexOf</code> and <code>substring</code> to extract relevant information:</p>

<pre><code>start = response.indexOf("id\":\"") + 5;
end = start + 5;
nodeId = response.substring(start, end);
</code></pre>

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

<p>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.</p>
