turn a json file from http into an integer to use in my program/NodeMCU

I' trying to turn JSON Data I get through an HTTP GET request into a variable I can work with just like any other variable. Im a rather inexperienced programmer and I have searched google for hours but cant figure out how to implement it in my code.
I'd be very happy if someone could help me.

API.ino (2.09 KB)

can you give us an example of the JSON data as you receive it and indicate what values you are looking for?

This is the Json Data I recieve:

{"_disclaimer":"This data is the exclusive property of Tomorrow and/or related parties. If you're in doubt about your rights to use this data, please contact hello@tmrow.com","status":"ok","countryCode":"DE","data":{"carbonIntensity":212.1861638551479,"fossilFuelPercentage":24.364762395715616},"units":{"carbonIntensity":"gCO2eq/kWh"}}

The underlined numbers are what I'm looking for.

you could use strstr() to search for the substring required

int main(void) {
  char data[]="{\"_disclaimer\":\"This data is the exclusive property of Tomorrow and/or related parties. If you're in doubt about your rights to use this data, please contact hello@tmrow.com\",\"status\":\"ok\",\"countryCode\":\"DE\",\"data\":{\"carbonIntensity\":212.1861638551479,\"fossilFuelPercentage\":24.364762395715616},\"units\":{\"carbonIntensity\":\"gCO2eq/kWh\"}}";
  char *ptr=strstr(data,"\"carbonIntensity\":" );
  printf("%s", ptr);
  }

gives

"carbonIntensity":212.1861638551479,"fossilFuelPercentage":24.364762395715616},"units":{"carbonIntensity":"gCO2eq/kWh"}}

alternativly try strtok() to split the string into tokens

Thank you!
but the problem ive ran into over the past 3 days is that I want to update these values every hour or so which i'm doing through the http get request. With the code you gave me I can't update them, can I?

every time after read your JSON text into a char array (of sufficient size) you run the search

using strtok() you look for a token and if found the next is the value you require

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main ()
{
  char str[] =  "{\"_disclaimer\":\"This data is the exclusive property of Tomorrow and/or related parties. If you're in doubt about your rights to use this data, please contact hello@tmrow.com\",\"status\":\"ok\",\"countryCode\":\"DE\",\"data\":{\"carbonIntensity\":212.1861638551479,\"fossilFuelPercentage\":24.364762395715616},\"units\":{\"carbonIntensity\":\"gCO2eq/kWh\"}}";
  char * pch, *lastToken;
  printf ("Splitting string \"%s\" into tokens:\n",str);
  pch = strtok (str,"\":,{}");
  while (pch != NULL)
  {
    printf ("token found %s\n",pch);
    if(strcmp(lastToken,"carbonIntensity") == 0) {
      double x=atof(pch);
      printf("     found %s value %f\n", lastToken, x);
    }
    lastToken= pch;
    pch = strtok (NULL, "\":,{}");
  }
  return 0;
}

gives

Splitting string "{"_disclaimer":"This data is the exclusive property of Tomorrow and/or related parties. If you're in doubt about your rights to use this data, please contact hello@tmrow.com","status":"ok","countryCode":"DE","data":{"carbonIntensity":212.1861638551479,"fossilFuelPercentage":24.364762395715616},"units":{"carbonIntensity":"gCO2eq/kWh"}}" into tokens:
token found _disclaimer
token found This data is the exclusive property of Tomorrow and/or related parties. If you're in doubt about your rights to use this data
token found  please contact hello@tmrow.com
token found status
token found ok
token found countryCode
token found DE
token found data
token found carbonIntensity
token found 212.1861638551479
     found carbonIntensity value 212.186164
token found fossilFuelPercentage
token found 24.364762395715616
token found units
token found carbonIntensity
token found gCO2eq/kWh
     found carbonIntensity value 0.000000

when you find the token carbonIntensity you know the next token is the value you require
the problem being your JSON has two carbonIntensity tokens and the secong one returns 0 as it is not a valid number

I really appreciate your help but I cant get your code to give me an output.
I guess this project was to big of a challenge for me but Ive invested so much time that I really want to see this thing working.

can you post your code and the output (if any) it displays
which code did you try? using stsrstr() or strtok()?
note that my code was run on a PC using printf() - on an arduino you use the serial class

here is an arduino version of the strtok() code

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

void setup()
{
  Serial.begin(115200);
  char str[] =  "{\"_disclaimer\":\"This data is the exclusive property of Tomorrow and/or related parties. If you're in doubt about your rights to use this data, please contact hello@tmrow.com\",\"status\":\"ok\",\"countryCode\":\"DE\",\"data\":{\"carbonIntensity\":212.1861638551479,\"fossilFuelPercentage\":24.364762395715616},\"units\":{\"carbonIntensity\":\"gCO2eq/kWh\"}}";
  char * pch, *lastToken;
  Serial.print ("Splitting string into tokens: ");
  Serial.println(str);
  pch = strtok (str,"\":,{}");
  while (pch != NULL)
  {
    Serial.print ("token found ");
   Serial.println(pch);
    if(strcmp(lastToken,"carbonIntensity") == 0) {
      double x=atof(pch);
      Serial.print ("     found ");
      Serial.print(lastToken);
      Serial.print(" value ");
      Serial.println( x);
    }
    lastToken= pch;
    pch = strtok (NULL, "\":,{}");
  }
}

void loop() {
  // put your main code here, to run repeatedly:

}

gives

Splitting string into tokens: {"_disclaimer":"This data is the exclusive property of Tomorrow and/or related parties. If you're in doubt about your rights to use this data, please contact hello@tmrow.com","status":"ok","countryCode":"DE","data":{"carbonIntensity":212.1861638551479,"fossilFuelPercentage":24.364762395715616},"units":{"carbonIntensity":"gCO2eq/kWh"}}
token found _disclaimer
token found This data is the exclusive property of Tomorrow and/or related parties. If you're in doubt about your rights to use this data
token found  please contact hello@tmrow.com
token found status
token found ok
token found countryCode
token found DE
token found data
token found carbonIntensity
token found 212.1861638551479
     found carbonIntensity value 212.19
token found fossilFuelPercentage
token found 24.364762395715616
token found units
token found carbonIntensity
token found gCO2eq/kWh
     found carbonIntensity value 0.00

Thank you, the output works beautifully but I dont understand how i can turn this value into a normal integer I can use in my project like I would any other integer.

in the JSON the value carbonIntensity is 212.1861638551479 which looks like a floating point number not an integer (if you require an integer cast the float to int)
updated code to store value read into carbonIntensity variable
note before calling atof() to convert the string into a floating point the token is checked to make sure it starts with a digit

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

void setup()
{
  float carbonIntensity=0.0f;    //  <<< define variable to hole carbonIntensity value
  Serial.begin(115200);
  char str[] =  "{\"_disclaimer\":\"This data is the exclusive property of Tomorrow and/or related parties. If you're in doubt about your rights to use this data, please contact hello@tmrow.com\",\"status\":\"ok\",\"countryCode\":\"DE\",\"data\":{\"carbonIntensity\":212.1861638551479,\"fossilFuelPercentage\":24.364762395715616},\"units\":{\"carbonIntensity\":\"gCO2eq/kWh\"}}";
  char * pch, *lastToken;
  Serial.print ("Splitting string into tokens: ");
  Serial.println(str);
  pch = strtok (str,"\":,{}");
  while (pch != NULL)
  {
    Serial.print ("token found ");
    Serial.println(pch);
    // check if last token is carbonIntensity and currect token starts with a digit
    if(strcmp(lastToken,"carbonIntensity") == 0 && isdigit(pch[0])) {
      carbonIntensity=atof(pch);              // <<<<  set up value of carbonIntensity
      Serial.print ("     found ");  Serial.print(lastToken);
      Serial.print(" value ");       Serial.println( carbonIntensity);
    }
    lastToken= pch;
    pch = strtok (NULL, "\":,{}");
  }
  Serial.print("\nresult carbonIntensity =  ");  Serial.println( carbonIntensity);
  
}

void loop() {}

gives

Splitting string into tokens: {"_disclaimer":"This data is the exclusive property of Tomorrow and/or related parties. If you're in doubt about your rights to use this data, please contact hello@tmrow.com","status":"ok","countryCode":"DE","data":{"carbonIntensity":212.1861638551479,"fossilFuelPercentage":24.364762395715616},"units":{"carbonIntensity":"gCO2eq/kWh"}}
token found _disclaimer
token found This data is the exclusive property of Tomorrow and/or related parties. If you're in doubt about your rights to use this data
token found  please contact hello@tmrow.com
token found status
token found ok
token found countryCode
token found DE
token found data
token found carbonIntensity
token found 212.1861638551479
     found carbonIntensity value 212.19
token found fossilFuelPercentage
token found 24.364762395715616
token found units
token found carbonIntensity
token found gCO2eq/kWh

result carbonIntensity =  212.19

That helped immensly but the whole point of my code is that I want to update the float regulary and right now its defined in the char array. Since I get the Json data everytime I run the code I feel like that should somehow be possible.

API.ino (3.47 KB)

I assume you are reading your text into a char array to give you a line of JSON such as

  char str[] =  "{\"_disclaimer\":\"This data is the exclusive property of Tomorrow and/or related parties. If you're in doubt about your rights to use this data, please contact hello@tmrow.com\",\"status\":\"ok\",\"countryCode\":\"DE\",\"data\":{\"carbonIntensity\":212.1861638551479,\"fossilFuelPercentage\":24.364762395715616},\"units\":{\"carbonIntensity\":\"gCO2eq/kWh\"}}";

where my code when tokenising uses the char array str replace it with the name of your char array

Thats the problem, I dont have one:/ I sent my code in the last reply.

reading JSON from serial into a char array

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

void setup()
{
  float carbonIntensity=0.0f;    //  <<< define variable to hole carbonIntensity value
  Serial.begin(115200);
  Serial.setTimeout(100000ul);
  char str[500]={0};
  Serial.readBytesUntil('\n', str, 500);      // read JSON from Serial into str
  char * pch, *lastToken;
  Serial.print ("Splitting string into tokens: ");
  Serial.println(str);
  pch = strtok (str,"\":,{}");
  while (pch != NULL)
  {
    Serial.print ("token found ");
    Serial.println(pch);
    // check if last token is carbonIntensity and current token starts with a digit
    if(strcmp(lastToken,"carbonIntensity") == 0 && isdigit(pch[0])) {
      carbonIntensity=atof(pch);              // <<<<  set up value of carbonIntensity
      Serial.print ("     found ");  Serial.print(lastToken);
      Serial.print(" value ");       Serial.println( carbonIntensity);
    }
    lastToken= pch;
    pch = strtok (NULL, "\":,{}");
  }
  Serial.print("\nresult carbonIntensity =  ");  Serial.println( carbonIntensity);
  
}

void loop() {}

I send it the file

{"_disclaimer":"This data is the exclusive property of Tomorrow and/or related parties. If you're in doubt about your rights to use this data, please contact hello@tmrow.com","status":"ok","countryCode":"DE","data":{"carbonIntensity":212.1861638551479,"fossilFuelPercentage":24.364762395715616},"units":{"carbonIntensity":"gCO2eq/kWh"}}

and the result is

Splitting string into tokens: {"_disclaimer":"This data is the exclusive property of Tomorrow and/or related parties. If you're in doubt about your rights to use this data, please contact hello@tmrow.com","status":"ok","countryCode":"DE","data":{"carbonIntensity":212.1861638551479,"fossilFuelPercentage":24.364762395715616},"units":{"carbonIntensity":"gCO2eq/kWh"}}

token found _disclaimer
token found This data is the exclusive property of Tomorrow and/or related parties. If you're in doubt about your rights to use this data
token found  please contact hello@tmrow.com
token found status
token found ok
token found countryCode
token found DE
token found data
token found carbonIntensity
token found 212.1861638551479
     found carbonIntensity value 212.19
token found fossilFuelPercentage
token found 24.364762395715616
token found units
token found carbonIntensity
token found gCO2eq/kWh
token found


result carbonIntensity =  212.19

therefore in your code after you have skipped the headers you read the incomming JSON into a char array and then tokenise it, e.g. something along the lines of

 // Skip HTTP headers
  char endOfHeaders[] = "\r\n\r\n";
  if (!client.find(endOfHeaders))
  {
    Serial.println(F("Invalid response"));
    return;
  }
  float carbonIntensity=0.0f;    //  <<< define variable to hole carbonIntensity value
  char str[500]={0};
  int index=0;
    // read each byte, send to the serial monitor and store in str[] until end of line
   while (client.available()) {
      char c = 0;
      client.readBytes(&c, 1);
      Serial.print(c);
      if(c != '\n')  str[index++]=c;
      else           break;
      }

  
  char * pch, *lastToken;
  Serial.print ("Splitting string into tokens: ");
  Serial.println(str);
  pch = strtok (str,"\":,{}");
  while (pch != NULL)
  {
    Serial.print ("token found ");
    Serial.println(pch);
    // check if last token is carbonIntensity and currect token starts with a digit
    if(strcmp(lastToken,"carbonIntensity") == 0 && isdigit(pch[0])) {
      carbonIntensity=atof(pch);              // <<<<  set up value of carbonIntensity
      Serial.print ("     found ");  Serial.print(lastToken);
      Serial.print(" value ");       Serial.println( carbonIntensity);
    }
    lastToken= pch;
    pch = strtok (NULL, "\":,{}");
  }
  Serial.print("\nresult carbonIntensity =  ");  Serial.println( carbonIntensity);

hope that makes sense

Thank you so much!
Ive read just about every google entry about this topic and just couldnt get it to work but thats exactly what i wanted to have.
Thank you <3