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

unable to parse json #1525

Closed
somequery opened this issue Mar 18, 2019 · 15 comments
Closed

unable to parse json #1525

somequery opened this issue Mar 18, 2019 · 15 comments

Comments

@somequery
Copy link

I am parsing the json file:
{
"default_txt": "sometext"
}

C++ code:
std::ifstream jsonFile("appTest.json", std::ios::in);
nlohmann::json parsed_json = nlohmann::json::parse(jsonFile);

I get an 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

I tried:
1/ full path name instead of just the file name
2/ I have checked the json formatting in online parsers with success
3/ parsed successfully with boost property tree

Do yo have any suggestions?

Thanks.

@gregmarr
Copy link
Contributor

Have you tried printing out the text after opening the stream? You appear to have some sort of garbage before the first character.

@somequery
Copy link
Author

It prints fine: std::cout << jsonFile.rdbuf() << std::endl;

@nlohmann
Copy link
Owner

Could you please share the file?

@somequery
Copy link
Author

I have attached the file (I changed the extension from json to txt).
Thanks.
appTest.txt

@nlohmann
Copy link
Owner

Hm. This all looks good:

#include "json.hpp"
#include <iostream>
#include <fstream>

using json = nlohmann::json;

int main() {
    std::ifstream f("/Users/niels/Downloads/appTest.txt");
    json j = json::parse(f);
    std::cout << j << std::endl;
}

produces

{"default":"sometext"}

@nlohmann
Copy link
Owner

@somequery Can you try the exact program of #1525 (comment) (well, with a different path...)?

@somequery
Copy link
Author

I used the full path as you mentioned, but the result is the same. I run it from VS and from a command line also, from the directory containing both the out and json file. I've received the same error always.

@somequery
Copy link
Author

After rebuilding it now works. But there is still problem (with the same error) on nlohmann::json parsed_json = nlohmann::json::parse(f);

Am I doing wrong something?

@nlohmann
Copy link
Owner

After rebuilding it now works.

Does it mean you can parse the file successfully now?

But there is still problem (with the same error) on nlohmann::json parsed_json = nlohmann::json::parse(f);

What is the problem?

@somequery
Copy link
Author

Does it mean you can parse the file successfully now?
Yes. The json file has to be specified with full path. In my case "./appTest.json"

What is the problem?
while learning the API I was using both:
std::ifstream jsonFile(./appTest.json);
nlohmann::json parsed_json = nlohmann::json::parse(jsonFile);

and

nlohmann::json jFields;
jsonFile >> jFields;

I figured out that I should be using the first or the second way but not both together. So everything works now.

Thanks.

@nlohmann
Copy link
Owner

Thanks for clarifying!

@terzioglan
Copy link

Hi,

I'm having parse_error.101 as well. I went through a bunch of reported issues about this but none of the suggested solutions seem to have solve my issue: I checked the encoding and made sure it's UTF8, I tried to use the full file path, I checked the bytes with Bless hex editor and everything seems to be in place.

Here is the exact program I'm using:


#include <iostream>
#include <fstream>
#include <nlohmann/json.hpp>

int main(int argc, char **argv)
{
  std::string line; 
  std::ifstream myJsonFile("/home/terzi/rosSpace/src/cirakdevel/cirakJson/example_1.json");

  if (myJsonFile.is_open())
  {
    while ( getline (myJsonFile,line) )
    {
      std::cout << line << '\n';
    }
    // myJsonFile.close();
  }
  else std::cout << "Unable to open file"; 

  try
  {
    // parsing input with a syntax error
    nlohmann::json::parse(myJsonFile);
  }
  catch (nlohmann::json::parse_error& e)
  {
    // output exception information
    std::cout << "message: " << e.what() << '\n'
              << "exception id: " << e.id << '\n'
              << "byte position of error: " << e.byte << std::endl;
  }
  return 0;
}

This is the .json file I'm using with its extension modified to .txt: example_1.txt

Here is the output of the program:

{
    "fruit": "Apple",
    "size": "Large",
    "color": "Red"
}
message: [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
exception id: 101
byte position of error: 1

I'm working on Ubuntu 16.04, compiling in a Catkin workspace.

@gregmarr
Copy link
Contributor

gregmarr commented May 8, 2019

You read the entire file and print it out, so there's nothing left to parse in the stream. What happens if you don't do that read and print first?

@terzioglan
Copy link

Thanks for the tip @gregmarr ! You are right and this actually solved the issue!.. Much kudos!

@gregmarr
Copy link
Contributor

gregmarr commented May 9, 2019

You're welcome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants