Json: "type must be string, but is " std::string(j.type_name()

Created on 21 Aug 2018  路  21Comments  路  Source: nlohmann/json

  • What is the issue you have? JSON_THROW(type_error::create(302, "type must be string, but is " + std::string(j.type_name())));

  • Please describe the steps to reproduce the issue. Can you provide a small but working code example?

Create a JSON file with multiple objects and attempt to iterate with ` assert(baddie.is_array());

for (auto& json : jsonStream)
{
      name = json["Name"].get<std::string>();
   }`
  • What is the expected behavior?
    The file is properly read and parsed.

  • And what is the actual behavior instead?
    it gives the error above and an exception Unhandled exception at 0x00007FFF278EB098 in rpg.exe: Microsoft C++ exception: nlohmann::detail::type_error at memory location 0x000000268ECFF168. occurred

Visual Studio 2017

  • Did you use a released version of the library or the version from the develop branch?
    From release.

Also this is for an entirely different project the solution for the question I posted worked perfectly.

Most helpful comment

Ah, I see, the baddie is a json object that is defined in the class definition in another file.
Looks like your problem is that you're accessing "Attack" and "HP" without checking that they exist, and they don't exist for the second object. You either need to check that they exist, or use the value() function that takes a default value.

All 21 comments

Can you show your JSON file and all the code from read to the exception?

I assume that baddie is a data member, is it default initialized in the header file?

If not, then default constructed json values are null, which causes the assertion.

@kenshen112 There is no json library code in there.

@gregmarr The createEnemy and init function is the JSON libaray code

@theodelrieu By initalized do you mean set to NULL or the JSON library equivilent? IE. JSON J = NULL or nullptr.

Ah, I see, the baddie is a json object that is defined in the class definition in another file.
Looks like your problem is that you're accessing "Attack" and "HP" without checking that they exist, and they don't exist for the second object. You either need to check that they exist, or use the value() function that takes a default value.

One last question it's giving me a Syntax error where there should be none "syntax error - unexpected end of input; expected '[', '{', or a literal" right after the red Dragon in my JSON file I've tried editing it in various ways to make it read correctly.

I would start by removing all whitespace from the file. You might have an invisible control character somewhere.

Didn't work (Quick Edit) Turns out it is reading the entire file, I tried printing out the Name variable and all three appear so the error must be referring to the end of the file.

@kenshen112 I am confused. Did https://github.com/nlohmann/json/issues/1206#issuecomment-414739630 solve your issue or is there anything left open?

@nlohmann It did but it brought another issue that I have yet to solve that i asked shortly after that

You mean the parse error? Could you share the JSON text and the code with which you parse it?

What is the exact error message?

JSON_THROW(parse_error::create(101, m_lexer.get_position(), error_msg));

error_msg | "syntax error - unexpected end of input; expected '[', '{', or a literal" | std::basic_string<char,std::char_traits<char>,std::allocator<char> >

That said I can see the name var printing out in the console perfectly for all three and no matter the adjustments I make to the file the error never vanishes not that it'd matter the file itself is valid

The exception should contain a position of the error.

Unhandled exception at 0x00007FFD8F3EB098 in rpg.exe: Microsoft C++ exception: nlohmann::detail::parse_error at memory location 0x000000F1CB6FE968. occurred

Can you parse the file in like this:

std::ifstream f("yourfile.json);
json j = json::parse(f);

?

It gave me the same error but this time it didn't print the name data like it was before

Are you sure you opened the file correctly? And all parse errors contain the position like

[json.exception.parse_error.101] parse error at 8: syntax error - unexpected ']'; expected '[', '{', or a literal
exception id: 101

If you have a parse error at 0 or parse error at 1 then this means that even reading the first byte fails.

Got it figured out, had to change the design of the parsing function it was indeed the while loop that was messing it all up thanks for the help!

Was this page helpful?
0 / 5 - 0 ratings