Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unexpected end of input; expected '[', '{', or a literal #2066

Closed
Cascades opened this issue Apr 26, 2020 · 10 comments
Closed

Unexpected end of input; expected '[', '{', or a literal #2066

Cascades opened this issue Apr 26, 2020 · 10 comments
Labels
kind: bug solution: invalid the issue is not related to the library

Comments

@Cascades
Copy link

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.

@nlohmann
Copy link
Owner

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)?

@Cascades
Copy link
Author

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.

@nlohmann
Copy link
Owner

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?

@Cascades
Copy link
Author

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

@nlohmann
Copy link
Owner

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

@cenzowm
Copy link

cenzowm commented Mar 29, 2023

Hello!

Same problem. Can't understand why. I'm using the code of official doc.

#include <fstream>
#include <nlohmann/json.hpp>
using json = nlohmann::json;

// ...

std::ifstream f("example.json");
json data = json::parse(f);

@gregmarr
Copy link
Contributor

Most likely, example.json is not in your working directory. Either use a fully qualified path instead of just "example.json", or make absolutely certain that you know what your working directory is when you start the program and that example.json is in that directory. See #2066 (comment) for example.

Note that you can not both print the file contents and parse it without extra work in between, so just do one run to make sure that you're getting the file, and don't do any of the parsing, and then remove the extra code for verifying the output and do the parsing.

@cenzowm
Copy link

cenzowm commented Mar 30, 2023

In my case the "problem" was the executable location, which is in project_root/cmake-build-debug, so if I have my json files in a json folder the relative path will be ../json/file.json (you have to move up the folder).

Special thanks to @jack23247

@M-E-N
Copy link

M-E-N commented Jun 24, 2023

Hello to everyone
I have also this issue and i do not know how to handle it
my file is good when open with fstream but when i parse that to the json object
it says 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 is my part of code

    {   cout<<"check null if ";
        ifstream AllAccountsFILE("./AllAccountsFILE.json");
        if (AllAccountsFILE.is_open())//AllAccountsFILE.good())
        {   cout<<"check good if ";
            cout<<"buffer : " <<AllAccountsFILE.rdbuf();
            lg_AllAccounts = json::parse(AllAccountsFILE);
            //AllAccountsFILE.close();
        }
    }```

for my project i need your help 
thsnks for reading

@gregmarr
Copy link
Contributor

Note that you can not both print the file contents and parse it without extra work in between, so just do one run to make sure that you're getting the file, and don't do any of the parsing, and then remove the extra code for verifying the output and do the parsing.

This is your problem, you're reading all the data, leaving nothing for the JSON library to read.

cout<<"check good if ";
cout<<"buffer : " <<AllAccountsFILE.rdbuf();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: bug solution: invalid the issue is not related to the library
Projects
None yet
Development

No branches or pull requests

5 participants