0

I am trying to parse a JSON object with poco parser. I am running into a Poco::JSON::JSONException at xxx memory location. I don't understand, what am I supposed to do to get past this?

Here is the code snippet:

 //Run API Get request, this returns an input stream.  This is done 
 with poco code and works well.  I know because I can print out the 
 input stream and it is the JSON object.  I will start from here:

 std::istream& page = session.receiveResponse(response); //page is the 
 input stream response from the api get request

 Poco::JSON::Parser parser;
 Poco::Dynamic::Var result = parser.parse(page);  // This is the line 
 that the debugger stops on and the error is thrown.

Error output

4 lines of error are outputed. I included a screenshot, which is in the link below. .

Can anyone help me out here?

This is the returned JSON object:

{ "Meta Data": { "1. Information": "Intraday (5min) open, high, low, close prices and volume", "2. Symbol": "gib", "3. Last Refreshed": "2023-03-15 16:05:00", "4. Interval": "5min", "5. Output Size": "Full size", "6. Time Zone": "US/Eastern" }, "Time Series (5min)": { "2023-03-15 16:05:00": { "1. open": "89.3400", "2. high": "89.3400", "3. low": "89.3400", "4. close": "89.3400", "5. volume": "100" }, "2023-03-15 16:00:00": { "1. open": "89.0500", "2. high": "89.3600", "3. low": "89.0500", "4. close": "89.3100", "5. volume": "19864" },...}

rawmud
  • 70
  • 9
  • If you print `page`, do you see any header information? JSON parsing is extremely picky. – Tim Roberts Mar 16 '23 at 18:26
  • I posted the header in the answer below. I also checked the memory locations specified and found nothing of interest. I thought maybe the json has like a null value at the memory location or something funny. The memory locations don't look like they are holding anything of interest, maybe that is the problem? I'm not sure where to go from here. I am attempting to find a way to filter out istream information sent to the parser. – rawmud Mar 17 '23 at 07:47
  • I added a try/catch block, with catch (Poco::Exception& exc) { std::cerr << exc.displayText() << std::endl; } as the catch. I get 'JSON Exception: unexpected end of text' as the error output. Something wrong with the end of text? – rawmud Mar 17 '23 at 07:54
  • I did some more research and found that the end of text error occurs when the JSON is formatted incorrectly and I should pass the JSON through a linter. I found a JSON online validator and passed the html text through the validator and it checked out fine - no problems with the JSON. Poco parser and the validator don't agree... is there a way to hone in on the exact location of the problem the poco parser is saying? – rawmud Mar 17 '23 at 08:16
  • The text you quote in your "answer" is valid JSON. There is no header that would foil a JSON parser. Here's one possibility. If you are saving or printing a copy of the JSON, that would leave the istream at end-of-file, so there's nothing for the parser to read. You would need to rewind it by doing a `seekg` back to position 0. – Tim Roberts Mar 17 '23 at 17:37
  • @Tim Roberts, I played around with seekg and couldn't get it to work. I do print the page json to the console with the following code preceding the parsing: Poco::StreamCopier::copyStream(page, std::cout); //prints entire JSON object . I then tried to parse the JSON and ran into the error. I added a seekg and couldn't get it to work. The last seekg thing I tried was: page.seekg(0, page.end); int length = page.tellg(); page.seekg(0, page.beg); std::cout << length << std::endl; I added this preceding the parsing. The length prints out -1, so something funny is going on. – rawmud Mar 18 '23 at 20:09
  • Maybe their `istream` only supports one pass. Try reading the `istream` into a std::string and pass the string to JSON. – Tim Roberts Mar 19 '23 at 06:32

1 Answers1

0

Changing from copystream to copy to string and parsing the string worked:

  Poco::StreamCopier::copyToString(page, jsonObject);
  Poco::JSON::Parser parser;
  Poco::Dynamic::Var result = parser.parse(jsonObject);
    Poco::JSON::Object::Ptr pObject = 
  result.extract<Poco::JSON::Object::Ptr>();
    std::string price1 = pObject->getValue<std::string>("Time Series 
  (5min)");
    Poco::Dynamic::Var result2 = parser.parse(price1);
    Poco::JSON::Object::Ptr pObject2 = 
  result2.extract<Poco::JSON::Object::Ptr>();
    std::string price2 = pObject2->getValue<std::string>("2023-03-17 
  16:00:00");
    std::cout << price2 << std::endl;

Thanks @Tim Roberts

rawmud
  • 70
  • 9