HTTP GET - can't display specific value

Hi there,

I’m trying to display a specific value from a http get-request. The request is from a value scraped from the website cnbc.com (http://quote.cnbc.com/quote-html-webservice/restQuote/symbolType/symbol?symbols=US10Y&requestMethod=itv&noform=1&partnerId=2&fund=1&exthrs=1&output=json&events=1)

  • which gives me this information:
{
  "FormattedQuoteResult" : {
    "FormattedQuote" : [ {
      "symbol" : "US10Y",
      "symbolType" : "symbol",
      "code" : 0,
      "name" : "U.S. 10 Year Treasury",
      "shortName" : "US 10-YR",
      "onAirName" : "10-Yr T-Note",
      "altName" : "US TREASURY-CURRENT 10 YEAR",
      "last" : "1.596%",
      "last_timedate" : "8:15 AM EDT",
      "last_time" : "2021-04-20T08:15:04.000-0400",
      "changetype" : "DOWN",
      "type" : "BOND",
      "subType" : "Government Bond",
      "exchange" : "U.S.",
      "source" : "Exchange",
      "open" : "1.598%",
      "high" : "1.633%",
      "low" : "1.587%",
      "change" : "-0.003",
      "change_pct" : "0.00%",
      "provider" : "CNBC Quote Cache",
      "previous_day_closing" : "1.599%",
      "altSymbol" : "US10YT=XX",
      "realTime" : "true",
      "curmktstatus" : "REG_MKT",
      "yrhiprice" : "1.63",
      "yrhidate" : "04/20/21",
      "yrloprice" : "0.00",
      "streamable" : "1",
      "bond_last_price" : "95.7344",
      "bond_change_price" : "+0.0312",
      "bond_change_pct_price" : "+0.0352%",
      "bond_open_price" : "95.7188",
      "bond_high_price" : "95.8125",
      "bond_low_price" : "95.4062",
      "bond_prev_day_closing_price" : "95.7031",
      "bond_changetype" : "UP",
      "maturity_date" : "2031-02-15",
      "coupon" : "1.12%",
      "issue_id" : "5093160",
      "countryCode" : "US",
      "timeZone" : "EDT",
      "EventData" : {
        "is_halted" : "N"
      }
    } ]
  }
}

I would like to dissect only the “last”-value from the above information to be shown on my IDE serial monitor output, for later displayed on an small o-led screen.

My IDE code is currently this:

http.begin(“http://quote.cnbc.com/quote-html-webservice/restQuote/symbolType/symbol?symbols=US10Y&requestMethod=itv&noform=1&partnerId=2&fund=1&exthrs=1&output=json&events=1”);

int httpCode = http.GET(); 

if (httpCode > 0)  // check the returning code
{
  String payload = http.getString();   //Get the request response payload

  DynamicJsonBuffer jsonBuffer(512);

  // Parse JSON object
  JsonObject& root = jsonBuffer.parseObject(payload);
  if (!root.success()) {

    Serial.println(F("Parsing failed!"));
    return;
  }

  float USlast = (float)root["FormattedQuoteResult"]["FormattedQuote"]["last"]; 

  Serial.print(USlast);

This gives me the value “0.00”.

Please help, how can I get the “last”-value to be shown on the serial output monitor alone?
Thanks in advance.

FormattedQuote appears to be an array, albeit with one element. You need to provide an array index I think.

indeed,

last = doc["FormattedQuoteResult"]["FormattedQuote"][0]["last"];

A filter could be helpful to extract only what you need

try this out:

create a new sketch and in the .ino put this code

#include <ArduinoJson.h>
#include "data.h"

StaticJsonDocument<200> filter;
StaticJsonDocument<2000> doc;

void setup() {
  const char* last;



  Serial.begin(115200);

  // direct deserialize the whole thing. Make sure you have enough memory
  deserializeJson(doc, (const __FlashStringHelper*) sampleJSON);

  // this is what we extracted, it's huge
  serializeJsonPretty(doc, Serial);

  // get the entry
  last = doc["FormattedQuoteResult"]["FormattedQuote"][0]["last"];

  // print it
  Serial.print(F("\nThe value you are looking for is --> Last : "));
  Serial.println(last);

  Serial.println(F("\n******** now with a filter ***********"));
  filter["FormattedQuoteResult"]["FormattedQuote"][0]["last"] = true;
  deserializeJson(doc, (const __FlashStringHelper*) sampleJSON, DeserializationOption::Filter(filter));

  // this is what we extracted, it's much smaller
  serializeJsonPretty(doc, Serial);

  last = doc["FormattedQuoteResult"]["FormattedQuote"][0]["last"];
  Serial.print(F("\nThe value you are looking for is --> Last : "));
  Serial.println(last);
}

void loop() {}

then create a new tab in your sketch and call it data.h and paste this in data.h

// C++ raw string literals cf http://en.cppreference.com/w/cpp/language/string_literal
// USE PROGMEM with Program Space Utilities http://www.nongnu.org/avr-libc/user-manual/group__avr__pgmspace.html

const char sampleJSON[] PROGMEM = R"--8<--8<--({
  "FormattedQuoteResult" : {
    "FormattedQuote" : [ {
      "symbol" : "US10Y",
      "symbolType" : "symbol",
      "code" : 0,
      "name" : "U.S. 10 Year Treasury",
      "shortName" : "US 10-YR",
      "onAirName" : "10-Yr T-Note",
      "altName" : "US TREASURY-CURRENT 10 YEAR",
      "last" : "1.596%",
      "last_timedate" : "8:15 AM EDT",
      "last_time" : "2021-04-20T08:15:04.000-0400",
      "changetype" : "DOWN",
      "type" : "BOND",
      "subType" : "Government Bond",
      "exchange" : "U.S.",
      "source" : "Exchange",
      "open" : "1.598%",
      "high" : "1.633%",
      "low" : "1.587%",
      "change" : "-0.003",
      "change_pct" : "0.00%",
      "provider" : "CNBC Quote Cache",
      "previous_day_closing" : "1.599%",
      "altSymbol" : "US10YT=XX",
      "realTime" : "true",
      "curmktstatus" : "REG_MKT",
      "yrhiprice" : "1.63",
      "yrhidate" : "04/20/21",
      "yrloprice" : "0.00",
      "streamable" : "1",
      "bond_last_price" : "95.7344",
      "bond_change_price" : "+0.0312",
      "bond_change_pct_price" : "+0.0352%",
      "bond_open_price" : "95.7188",
      "bond_high_price" : "95.8125",
      "bond_low_price" : "95.4062",
      "bond_prev_day_closing_price" : "95.7031",
      "bond_changetype" : "UP",
      "maturity_date" : "2031-02-15",
      "coupon" : "1.12%",
      "issue_id" : "5093160",
      "countryCode" : "US",
      "timeZone" : "EDT",
      "EventData" : {
        "is_halted" : "N"
      }
    } ]
  }
})--8<--8<--";

now compile and upload to your Arduino

Serial Monitor (@ 115200 bauds) will show

{
  "FormattedQuoteResult": {
    "FormattedQuote": [
      {
        "symbol": "US10Y",
        "symbolType": "symbol",
        "code": 0,
        "name": "U.S. 10 Year Treasury",
        "shortName": "US 10-YR",
        "onAirName": "10-Yr T-Note",
        "altName": "US TREASURY-CURRENT 10 YEAR",
        "last": "1.596%",
        "last_timedate": "8:15 AM EDT",
        "last_time": "2021-04-20T08:15:04.000-0400",
        "changetype": "DOWN",
        "type": "BOND",
        "subType": "Government Bond",
        "exchange": "U.S.",
        "source": "Exchange",
        "open": "1.598%",
        "high": "1.633%",
        "low": "1.587%",
        "change": "-0.003",
        "change_pct": "0.00%",
        "provider": "CNBC Quote Cache",
        "previous_day_closing": "1.599%",
        "altSymbol": "US10YT=XX",
        "realTime": "true",
        "curmktstatus": "REG_MKT",
        "yrhiprice": "1.63",
        "yrhidate": "04/20/21",
        "yrloprice": "0.00",
        "streamable": "1",
        "bond_last_price": "95.7344",
        "bond_change_price": "+0.0312",
        "bond_change_pct_price": "+0.0352%",
        "bond_open_price": "95.7188",
        "bond_high_price": "95.8125",
        "bond_low_price": "95.4062",
        "bond_prev_day_closing_price": "95.7031",
        "bond_changetype": "UP",
        "maturity_date": "2031-02-15",
        "coupon": "1.12%",
        "issue_id": "5093160",
        "countryCode": "US",
        "timeZone": "EDT",
        "EventData": {
          "is_halted": "N"
        }
      }
    ]
  }
}
The value you are looking for is --> Last : 1.596%

******** now with a filter ***********
{
  "FormattedQuoteResult": {
    "FormattedQuote": [
      {
        "last": "1.596%"
      }
    ]
  }
}
The value you are looking for is --> Last : 1.596%

But if this is the only field of interest, rather than getting the full JSON back in your arduino and parse later, you could use json-streaming-parser which invokes a callback for each token so it lets you get in memory only what you need

That is AMAZING! Thanks for the fast reply and apologize for my late one :slight_smile:

This morning I got the “last” data shown on my serial monitor as well as on a little display, as intended. Unfortunately I did it by replacing:
float last = (float)root[“FormattedQuoteResult”][“FormattedQuote”][“last”];
with
float last = (float)root[“FormattedQuoteResult”][“FormattedQuote”][0][“last”];

Thank you very much!! :slight_smile:

I really like the idea of filtering and extract only, what is needed, though.
I created a new sketch file and created the data.h with the data, as you prescribed.
I got the sketch to work and got data in the serial monitor as expected.

However the data is not static and does not update as the “last” value changes during the day, due to the data.h is not updated with new data (from the cnbc.com-webside).

How do I update the data.h, so the correct last-value occurs?

I did not check the json-streaming-parser yet, but will in the upcoming days.

Thanks again for the help so far.
Best regards Mass

Every morning in the mirror *checks the weather AND the 10-Year U.S. Treasury Yield :+1: :joy:

Sketch working with:
float last = (float)root[“FormattedQuoteResult”][“FormattedQuote”][0][“last”];
instead of
float last = (float)root[“FormattedQuoteResult”][“FormattedQuote”][“last”];

Thanks!!

this was just an example, the sampleJSON holds a cstring in PROGMEM in my case but if you get the JSON dynamically from an HTTP request into a cstring , then you just pass the cString to the same function.


I understand you can’t go out without knowing the 10-Year U.S. Treasury Yield, but really the outside temperature, who cares :innocent:

joke aside, cool integration into the mirror ! (and still cold in Denmark! (?))

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.