Skip to content

Parsing Multiple JSON Files #1240

Closed
Closed
@schoen2016

Description

Hi,

Thank you for reading.

I am attempting to parse multiple json files and receiving an error :

[json.exception.parse_error.101] parse error at 15: syntax error - unexpected '}'; expected end of input.
Aborted

I use the following function to parse the json file
`

json JSON_Reader(std::string filename){
	char ch, json_ch[1000000];
	std::ifstream f(filename, fstream::in);
	int i = 0;
	while (f >> noskipws >> ch){
		json_ch[i] = ch;
		i++;
	}
	f.close();

	//Parse JSON file
	return json::parse(json_ch);
}

`

MAIN
`

using json = nlohmann::json;

json JSON_Reader(std::string);

int  main(int argc, char* argv[]) {

	if (argc < 3) {
		std::cerr << "Usage: " << argv[0] << " [JSON FILE]" << std::endl;
		return 1;
	}
	printf("arg %s\n", argv[1]);
	printf("arg %s\n", argv[2]);
	printf("arg %s\n", argv[3]);

	json setup;
	json setup2;
	json setup3;

	setup = JSON_Reader(argv[1]);
	cout << setup["num"] << "\n\n";
	setup2 = JSON_Reader(argv[2]);
	cout << setup2["num"] << "\n\n";
	setup3 = JSON_Reader(argv[3]);
	cout << setup3["num"] << "\n\n";
}

`

JSON file
{ "num": 2 }

All three arguments call a different json file with the same content shown above. The expected behavior is that '2' is printed in the terminal three times.

After some trial and error I found that the last one saved gives me the error. The three setup files are called setup1.json, setup2.json and setup3.json. If I save setup1.json or setup2.json last then I get the error mentioned above. If I save setup3.json last, there is no error.

However, lets say I do save setup3.json last :
When I use setup3.json for all three arguments I get no error.
When I exclude setup3.json, replacing it with one of the other json files, I get no error.

I am using gcc version 5.3.0 which I believe is said to be supported, please correct me if I am mistaken.
JSON for Modern C++ version 3.2.0.

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions