-2

I'm trying to parse JSON in C++ with nlohmann/json library. But this library can't parse not formatted JSON.

I get an error:

libc++abi.dylib: terminating with uncaught exception of type nlohmann::detail::parse_error: [json.exception.parse_error.101] parse error at line 1, column 1: syntax error while parsing value - unexpected end of input; expected '[', '{', or a literal

This JSON gives me an error:

{"1":{"row":{"last":"1.1857","ma":"Strong Sell","ma_class":"redFont","clock":"<span class=\"greenClockIcon\">&nbsp;<\/span>"},"arrowBoxClass":"upArrow","summaryLast":"1.1857","summaryName":"EUR\/USD","summaryNameAlt":"EUR\/USD - Euro US Dollar","summaryChange":"+0.0056","summaryChangePercent":"+0.48","summaryChangeClass":"greenFont","technicalSummary":"Strong Sell","technicalSummaryClass":"sell","maBuy":2,"maSell":10,"tiBuy":0,"tiSell":7,"updateTime":null,"link":"\/currencies\/eur-usd"},"2":{"row":{"last":"1.3109","ma":"Strong Sell","ma_class":"redFont","clock":"<span class=\"greenClockIcon\">&nbsp;<\/span>"},"arrowBoxClass":"upArrow","summaryLast":"1.3109","summaryName":"GBP\/USD","summaryNameAlt":"GBP\/USD - British Pound US Dollar","summaryChange":"+0.0049","summaryChangePercent":"+0.37","summaryChangeClass":"greenFont","technicalSummary":"Strong Sell","technicalSummaryClass":"sell","maBuy":0,"maSell":12,"tiBuy":0,"tiSell":7,"updateTime":null,"link":"\/currencies\/gbp-usd"},"3":{"row":{"last":"105.64","ma":"Strong Buy","ma_class":"greenFont","clock":"<span class=\"greenClockIcon\">&nbsp;<\/span>"},"arrowBoxClass":"downArrow","summaryLast":"105.64","summaryName":"USD\/JPY","summaryNameAlt":"USD\/JPY - US Dollar Japanese Yen","summaryChange":"-0.07","summaryChangePercent":"-0.06","summaryChangeClass":"redFont","technicalSummary":"Strong Buy","technicalSummaryClass":"buy","maBuy":12,"maSell":0,"tiBuy":9,"tiSell":0,"updateTime":null,"link":"\/currencies\/usd-jpy"},"5":{"row":{"last":"0.7189","ma":"Strong Sell","ma_class":"redFont","clock":"<span class=\"greenClockIcon\">&nbsp;<\/span>"},"arrowBoxClass":"upArrow","summaryLast":"0.7189","summaryName":"AUD\/USD","summaryNameAlt":"AUD\/USD - Australian Dollar US Dollar","summaryChange":"+0.0029","summaryChangePercent":"+0.40","summaryChangeClass":"greenFont","technicalSummary":"Strong Sell","technicalSummaryClass":"sell","maBuy":0,"maSell":12,"tiBuy":0,"tiSell":8,"updateTime":null,"link":"\/currencies\/aud-usd"},"7":{"row":{"last":"1.3278","ma":"Buy","ma_class":"greenFont","clock":"<span class=\"greenClockIcon\">&nbsp;<\/span>"},"arrowBoxClass":"downArrow","summaryLast":"1.3278","summaryName":"USD\/CAD","summaryNameAlt":"USD\/CAD - US Dollar Canadian Dollar","summaryChange":"-0.0039","summaryChangePercent":"-0.29","summaryChangeClass":"redFont","technicalSummary":"Buy","technicalSummaryClass":"buy","maBuy":11,"maSell":1,"tiBuy":3,"tiSell":3,"updateTime":null,"link":"\/currencies\/usd-cad"},"9":{"row":{"last":"125.25","ma":"Strong Sell","ma_class":"redFont","clock":"<span class=\"greenClockIcon\">&nbsp;<\/span>"},"arrowBoxClass":"upArrow","summaryLast":"125.25","summaryName":"EUR\/JPY","summaryNameAlt":"EUR\/JPY - Euro Japanese Yen","summaryChange":"+0.51","summaryChangePercent":"+0.40","summaryChangeClass":"greenFont","technicalSummary":"Strong Sell","technicalSummaryClass":"sell","maBuy":2,"maSell":10,"tiBuy":0,"tiSell":9,"updateTime":null,"link":"\/currencies\/eur-jpy"},"10":{"row":{"last":"1.0777","ma":"Strong Buy","ma_class":"greenFont","clock":"<span class=\"greenClockIcon\">&nbsp;<\/span>"},"arrowBoxClass":"upArrow","summaryLast":"1.0777","summaryName":"EUR\/CHF","summaryNameAlt":"EUR\/CHF - Euro Swiss Franc","summaryChange":"+0.0003","summaryChangePercent":"+0.03","summaryChangeClass":"greenFont","technicalSummary":"Strong Buy","technicalSummaryClass":"buy","maBuy":12,"maSell":0,"tiBuy":5,"tiSell":2,"updateTime":null,"link":"\/currencies\/eur-chf"}}

But nlohmann works fine with JSON like this:

{
  "1": {
    "row": {
      "last": "1.1857",
      "ma": "Strong Sell",
      "ma_class": "redFont",
      "clock": "<span class=\"greenClockIcon\">&nbsp;<\\/span>"
    },
    "arrowBoxClass": "upArrow",
    "summaryLast": "1.1857",
    "summaryName": "EUR\\/USD",
    "summaryNameAlt": "EUR\\/USD - Euro US Dollar",
    "summaryChange": "+0.0056",
    "summaryChangePercent": "+0.48",
    "summaryChangeClass": "greenFont",
    "technicalSummary": "Strong Sell",
    "technicalSummaryClass": "sell",
    "maBuy": 2,
    "maSell": 10,
    "tiBuy": 0,
    "tiSell": 7,
    "updateTime": null,
    "link": "\\/currencies\\/eur-usd"
  },
  "2": {
    "row": {
      "last": "1.3109",
      "ma": "Strong Sell",
      "ma_class": "redFont",
      "clock": "<span class=\"greenClockIcon\">&nbsp;<\\/span>"
    },
    "arrowBoxClass": "upArrow",
    "summaryLast": "1.3109",
    "summaryName": "GBP\\/USD",
    "summaryNameAlt": "GBP\\/USD - British Pound US Dollar",
    "summaryChange": "+0.0049",
    "summaryChangePercent": "+0.37",
    "summaryChangeClass": "greenFont",
    "technicalSummary": "Strong Sell",
    "technicalSummaryClass": "sell",
    "maBuy": 0,
    "maSell": 12,
    "tiBuy": 0,
    "tiSell": 7,
    "updateTime": null,
    "link": "\\/currencies\\/gbp-usd"
  },
  "3": {
    "row": {
      "last": "105.64",
      "ma": "Strong Buy",
      "ma_class": "greenFont",
      "clock": "<span class=\"greenClockIcon\">&nbsp;<\\/span>"
    },
    "arrowBoxClass": "downArrow",
    "summaryLast": "105.64",
    "summaryName": "USD\\/JPY",
    "summaryNameAlt": "USD\\/JPY - US Dollar Japanese Yen",
    "summaryChange": "-0.07",
    "summaryChangePercent": "-0.06",
    "summaryChangeClass": "redFont",
    "technicalSummary": "Strong Buy",
    "technicalSummaryClass": "buy",
    "maBuy": 12,
    "maSell": 0,
    "tiBuy": 9,
    "tiSell": 0,
    "updateTime": null,
    "link": "\\/currencies\\/usd-jpy"
  },
  "5": {
    "row": {
      "last": "0.7189",
      "ma": "Strong Sell",
      "ma_class": "redFont",
      "clock": "<span class=\"greenClockIcon\">&nbsp;<\\/span>"
    },
    "arrowBoxClass": "upArrow",
    "summaryLast": "0.7189",
    "summaryName": "AUD\\/USD",
    "summaryNameAlt": "AUD\\/USD - Australian Dollar US Dollar",
    "summaryChange": "+0.0029",
    "summaryChangePercent": "+0.40",
    "summaryChangeClass": "greenFont",
    "technicalSummary": "Strong Sell",
    "technicalSummaryClass": "sell",
    "maBuy": 0,
    "maSell": 12,
    "tiBuy": 0,
    "tiSell": 8,
    "updateTime": null,
    "link": "\\/currencies\\/aud-usd"
  },
  "7": {
    "row": {
      "last": "1.3278",
      "ma": "Buy",
      "ma_class": "greenFont",
      "clock": "<span class=\"greenClockIcon\">&nbsp;<\\/span>"
    },
    "arrowBoxClass": "downArrow",
    "summaryLast": "1.3278",
    "summaryName": "USD\\/CAD",
    "summaryNameAlt": "USD\\/CAD - US Dollar Canadian Dollar",
    "summaryChange": "-0.0039",
    "summaryChangePercent": "-0.29",
    "summaryChangeClass": "redFont",
    "technicalSummary": "Buy",
    "technicalSummaryClass": "buy",
    "maBuy": 11,
    "maSell": 1,
    "tiBuy": 3,
    "tiSell": 3,
    "updateTime": null,
    "link": "\\/currencies\\/usd-cad"
  },
  "9": {
    "row": {
      "last": "125.25",
      "ma": "Strong Sell",
      "ma_class": "redFont",
      "clock": "<span class=\"greenClockIcon\">&nbsp;<\\/span>"
    },
    "arrowBoxClass": "upArrow",
    "summaryLast": "125.25",
    "summaryName": "EUR\\/JPY",
    "summaryNameAlt": "EUR\\/JPY - Euro Japanese Yen",
    "summaryChange": "+0.51",
    "summaryChangePercent": "+0.40",
    "summaryChangeClass": "greenFont",
    "technicalSummary": "Strong Sell",
    "technicalSummaryClass": "sell",
    "maBuy": 2,
    "maSell": 10,
    "tiBuy": 0,
    "tiSell": 9,
    "updateTime": null,
    "link": "\\/currencies\\/eur-jpy"
  },
  "10": {
    "row": {
      "last": "1.0777",
      "ma": "Strong Buy",
      "ma_class": "greenFont",
      "clock": "<span class=\"greenClockIcon\">&nbsp;<\\/span>"
    },
    "arrowBoxClass": "upArrow",
    "summaryLast": "1.0777",
    "summaryName": "EUR\\/CHF",
    "summaryNameAlt": "EUR\\/CHF - Euro Swiss Franc",
    "summaryChange": "+0.0003",
    "summaryChangePercent": "+0.03",
    "summaryChangeClass": "greenFont",
    "technicalSummary": "Strong Buy",
    "technicalSummaryClass": "buy",
    "maBuy": 12,
    "maSell": 0,
    "tiBuy": 5,
    "tiSell": 2,
    "updateTime": null,
    "link": "\\/currencies\\/eur-chf"
  }
}

Is there a way to format JSON from first format to the second one in C++ code? Without using websites. Maybe there's a library for this, or it's achievable with just C++ or STL.

Natoly
  • 7
  • 3
  • *"But this library can't parse not formatted JSON."* - What makes you think this? – 0x5453 Aug 05 '20 at 20:25
  • @0x5453 I added error to question. Double info here, the error is: libc++abi.dylib: terminating with uncaught exception of type nlohmann::detail::parse_error: [json.exception.parse_error.101] parse error at line 1, column 1: syntax error while parsing value - unexpected end of input; expected '[', '{', or a literal – Natoly Aug 05 '20 at 20:26
  • @Natoly Obviously your json is just badly formed, this has nothing to do with formatting. – john Aug 05 '20 at 20:28
  • `nlohmann::json` is capable of parsing single line JSON files. The issue is in the input. Basically, it's saying that it's trying to parse an empty string. – ChrisMM Aug 05 '20 at 20:28
  • @ChrisMM I'll double check this now. – Natoly Aug 05 '20 at 20:30
  • Please provide the code showing your parsing attempt - you might have forgotten to append `_json` to your string or something – Odysseus Aug 05 '20 at 20:31
  • @John Do you mean that first JSON is not valid? I checked for this - everything is fine. – Natoly Aug 05 '20 at 20:31
  • @Natoly It's just not the case that nlohmann can't parse unformated JSON, so the problem is almost certainly in your code or in your data. – john Aug 05 '20 at 20:34
  • please show a [mre] at a guess you are reading from a file that has a utf-8 BOM marker – Alan Birtles Aug 05 '20 at 20:41
  • Here is a [working example on Repl.it](https://repl.it/@geary/StackOverflow-63272990) of nlohmann/json parsing your one-line JSON string and fetching a value from it. Click the Run button at the top to see it in action. Clearly, as others have said, there is something else going on with your code or JSON data, and worrying about formatting it into a "pretty printed" JSON string is not what you need to solve the problem. – Michael Geary Aug 05 '20 at 20:56
  • @john You were right, thanks a lot! The problem is that I try to parse the file which I just downloaded. Maybe it's not downloaded for real and I try to parse an empty file. I figured that out when tried to parse file which I downloaded some time ago, and parsing worked. – Natoly Aug 05 '20 at 21:03
  • @MichaelGeary Yes, you're right, thanks a lot, this problem isn't because of nlohmann code, it's because I try to parse file which I've just downloaded, and maybe I'm trying to parse an empty file. – Natoly Aug 05 '20 at 21:05
  • @Odysseus thanks for caring, the problem was not in parsing code, it's because maybe I try to parse file which is not downloaded (I download and try to parse file in the same program). – Natoly Aug 05 '20 at 21:08

1 Answers1

1

The problem isn't connected with nlohmann code. It works fine.

The problem is that I download that JSON and try to parse it in one program. The problem was that I create file with this code:

FILE* file = fopen(path, "w");

And I don't close it after. The problem was I tried to parse not closed file.

I added:

fclose(file);

Before trying to parse. And it all worked!

I tried the same code to parse file which I downloaded some time ago, and it worked as it should.

Natoly
  • 7
  • 3
  • 1
    If the data is small (like yours) then the data is not flushed to the file until you flush or close the file. So the file is actually empty until you do one of those two things even though you have written data to the file. – Jerry Jeremiah Aug 05 '20 at 21:53