0

I have a C++ program where I jump around between many different files and classes collecting data that I'd like to output as one clean JSON file.

I'm using nlohmann's JSON files.

I can successfully write to a JSON file by doing the following:

json dm;
to_json(dm);
std::ofstream o("data_model.json");
o << setw(1) << dm << "\n";

void World::to_json(json& dm) {
    dm = {
           {"People", {
             {"Rick", "c137"},
             {"Jerry", "N/A"}
         }}};
}

data_model.json then looks like:

{
 "People": {
  "Jerry": "N/A",
  "Rick": "c137"
 }
}

This is what a want, so far so good!

However, I seem to be unable to then pass "dm" to another function and append to the JSON file; it seems I can only write to the JSON file once.

To get around this, I'm trying to instead write all my desired data to a regular txt file, then somehow copy the data over from the text file into the JSON file in one go.

This is a sample of how I've tried to do that:

const char* fname = "text.txt";

// Writing to text file to later read from:
std::ofstream txt_for_jsn;
txt_for_jsn.open(fname);
txt_for_jsn <<
            "{\n" <<
            "{\"People\": {\n" <<
            "\"Rick\": \"c137\"\n" <<
            "\"Jerry\": \"N/A\"\n" <<
            "}}};";
txt_for_jsn.close();

// Appending all lines from txt file to one string:
string txt_to_str;
std::ifstream read_file(fname);
string line;
while(std::getline(read_file, line)){
  txt_to_str.append(line);
}
// Assigning the string that contains all of the txt file to the JSON file
dm = txt_to_str;

However, this doesn't give the same desired output as before. Instead, I get this:

"{{\"People\": {\"Rick\": \"c137\"\"Jerry\": \"N/A\"}}};"

Is there a better way to get my widely spread data consolidated nicely into a JSON file?

tadman
  • 208,517
  • 23
  • 234
  • 262
Otherness
  • 385
  • 2
  • 16
  • You're writing invalid JSON to the file with your `txt_for_json` output, so are you really surprised it's invalid when you read it back in? – tadman Feb 27 '20 at 23:37
  • 1
    It's unclear what you mean by " I seem to be unable to then pass "dm" to another function" so can you explain what problem you had there, specifically? – tadman Feb 27 '20 at 23:38
  • 1
    You can open the file in append mode: `auto file = std::ofstream("bla", std::ios:app);`. However, you'll end up with multiple JSON objects in the file, which is most likely not what you want. If you want one single "big" JSON object, then create that first and only output it to the file once. – AVH Feb 27 '20 at 23:44
  • @tadman, can you explain what you mean in your first comment? I'm not attempting to write JSON into txt_for_jsn, I'm writing a .txt file then trying to later convert it to JSON. Regarding your second comment, it's simply that I can't append to the JSON file. What I want to do in pass it to another function and append to "dm", but all I can do is pass to a function and overwrite the JSON file. – Otherness Feb 28 '20 at 21:55
  • The JSON you're writing out manually is trashed, the syntax is wrong. You're getting back *exactly* what you're writing, syntax errors and all. I don't know what use that junk data would be, it's not JSON, and it can't be parsed by a JSON parser. You need to clean up that syntax if you want to import it later. – tadman Feb 28 '20 at 21:57
  • @tadman, Well one of us is definitely confused. Like I said in my previous comment, I'm not trying to write JSON to txt_for_jsn. Is that what you're talking about? Please let me know what part of the code you're talking about, as I don't know what you're referring to. – Otherness Feb 28 '20 at 22:00
  • @Darhuuk, I'm sorry, but I'm new to JSON and I don't fully understand what you mean. I can successfully create "file" how you've laid out, but how is that in append mode? How do I add data to that file in JSON and how would I append? – Otherness Feb 28 '20 at 22:23
  • The `txt_for_jsn << ...` part. – tadman Feb 28 '20 at 22:39
  • @tadman, Okay. So I intentionally wrote that as a txt file because I don't know of any syntax which would import to JSON. Do you know of such syntax? – Otherness Feb 28 '20 at 22:43
  • It'd help if what you wrote out was [valid JSON](https://jsonlint.com). – tadman Feb 28 '20 at 22:46
  • @tadman, Not sure if you're trolling me, but the question is how to do that, as I understand that what I want to write is valid JSON. The question is how do I write and append valid JSON. Since I was struggling to append JSON via multiple inputs, I tried writing txt syntax to see if I could then convert that over, which has also been a struggle. Does that make sense? – Otherness Feb 28 '20 at 22:59
  • I'm just confused by your intent there, and even more confused by what you're asking regarding that problem. Can you clarify *precisely* what you're trying to do here, objective-wise, and what specific problems you had in the course of solving that? You still haven't answered about what precludes you from passing `dm` to another function. – tadman Feb 28 '20 at 23:04
  • If you want to parse, the documentation is pretty clear that [`json::parse`](https://nlohmann.github.io/json/classnlohmann_1_1basic__json_a265a473e939184aa42655c9ccdf34e58.html#a265a473e939184aa42655c9ccdf34e58) is the answer, and works on any valid JSON string. – tadman Feb 28 '20 at 23:05
  • I *can* pass it to another function, I just can't seem to append: "What I want to do in pass it to another function and append to "dm", but all I can do is pass to a function and overwrite the JSON file." My goal is to gather a bunch of data that's scattered throughout my c++ program and consolidate it into one JSON file. I guess I just thought this was simpler than it actually is, so I'll have to dive deeper into the documentation. I appreciate the time you've taken here to reply though, thank you! – Otherness Feb 28 '20 at 23:14

0 Answers0