Json: Unexpected end of input; expected '[', '{', or a literal

Created on 26 Apr 2020  Â·  5Comments  Â·  Source: nlohmann/json

I'm annoyed I couldn't find a way to fix this myself becaise I feel like I am missing something obvious.

Put simply I am getting this error:

terminate called after throwing an instance of 'nlohmann::detail::parse_error'
  what():  [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
Aborted (core dumped)

I am trying to parse an ifstream into a json object, and have tried many ways including:

std::string s = "built_in_langs/greek.json";
std::ifstream i(s);
json j = json::parse(i);

but also inlining the string, doing json j; i >> j;, and probably some other combinations of the same thing.

I'm on WSL using Ubuntu 18.04.3 LTS with g++7.4 (I want to get that high for various other reasons... but CMake isn't playing ball) and a host of Windows 10.

All variations of the above code snippet have worked perfectrly when I run on Windows with MSVC 19.25.28614.0... But Linux just doesn't??

I also tried an absolute path, didn't seem to help. I was able to read in the file fine on Linux with an ifstream, but json still threw this error.

I checked the file itself too. It most certainly does start with {, otherwise Windows would have cried too. The line endings are UNIX and there is no hidden BOM at the top. For the sake of completeness here is what it looks like:

{
    "keywords": {
        "and": "και", 
        "for $$0 in $$1": "για $$0 στο $$1", 
        "list": "λίστα", 
        "as": "ως", 
        "dict": "λεξ", 
        "def $$0": "καθ $$0", 
        "import $$0": "εισαγωγή $$0"
    }, 
    "customFunctions": {
        "print": "Τυπώνω", 
        "array": "πίνακας", 
        "random": "τυχαίος"
    }
}

Finally I pulled the project in to my work as a git submodule and am using PRIVATE nlohmann_json::nlohmann_json with all the recommended extras, like turning tests off.

Any help would be appreciated.

bug invalid

Most helpful comment

It's not an issue of the library - it assumes a correctly opened stream to read from.

All 5 comments

Can you double check that the file was successfully opened and that you could read its contents (e.g., by just printing it to cout)?

Thanks for the speedy reply. So:

std::string s = "built_in_langs/greek.json";
std::ifstream i(s);
std::cout << i.rdbuf();

Produces no output, while:

std::string s = "all/the/absolute/path/built_in_langs/greek.json";
std::ifstream i(s);
std::cout << i.rdbuf();

prints out perfectly. (And i must have not tested before for some reason, but with absolute path json::parse(i); works too).

As I say though, on windows it just works with rel paths, and for my application I think i'd really prefer them.

As I am writing this, I am also struggling with relative/absolute paths in a project ;-)

Good to know this works now.

Do you need further assistance?

If this is a known issue/not considered an issue then no, thanks a lot for the help once again.

It's not an issue of the library - it assumes a correctly opened stream to read from.

Was this page helpful?
0 / 5 - 0 ratings