how to use strtok func to truncate json response

i need small example of how to use the strtok() fun to truncate a long json to parse it with arduinojson lib?

(deleted)

this didnt help as strtok() work with chars and there are alot of dup of the same char also indexOf() and strstr() doesn't work with json string? or is it me cuz i tried both and found indexof ret -1 and strstr() returns nothing. any idea?

maxwe11:
any idea?

what does your JSON look like?

what bits are you looking to extract?

{
    "coord": {
        "lon": -8.61,
        "lat": 41.15
    },
    "weather": [
        {
            "id": 800,
            "main": "Clear",
            "description": "clear sky",
            "icon": "01d"
        }
    ],
    "base": "stations",
    "main": {
        "temp": 296.15,
        "pressure": 1020,
        "humidity": 69,
        "temp_min": 296.15,
        "temp_max": 296.15
    },
    "visibility": 10000,
    "wind": {
        "speed": 4.6,
        "deg": 320
    },
    "clouds": {
        "all": 0
    },
    "dt": 1499869800,
    "sys": {
        "type": 1,
        "id": 5959,
        "message": 0.0022,
        "country": "PT",
        "sunrise": 1499836380,
        "sunset": 1499890019
    },
    "id": 2735943,
    "name": "Porto",
    "cod": 200
}

i just need the temp and humidity only

I'm guessing that the returned string is larger than your arduinojson library can handle?

yes exactly and thats why trying to truncate it first before i can parse it with the lib

so anyway to truncate the response before i parse it?

maxwe11:
so anyway to truncate the response before i parse it?

I would just parse the enough of the message to be assured I had what I was searching for, and just destroy the rest of the JSON. pseudo code:

char message[BIG_ENOUGH_TO_CONTAIN_MY_DATA + 1];
for (int i = 0; i < BIG_ENOUGH_TO_CONTAIN_MY_DATA; i++)
{
  message[i] = Serial.read();
  delay(10);
}
message[BIG_ENOUGH_TO_CONTAIN_MY_DATA] = '\0';
while (Serial.available())
{
  (void) Serial.read();
  delay(10);
}
goParseMyMessage();

i dont read json from serial its response from the web and here is the code i made just truncating the code is the part i cant get think about.

const unsigned long HTTP_TIMEOUT = 10000;  // max respone time from server
const size_t MAX_CONTENT_SIZE = 512; 

void getTemp(){
char serverName[] = "api.openweathermap.org";
t1.stop(tim);
client2.stop();



 if(client3.connect(serverName, 80)) {
    if(sendRequest(serverName) && skipResponseHeaders()) {
      clientData clientData;
      if(readReponseContent(&clientData)) {
        printclientData(&clientData);
      }
    }
  }

 client3.stop();
  tim = t1.every(10000, getTime);

 
}


bool sendRequest(const char* host) {


  client3.print(F("GET /data/2.5/weather?q=xxxxxx,xxxxx&units=metric&APPID=xxxxx&type=accurate"));
  client3.println(F(" HTTP/1.1"));
  client3.print(F("Host: "));
  client3.println(host);
  client3.println(F("Connection: close"));
  client3.println();

  return true;
}

bool skipResponseHeaders() {
  // HTTP headers end with an empty line
  char endOfHeaders[] = "\r\n\r\n";

  client3.setTimeout(HTTP_TIMEOUT);

  bool ok = client3.find(endOfHeaders);

 
  return ok;
}

bool readReponseContent(struct clientData* clientData) {
  // Compute optimal size of the JSON buffer according to what we need to parse.
  // See https://bblanchon.github.io/ArduinoJson/assistant/
  const size_t bufferSize = JSON_ARRAY_SIZE(1) + JSON_OBJECT_SIZE(1) + 2*JSON_OBJECT_SIZE(2) + JSON_OBJECT_SIZE(4) + JSON_OBJECT_SIZE(5) + JSON_OBJECT_SIZE(6) + JSON_OBJECT_SIZE(12) + 390;
  DynamicJsonBuffer jsonBuffer(bufferSize);

  JsonObject& root = jsonBuffer.parseObject(client3);

  if (!root.success()) {
    Serial.println(F("JSON parsing failed!"));
    return false;
  }

  // Here were copy the strings we're interested in using to your struct data
  strcpy(clientData->temp, root["main"]["temp"]);
 // strcpy(clientData->humidity, root["main"]["humidity"]);
  // It's not mandatory to make a copy, you could just use the pointers
  // Since, they are pointing inside the "content" buffer, so you need to make
  // sure it's still in memory when you read the string

  return true;
}

// Print the data extracted from the JSON
void printclientData(const struct clientData* clientData) {
  Serial.print(F("Temp = "));
  Serial.println(clientData->temp);
  //Serial.print(F("Humidity = "));
  //Serial.println(clientData->humidity);
}

i appreciate ur time and support alot ^^

Max

actually the way u suggested returns squares for some reason and some bit of garbage. does that usually happpens?