title: Is there a way to know which is the last token when iterating and parsing a JSON string in C#?
body: <p>I'm parsing some JSON data that I have on a string, to pass it to a file with a custom format. 
  But I need to know which is the last token of the file, so that I close the tags with "}" instead of "}," (that would be, without the comma to have a correct syntax).</p>
  
  <p>I tried to get the last token using the <code>Last</code> property, but it doesn't work as I thought.</p>
  
  <p>This is the function I used to parse the data and write the file (it also prints the desired data to the terminal, but not in JSON data, that's why I'm parsing it first):</p>
  
  <pre><code>private static void ParseData()
          {
              JObject jsonData;
  
              try
              {
                  using (StreamWriter file = new StreamWriter(Globals.dataFile))
                  {
                      file.WriteLine("{");
  
                      jsonData = JObject.Parse(Globals.json);
                      Console.WriteLine("*********************************************");
                      foreach (JToken tokens in jsonData["indicator"]["values"])
                      {
                          Console.WriteLine("Fecha y hora: " + tokens["datetime"] + " Valor: " + tokens["value"] + Environment.NewLine + "*********************************************");
                          file.Write(Environment.NewLine + "\t{" + Environment.NewLine + "\t\t\"datetime\": " + tokens["datetime"] + ",");
                          file.WriteLine("\t\t\"value\": " + tokens["value"]);
                          if (tokens != tokens.Last)
                          {
                              file.WriteLine("\t},");
                              Console.WriteLine("\t},");
                          }
                          else
                          {
                              Console.WriteLine("\t}");
                          }
                      }
  
                      file.WriteLine("}");
                  }
              }
              catch (Exception ex)
              {
                  PrintExceptionMessage(ex);
              }
          }
  </code></pre>
  
  <p>And this is the format of the JSON file that I'm formatting:</p>
  
  <pre><code>{
      "indicator": {
          "name": "Precio medio horario final contratación libre suma de componentes",
          "short_name": "Precio medio horario final c.libre suma",
          "id": 10214,
          "composited": true,
          "step_type": "linear",
          "disaggregated": false,
          "magnitud": [{
                  "name": "Precio",
                  "id": 23
              }
          ],
          "tiempo": [{
                  "name": "Hora",
                  "id": 4
              }
          ],
          "geos": [{
                  "geo_id": 8741,
                  "geo_name": "Península"
              }
          ],
          "values_updated_at": "2019-01-31T08:05:32.000+01:00",
          "values": [{
                  "value": 49.38,
                  "datetime": "2019-01-31T01:00:00.000+01:00",
                  "datetime_utc": "2019-01-31T00:00:00Z",
                  "tz_time": "2019-01-31T00:00:00.000Z",
                  "geo_id": 8741,
                  "geo_name": "Península"
              }, {
                  "value": 47.46,
                  "datetime": "2019-01-31T02:00:00.000+01:00",
                  "datetime_utc": "2019-01-31T01:00:00Z",
                  "tz_time": "2019-01-31T01:00:00.000Z",
                  "geo_id": 8741,
                  "geo_name": "Península"
              }, {
                  "value": 46.82,
                  "datetime": "2019-01-31T03:00:00.000+01:00",
                  "datetime_utc": "2019-01-31T02:00:00Z",
                  "tz_time": "2019-01-31T02:00:00.000Z",
                  "geo_id": 8741,
                  "geo_name": "Península"
              }, {
                  "value": 46.5,
                  "datetime": "2019-01-31T04:00:00.000+01:00",
                  "datetime_utc": "2019-01-31T03:00:00Z",
                  "tz_time": "2019-01-31T03:00:00.000Z",
                  "geo_id": 8741,
                  "geo_name": "Península"
              }, {
                  "value": 47.36,
                  "datetime": "2019-01-31T05:00:00.000+01:00",
                  "datetime_utc": "2019-01-31T04:00:00Z",
                  "tz_time": "2019-01-31T04:00:00.000Z",
                  "geo_id": 8741,
                  "geo_name": "Península"
              }, {
                  "value": 50.05,
                  "datetime": "2019-01-31T06:00:00.000+01:00",
                  "datetime_utc": "2019-01-31T05:00:00Z",
                  "tz_time": "2019-01-31T05:00:00.000Z",
                  "geo_id": 8741,
                  "geo_name": "Península"
              }, {
                  "value": 56.61,
                  "datetime": "2019-01-31T07:00:00.000+01:00",
                  "datetime_utc": "2019-01-31T06:00:00Z",
                  "tz_time": "2019-01-31T06:00:00.000Z",
                  "geo_id": 8741,
                  "geo_name": "Península"
              }, {
                  "value": 65.58,
                  "datetime": "2019-01-31T08:00:00.000+01:00",
                  "datetime_utc": "2019-01-31T07:00:00Z",
                  "tz_time": "2019-01-31T07:00:00.000Z",
                  "geo_id": 8741,
                  "geo_name": "Península"
              }, {
                  "value": 66.59,
                  "datetime": "2019-01-31T09:00:00.000+01:00",
                  "datetime_utc": "2019-01-31T08:00:00Z",
                  "tz_time": "2019-01-31T08:00:00.000Z",
                  "geo_id": 8741,
                  "geo_name": "Península"
              }, {
                  "value": 66.49,
                  "datetime": "2019-01-31T10:00:00.000+01:00",
                  "datetime_utc": "2019-01-31T09:00:00Z",
                  "tz_time": "2019-01-31T09:00:00.000Z",
                  "geo_id": 8741,
                  "geo_name": "Península"
              }, {
                  "value": 65.54,
                  "datetime": "2019-01-31T11:00:00.000+01:00",
                  "datetime_utc": "2019-01-31T10:00:00Z",
                  "tz_time": "2019-01-31T10:00:00.000Z",
                  "geo_id": 8741,
                  "geo_name": "Península"
              }, {
                  "value": 63.71,
                  "datetime": "2019-01-31T12:00:00.000+01:00",
                  "datetime_utc": "2019-01-31T11:00:00Z",
                  "tz_time": "2019-01-31T11:00:00.000Z",
                  "geo_id": 8741,
                  "geo_name": "Península"
              }, {
                  "value": 61.94,
                  "datetime": "2019-01-31T13:00:00.000+01:00",
                  "datetime_utc": "2019-01-31T12:00:00Z",
                  "tz_time": "2019-01-31T12:00:00.000Z",
                  "geo_id": 8741,
                  "geo_name": "Península"
              }, {
                  "value": 56.58,
                  "datetime": "2019-01-31T14:00:00.000+01:00",
                  "datetime_utc": "2019-01-31T13:00:00Z",
                  "tz_time": "2019-01-31T13:00:00.000Z",
                  "geo_id": 8741,
                  "geo_name": "Península"
              }, {
                  "value": 56.08,
                  "datetime": "2019-01-31T15:00:00.000+01:00",
                  "datetime_utc": "2019-01-31T14:00:00Z",
                  "tz_time": "2019-01-31T14:00:00.000Z",
                  "geo_id": 8741,
                  "geo_name": "Península"
              }
          ]
      }
  }
  </code></pre>
  
  <p>I'm only getting the "value" and "datetime" data from here.</p>
  
  <p>The file I'm getting right now has this format (really bad one, I'm sorry):</p>
  
  <pre><code>{
  
      {
          "datetime": 01/02/2019 01:00:00,        "value": 48,68
      },
  
      {
          "datetime": 01/02/2019 02:00:00,        "value": 45,7
      },
  
      {
          "datetime": 01/02/2019 03:00:00,        "value": 44,1
      },
  
      {
          "datetime": 01/02/2019 04:00:00,        "value": 44,53
      },
  
      {
          "datetime": 01/02/2019 05:00:00,        "value": 46,87
      },
  
      {
          "datetime": 01/02/2019 06:00:00,        "value": 49,84
      },
  
      {
          "datetime": 01/02/2019 07:00:00,        "value": 56,69
      },
  
      {
          "datetime": 01/02/2019 08:00:00,        "value": 62,5
      },
  }
  </code></pre>
  
  <p>And what I'd like to get is:</p>
  
  <pre><code>{
      {
          "datetime": 01/02/2019 01:00:00,
          "value": 48,68
      },{
          "datetime": 01/02/2019 02:00:00,
          "value": 45,7
      },{
          "datetime": 01/02/2019 03:00:00,
          "value": 44,1
      },{
          "datetime": 01/02/2019 04:00:00,
          "value": 44,53
      },{
          "datetime": 01/02/2019 05:00:00,
          "value": 46,87
      },{
          "datetime": 01/02/2019 06:00:00,
          "value": 49,84
      },{
          "datetime": 01/02/2019 07:00:00,
          "value": 56,69
      },{
          "datetime": 01/02/2019 08:00:00,
          "value": 62,5
      }
  }
  </code></pre>
  
  <p>Thank you a lot and I'm sorry. I'm really new to JSON and formatting it on C#.</p>