Parse a JSON file with Spark Core

Hi guys!

I’m trying to print on a LCD some data which is structured in a JSON file on the Internet.
A browser requesting the URL http://server2.example.com/Users/1234 returns:

{
“Name”: “Foo”,
“Id”: 1234,
“Rank”: 7
}

How could I parse this JSON and display “Rank” field (for example) on a LCD?
I’ve found an Arduino Library called aJson which seems interesting https://github.com/interactive-matter/aJson but I don’t really know if it is Spark compatible or if I need it.

Thanks!

1 Like

Hi! I haven’t had a chance to play around with parsing JSON strings, so if you have any luck with that library let us know.

Something to watch out for (if you decide to go this route for debugging or otherwise) is that there is currently a bug with using strings in the Spark.variable() function… strings greater than 9 characters seem to have adverse effects. However, this is only if you are making your JSON string available through the cloud API.

Post back if you find anything interesting!

@clex I took a look at converting that aJSON for spark core… starts to get involved when it’s looking for Client, Stream and Print classes… also typedef enum and typedef struct don’t seem to work in the web IDE at the moment.

Found a simpler library: http://zserge.bitbucket.org/jsmn.html

But have similar problems with typedefs.

I’m thinking if you know what you want, and you don’t need to absolutely convert the whole JSON object into a structure, you can just buffer your data into a character array or String, and then look for indexOf() the token you are looking for like string.indexOf('Rank"); to get you to the right place… then parse out the bits you need and make conversions on the data (e.g. String to Int, etc…).

This would save a lot of code space… but obviously you have to have specific requirements… like getting the sunset and sunrise unix timestamps from your favorite time keeping website.

Even I wanted to parse this JSON http://open-notify.org/Open-Notify-API/ISS-Pass-Times/ for my ISS tracker using Spark Core. @mohit @zach help!

I did end up creating a simple way to parse JSON… you’ll have to write very specific code to get at the data you want, but it’s pretty easy:

Note there are some problems with the TCP Client right now, so you’ll have to build locally to get this fully working and include all of the files in the GIST in the right directories.

Work is being done by Dave to fix the issues in the Compiler that prevent us from converting the above mentioned libraries. Maybe this week that second library I posted JSMN could be ported easily.

2 Likes

Bdub, using the Web IDE I was able to compile the simpler JSMN json parser you found. If you or anyone else is interested, I put the library and test code on my github.

3 Likes

These JSON Parsing Libraries are huge and not easy to understand. Can someone maybe paste a code that explains json parsing and variable storing of this simple first example?

http://146.185.175.78/get_settings.php

[{"id":"1","timestamp":"1337","username":"kaul"}]

@zach @Dave @mohit Thank you guys!

1 Like

Hi @kaul,

Actually, @peekay123 just posted a port of a JSON parsing library here: https://community.spark.io/t/json-parser-for-spark/2838

Thanks!
David

kaul, take a look at the JSON parser I posted on another thread. It is based on JSMN which was pointed out by BDub. I suggest you look at the jsmntest.cpp code, function test_simple(). The end of that test function shows the simple JSON test string:

js = "{\n \"Day\": 26,\n \"Month\": 9,\n \"Year\": 12\n }";

This is essentially the same JSON message structure you wanted but different objects. The JSMN parser will tag each “token” in the JSON message for your to then take action on. I will try and put a specific demo for you when I get a chance. :smile:

Kaul, I’m almost done with the demo of the parser for the JSON string you supplied. Did you intend on having an array of one object, made up of 3 objects, each being a string? Or did you want three objects of two INT and one STRING like this:

{"id" : 1, "timestamp" : 1337, "username"  : "kaul"}

UPDATE: I have posted the file JSMNDEMO.CPP on my github. I contains code to parse your string and outputs parsing info and the parsed objects.

:smile:

3 Likes

Thank you so much @peekay123. Unfortunately this does not give me any Serial output. Did it work for you?

Kaul, I had the same issue that is why I wrote it the way I did. Once you have terminal open, press any key and it will run once. Pressing a key again and it will run again. I did it this way so setup() or loop() would not hang the wifi waiting for a key. It is nearly impossible to time to the opening of a terminal window with the start of the user sketch!

1 Like

Hey all,

Just wanted to say I was able to get aJSON working by simply changing the following in aJSON.h:

#include "spark_wiring_stream.h"
#include "spark_wiring_print.h"
#include "spark_wiring_client.h"
//#include <Print.h>
//#include <Stream.h>
//#include <Client.h>
//#include <Arduino.h>  // To get access to the Arduino millis() function

I will see about creating a spark-compatible library and adding to the web IDE library list.