I have an array of JSON objects similar to below:
[
{"hello": "rapidjson",
"t": true,
"f": false,
"n": null,
"i": 2,
"pi": 3.1416},
{"hello": "rapidjson",
"t": true,
"f": false,
"n": null,
"i": 12,
"pi": 3.88},
{"hello": "rapidjson",
"t": true,
"f": false,
"n": null,
"i": 14,
"pi": 3.99}
]
My application spits out a bunch of JSON objects that I need to add to a JSON file every 30 seconds lets say.
Each round I need to append to the same file and add new JSON objects to the array of JSON objects that I have. The first entry of each JSON file is the JSON schema.
The problem I am facing is that I do not know how to each time read the previous file and add new objects to the array of existing objects in file and write back the updated file.
Could you please provide me guidance as what needs to be done? or point me to couple of examples (couldn't find similar example in tutorial)?
For the insertion part, it can be done in this way:
Document d, d2;
d.Parse(...);
Value v(d2, d.GetAllocator()); // Deep copy whole d2 to v, using d's allocator
d.PushBack(v, d.GetAllocator());
You may refer to the tutorial for these usages.
Doesnt this add another array into an array? I need more objects into an existing array
For instance:
Current json file:
[
{"hello": "rapidjson",
"t": true,
"f": false,
"n": null,
"i": 2,
"pi": 3.1416},
{"hello": "rapidjson",
"t": true,
"f": false,
"n": null,
"i": 12,
"pi": 3.88}
]
Adding:
{"hello": "rapidjson",
"t": true,
"f": false,
"n": null,
"i": 14,
"pi": 3.99}
New file should look exactly like:
[
{"hello": "rapidjson",
"t": true,
"f": false,
"n": null,
"i": 2,
"pi": 3.1416},
{"hello": "rapidjson",
"t": true,
"f": false,
"n": null,
"i": 12,
"pi": 3.88},
{"hello": "rapidjson",
"t": true,
"f": false,
"n": null,
"i": 14,
"pi": 3.99}
]
However, with what you suggested I get below (unacceptable) output:
[
{"hello": "rapidjson",
"t": true,
"f": false,
"n": null,
"i": 2,
"pi": 3.1416},
{"hello": "rapidjson",
"t": true,
"f": false,
"n": null,
"i": 12,
"pi": 3.88},
[{"hello": "rapidjson",
"t": true,
"f": false,
"n": null,
"i": 14,
"pi": 3.99}]
]
( Note the additional "[" and "]" added by adding the new object to the existing array of objects. Ideally only new object should be added to an existing array not new array of objects to an existing array of objects ). Am I missing something here?
It should not.
But if the value v in my example code is an array, and you want to extract the first element of it, then it just need to:
d.PushBack(v[0], d.GetAllocator());
Still the same behavior and adding extra "[" "]"
Note that D2 is an object. What I simply want is:
D1 (array of JSON objects), for instance: [{"one":1}, {"two":2}]
D2 is a JSON object, for instance: {"three":3}
I want to read D1 from file and add D2 to it and write it back to file. What needs to be written back is:
[{"one":1}, {"two":2}, {"three":3}] not --> [{"one":1}, {"two":2}, [{"three":3}]]
I found the solution (here is the list of complete steps):
using namespace rapidjson;
// Reading the JSON file
FILE* fp = fopen(json_file_name.c_str(), "r");
char readBuffer[65536];
FileReadStream is(fp, readBuffer, sizeof(readBuffer));
Document d, d2;
d.ParseStream(is);
assert(d.IsArray());
fclose(fp);
d2.SetObject();
Value json_objects(kObjectType);
json_objects.AddMember("three", 3, d2.GetAllocator());
d.PushBack(json_objects, d2.GetAllocator());
FILE* outfile = fopen(json_file_name.c_str(), "w");
char writeBuffer[65536];
FileWriteStream os(outfile, writeBuffer, sizeof(writeBuffer));
Writer
d.Accept (writer);
fclose(outfile);
It won't add extra []...
Most helpful comment
Still the same behavior and adding extra "[" "]"
Note that D2 is an object. What I simply want is:
D1 (array of JSON objects), for instance: [{"one":1}, {"two":2}]
D2 is a JSON object, for instance: {"three":3}
I want to read D1 from file and add D2 to it and write it back to file. What needs to be written back is:
[{"one":1}, {"two":2}, {"three":3}] not --> [{"one":1}, {"two":2}, [{"three":3}]]