parsing json data in arduino

hi,
I am connecting to web service using arduino wi-fi sheild and receiving data from it. i am using following code to do it
if (client.connect(server, 80)) {
client.println("GET URL HTTP/1.0");
client.println();
}
char json[] = {};
char c ;
while (client.available()) {
c = client.read();
}
json[] = c;
when i am assigning my output to a char array for parsing it i am getting following errror
error: expected primary-expression before ']' token

Thanks in advance

try putting all chars in the array :wink:

 char c ;
int index = 0;

while (client.available()) 
{
    c = client.read();
    json[index] = c;
    index++;
}

hi,
Thanks for your quick reply.............now error is not coming but some junk characters are getting added at the end of array. can you please suggest any solution for this ?

add a terminating \0 char to indicate end of string (might be the cause)

char c ;
int index = 0;

while (client.available()) 
{
    c = client.read();
    json[index] = c;
    index++;
}
json[index] = '\0';

hi,
I am using following library 'GitHub - bblanchon/ArduinoJson: 📟 JSON library for Arduino and embedded C++. Simple and efficient.' for parsing json data received from web service. here i am using WiFi sheild to call my web service and the Data received is

"{"productCode":4002,"productName":"Apples","Weight":250","minOSA":6}"

i am storing this data in a results[]. when i pass this char array as input to the following library it is giving parsing failed.

can any one please help me to solve this issue....

Thanks in advance.......

can any one please help me to solve this issue....

Fix line 42.

If that doesn't work, there is something wrong with your code. You'll need to print it out, and hold it up in front of your web cam until we get around to reading it.

hi,
Thanks for your reply. sorry to say that i did not understand what you are saying. can you please elaborate your answer ?. The output i am receiving from web service is :

"{"productCode":4002,"productName":"Apples","Weight":250","minOSA":6}"

i am storing it in a char array before giving it as input to the following library 'GitHub - bblanchon/ArduinoJson: 📟 JSON library for Arduino and embedded C++. Simple and efficient.'. then i am getting parsing failed.

if initialize a char array with the same output as shown in their example 'ArduinoJson/JsonParserExample.ino at 6.x · bblanchon/ArduinoJson · GitHub' then it is able to parse it.

please help to solve this issue..........

sorry to say that i did not understand what you are saying. can you please elaborate your answer ?.

Enough hinting, then.

POST YOUR CODE!

My Code:

#include <SPI.h>
#include <WiFi.h>
#include <JsonParser.h>
char ssid[] = "AndroidAp";    
char pass[] = "saisri123"; 
int status = WL_IDLE_STATUS;
IPAddress server(202,91,136,148); 
WiFiClient client;
void setup(){
    Serial.begin(9600);
    Serial.println("in setup");
    Serial.println("Attempting to connect to network...");
    Serial.print("SSID: ");
    Serial.println(ssid);
    status = WiFi.begin(ssid, pass);
    String fv = WiFi.firmwareVersion();
    Serial.println(fv);
    if ( status != WL_CONNECTED) {
      Serial.println("Couldn't get a wifi connection");
      while(true);
    }
    else {
      Serial.println("Connected to wifi network");
     }
  }
  
 void loop(){
   if (client.connect(server, 80)) {
        Serial.println("connected to server");
        client.println("GET /iSmartStoreWS/smartStore/ProductCatalogWS/getProductCatalog/2002  HTTP/1.0");
        client.println();
      }
   JsonParser<32> parser;
   String response = "";
   char results[] = {};
   char c ;
   int index = 0;
   while (client.available()) 
    {
      c = client.read();
      results[index] = c;
      index++;
  }
  results[index] = '\0';
  char json[] = {};
  int j = 0;
  for(int i = 128;i < index;i++){
   json[j]=results[i];
   j++;
 } 
   //Serial.print("results");
  Serial.println(json);
 delay(10000);
 //char test[] = "{\"productCode\":4002,\"productName\":\"Apples\",\"Weight\":250\",\"minOSA\":6}";

JsonHashTable hashTable = parser.parseHashTable(json);

if (!hashTable.success())
    {
        Serial.println("JsonParser.parseHashTable() failed");
       
    }else{
      Serial.println("sucess");
    }
}

moderatore update: added code tags -> # button above smileys.

    String fv = WiFi.firmwareVersion();
    Serial.println(fv);

Why are you pissing away resources on the String class? Pay some attention to what the functions you are using actually return.

        Serial.println("connected to server");
        client.println("GET /iSmartStoreWS/smartStore/ProductCatalogWS/getProductCatalog/2002  HTTP/1.0");

Those string literals get stored in SRAM, unnecessarily. Save your SRAM for useful stuff:
Serial.println(F("connected to server"));
client.println(F("GET /iSmartStoreWS/smartStore/ProductCatalogWS/getProductCatalog/2002 HTTP/1.0"));

   char results[] = {};

How many characters can you store in this array? You lied to the compiler, telling it that you would provide enough information for it to determine the size of the array, when you knew you had no intention of doing so.

      results[index] = c;

You just shit on memory you do not own. NOTHING else that happens from here on is guaranteed to work.

Therefore, I quit reading here.

hi ,
is this problem is due to memory issue ?. i wanted to store the output coming from web service in char array and the data may vary from time to time. so i doesn't know exactly how much space is required. so i didn't specify the size of char array.

hi ,
is this problem is due to memory issue ?. i wanted to store the output coming from web service in char array and the data may vary from time to time. so i doesn't know exactly how much space is required. so i didn't specify the size of char array.

That's like saying you need a bucket, but you don't know how big a bucket you will need, so you get a thimble instead, thinking that that will be big enough.

You MUST specify a size for the array. You need to determine the maximum amount of data you will handle. That might be 20 characters. It might be 100. Whatever size it is, you simply throw away whatever won't fit.

The less SRAM you waste on string literals and String crap, the more you can use to store the data that you need to parse.

Thanks for your replies. I will try to modify the code as you have said and check whether that will solve my issue.

hi,
I have modified my code as you suggested and please correct me if some part of code is to be modified but sill i am facing same issue. i am posting modified code please help in solving this issue.

#include <SPI.h>
#include <WiFi.h>
#include <JsonParser.h>
char ssid[] = "AndroidAP";    
char pass[] = "inode@123"; 
int status = WL_IDLE_STATUS;
IPAddress server(202,91,136,148); 
WiFiClient client;
void setup(){
    Serial.begin(9600);
    Serial.println(F("Attempting to connect to network..."));
    status = WiFi.begin(ssid, pass);
    if ( status != WL_CONNECTED) {
      Serial.println(F("Couldn't get a wifi connection"));
      while(true);
    }
    else {
      Serial.println(F("Connected to wifi network"));
     }
  }
  
 void loop(){
   if (client.connect(server, 80)) {
        Serial.println(F("connected to server"));
        client.println(F("GET /iSmartStoreWS/smartStore/ProductCatalogWS/getProductCatalog/2002  HTTP/1.0"));
        client.println();
      }
   JsonParser<32> parser;
   char results[250] = {};
   char c ;
   int index = 0;
   while (client.available()){
      c = client.read();
      results[index] = c;
      index++;
  }
  results[index] = '\0';
  char json[150] = {};
  int j = 0;
  for(int i = 128;i < index;i++){
    if(results[index] != ';'){
   json[j]=results[i];
    }
   j++;
 } 
 Serial.println(json);
 delay(10000);
 //char test[] = "{\"productCode\":4002,\"productName\":\"Apples\",\"Weight\":250\",\"minOSA\":6}";
 JsonHashTable hashTable = parser.parseHashTable(json);
  if (!hashTable.success()){
        Serial.println(F("JsonParser.parseHashTable() failed"));
  }else{
      Serial.println(F("sucess"));
  }
}
   while (client.available()){
      c = client.read();
      results[index] = c;
      index++;
  }
  results[index] = '\0';

When this bit of code finishes, what is in results?

  for(int i = 128;i < index;i++){
    if(results[index] != ';'){
   json[j]=results[i];
    }

If you put each { on a new line, you'd see what is wrong with this bit of code. When SHOULD j be incremented? When IS j incremented?

 Serial.println(json);
 delay(10000);

We've got something to parse (maybe). So, lets wait 10 seconds. What possible reason is there for waiting?

Value in results is :

HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Type: text/plain
Date: Fri, 04 Jul 2014 19:12:07 GMT
Connection: close
"{\"productCode\":4002,\"productName\":\"Apples\",\"Weight\":250\",\"minOSA\":6}"

In this piece of code i am assigning the value of results to json by eliminating HTTP response header.

for(int i = 128;i < index;i++){
    json[j]=results[i];
}

When SHOULD j be incremented? When IS j incremented?

You didn't answer these.

I thought you might take the hint that it is potentially necessary to print all intermediate variables, until you discover what the problem is. Since you didn't, I'll ask explicitly. What is in json when that for loop ends? Is it what you expect?

for(int i = 128;i < index;i++){
    json[j]=results[i];
    j++;
}

j is incremented to push the values of results char array to json char array. so j is incremented in for loop until all the values in results char array is pushed to json char array. i have moved the output from results char array to json char array to eliminate http response header.

Value in results char array is :

HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Type: text/plain
Date: Fri, 04 Jul 2014 19:12:07 GMT
Connection: close
"{\"productCode\":4002,\"productName\":\"Apples\",\"Weight\":250\",\"minOSA\":6}"

Value in json char array is :

"{\"productCode\":4002,\"productName\":\"Apples\",\"Weight\":250\",\"minOSA\":6}"

Now in json char array i got my json response to be parsed. when i give this as input to the following library "GitHub - bblanchon/ArduinoJson: 📟 JSON library for Arduino and embedded C++. Simple and efficient.". i am facing issue for parsing the data.

Now in json char array i got my json response to be parsed. when i give this as input to the following library "GitHub - bblanchon/ArduinoJson: 📟 JSON library for Arduino and embedded C++. Simple and efficient.". i am facing issue for parsing the data.

But you still haven't said what the issue is. I'm tired of guessing.

Issue now is i want to split the output which is in json char array in to key value pairs using the library i mentioned earlier.
For example :
Product code:4001
Weight:250

I want to split Json data which is in json char array like this.