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

Exception when trying to insert my json object inside json file #4239

Open
2 tasks
marfixdev opened this issue Dec 11, 2023 · 4 comments
Open
2 tasks

Exception when trying to insert my json object inside json file #4239

marfixdev opened this issue Dec 11, 2023 · 4 comments
Labels
solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@marfixdev
Copy link

marfixdev commented Dec 11, 2023

Description

I'm trying to append my json object to inside a json file.

Reproduction steps

Bug occurs when trying to .insert()

Expected vs. actual results

This is how my json file looks like

{ "Base3": [ { "Name": "Base3", "X": 69, "Y": 999, "Z": 999 } ], "Base4": [ { "Name": "Base4", "X": 69, "Y": 999, "Z": 999 } ], "Base5": [ { "Name": "Base5", "X": 69, "Y": 999, "Z": 999 } ] }

What I expected ;

{ "Base3": [ { "Name": "Base3", "X": 69, "Y": 999, "Z": 999 } ], "Base4": [ { "Name": "Base4", "X": 69, "Y": 999, "Z": 999 } ], "Base5": [ { "Name": "Base5", "X": 69, "Y": 999, "Z": 999 } ], "Base6": [ { "Name": "Base6", "X": 22, "Y": 33, "Z": 11 } ] }

Minimal code example

using Json = nlohmann::json;
int main()
{

	Json Data;
	std::string Name = "Base6";
	float xx = 22;
	float yy = 33;
	float zz = 11;

	
	Data[Name]["Name"] = Name;
	Data[Name]["X"] = xx;
	Data[Name]["Y"] = yy;
	Data[Name]["Z"] = zz;

	std::fstream filewrite;
	filewrite.open("coords.json", std::ios::out | std::ios::in);
	
	Json existingjson = Json::parse(filewrite);
	filewrite.seekg(0, filewrite.beg);
	existingjson.insert(existingjson.begin(), Data);
	filewrite << existingjson;
	

}

Error messages

Unhandled exception at 0x00007FF8101F565C in JSON.exe: Microsoft C++ exception: nlohmann::json_abi_v3_11_2::detail::type_error at memory location 0x000000000014FC80.

Compiler and operating system

MSVC 14.38 / WINDOWS11

Library version

3.11.2

Validation

@nomadwolf0
Copy link

json::insert() only operates on arrays, not objects in the form you used. (Form (1) at the API documentation.). Only Form (5) of insert() is for inserting into objects.

You should be using json::update().

@nlohmann
Copy link
Owner

The type_error exception should provide more information in the what string. If you print it, it should probably indicate what @nomadwolf0 wrote.

@nlohmann nlohmann added solution: proposed fix a fix for the issue has been proposed and waits for confirmation and removed kind: bug labels Dec 11, 2023
@edvardsanta
Copy link

I tried to reproduce the error and it is showing a good exception actually.
image

@nlohmann
Copy link
Owner

@marfixdev Can I close this issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
solution: proposed fix a fix for the issue has been proposed and waits for confirmation
Projects
None yet
Development

No branches or pull requests

4 participants