title: How to parse specific JSON data into a string in C language
body: <p>I'm currently developing the uptime functionality of my Twitch ChatBot, and i have to deal with the JSON of Twitch API...using just C...with no libraries.</p>
  
  <p>This is the Twitch JSON file that i <code>curl</code> from my ChatBot:</p>
  
  <pre><code>{
      "stream": {
          "_id": 00000000000,
          "game": "Science \u0026 Technology",
          "viewers": 2,
          "video_height": 720,
          "average_fps": 30,
          "delay": 0,
          "created_at": "2019-02-07T12:31:43Z",
          "is_playlist": false,
          "stream_type": "live",
          "preview": {
              "small": "",
              "medium": "",
              "large": "",
              "template": ""
          },
          "channel": {
              "mature": true,
              "partner": false,
              "status": "[ITA]  Twitch Chatbot in C Language | Just Chilling | Ask Questions",
              "broadcaster_language": "it",
              "broadcaster_software": "",
              "display_name": "St3veR0nix",
              "game": "Science \u0026 Technology",
              "language": "it",
              "_id": 000000000,
              "name": "st3ver0nix",
              "created_at": "2016-04-24T11:03:28Z",
              "updated_at": "2019-02-07T14:57:54Z",
              "delay": null,
              "logo": "",
              "banner": null,
              "video_banner": null,
              "background": null,
              "profile_banner": "",
              "profile_banner_background_color": "",
              "url": "",
              "views": 1243,
              "followers": 40,
              "_links": {
                  "self": "",
                  "follows": "",
                  "commercial": "",
                  "stream_key": "",
                  "chat": "",
                  "features": "",
                  "subscriptions": "",
                  "editors": "",
                  "teams": "",
                  "videos": ""
              }
          },
          "_links": {
              "self": ""
          }
      },
      "_links": {
          "self": "",
          "channel": ""
      }
  }
  </code></pre>
  
  <p>The data which i'm looking for is the date when my stream started, so i need the content of the first <code>"created_at"</code> tag. Then i can simply <code>strtok()</code> the content of that tag and get the actual date in the format hours:minutes:seconds.</p>
  
  <p>For now i tried this solution, but it seems that every time the stream gets updated, the JSON file gets somehow modified with my solution that proves not to be the most correct solution.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/wait.h&gt;
  
  
  #define __USE_XOPEN
  #include &lt;time.h&gt;
  
  #define MAX_BUFFER 10000
  
  void Date_Parser(char * buffer, char * output)
  {
      char * tok = (char *)malloc(sizeof(char)*MAX_BUFFER);
  
      int c;
       for(c = 0, tok = strtok(buffer, "created_at:"); tok != NULL; tok = strtok(NULL, "T"), c++)
      {
          if(c == 3)
          {
                tok = strtok(tok, "Z");
                strcpy(output, tok);
                break;
          }
      }
  }
  
  void Find_Time(char * start_date, char * output)
  {
      char * return_value = (char *)malloc(sizeof(char)*64);
      struct tm ttime;
  
      strptime(start_date, "%H:%M:%S", &amp;ttime);
  
      time_t start = mktime(&amp;ttime);
      time_t end;
      time(&amp;end);
  
      time_t seconds = difftime(end, start);
      struct tm * date = localtime(&amp;seconds);
  
      if(date-&gt;tm_hour == 0)
      {
         sprintf(return_value, "%d Hours, %d Minutes, %d Seconds", date-&gt;tm_hour, date-&gt;tm_min, date-&gt;tm_sec); 
      }
      else{
          sprintf(return_value, "%d Hours, %d Minutes, %d Seconds", date-&gt;tm_hour-2, date-&gt;tm_min, date-&gt;tm_sec);
      }
      strcpy(output, return_value);
      return;
  }
  
  char * Date_Calculator()
  {
      pid_t pid;  
      FILE * fp;
      char * buffer = (char *)malloc(sizeof(char)*MAX_BUFFER);
      char * time = (char *)malloc(sizeof(char)*64);
  
      if((pid = fork()) == -1 )
      {
          fprintf(stderr, "\nFork Failed\n");
          exit(EXIT_FAILURE);
      }
      if(pid == 0)
      {
          execl("/usr/bin/curl", "curl", "-H","'Authorization: OAuth nm3ht1an41jay1lp98uafkxp1tpsaf'", "https://api.twitch.tv/kraken/streams/st3ver0nix?client_id=k9z6dtirx92i34vwkufkzfc4mdekx8", "-omy_api.json", NULL);
      }
      sleep(1);
  
      if(!(fp = fopen("my_api.json", "r")))
      {
          fprintf(stderr, "\nErrore in apertura file api\n");
          exit(EXIT_FAILURE);
      }
  
      fseek(fp, 0, SEEK_END);
      int bytes = ftell(fp);
      fseek(fp, 0, SEEK_SET);
  
      fread(buffer, sizeof(char), bytes-1, fp);
  
      char * date = (char *)malloc(sizeof(char)*128);
      Date_Parser(buffer, date);
  
      Find_Time(date, time);
  
      free(date);
      fclose(fp);
  
      wait(0);
      return time;
  }
  </code></pre>
  
  <p>What this code does is: making the request with <code>curl</code> and saving the JSON response into a file, trying to parse the data (which proves not to be correct), it takes the actual date to get the difference from the dates, and returns to the main code the time structure converted into a string.</p>
  
  <p>I would like to hear people opinions about this method, and i'm open for any suggestions, but for making this clear, i don't want to use libraries, i want to do it raw.</p>