-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
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
How to programmatically fill an n-th dimensional JSON object? #1501
Comments
|
@garethsb-sony I don;t think i get it.. For the very reason for temp to not be the same object and basically walk deeper at every iteration. |
An assignment to a C++ reference doesn't change the object to which the reference refers. That line of code is exactly the same as |
Not related to your actual problem: If your text lines/index are really in the format: (code not tested). json json;
json["/aaa/bbb/xxx"_json_pointer] = 1;
// or
std::string s = "/aaa/bbb/xxx";
json[json_pointer(s)] = 1; |
@garethsb-sony Do you have an example for that? As the code looks quite different of pointers are used. @pboettch That works but gives me an output i'm not looking for:
Not the intention i'm looking for. |
#include <nlohmann/json.hpp>
#include <iomanip>
#include <iostream>
using nlohmann::json;
int main(void)
{
json j;
for (auto &p : {"/aaa/bbb/xxx", "/aaa/bbb/yyy", "/aaa/bbb/zzz"})
j[json::json_pointer(p)] = 1;
std::cout << std::setw(2) << j << "\n";
return 0;
} gives:
|
It really didn't ought to look very different, if you insist on keeping your current approach. Untested: json jsonDoc;
json *temp = &jsonDoc;
for(auto node : {"aaa", "bbb", "xxx"})
{
(*temp)[node] = {};
temp = &(*temp)[node];
} But if you have an input format using json-pointer, @pboettch has shown how easy that is to use. |
@pboettch Thank you very much! I did try it with @garethsb-sony ouch! I try to prevent writing code that looks like that :) I'm going for the much neater json_pointer way. Thank you both a lot for the help! Also, it might be worth pointing this out on the main json overview page. Even though it might be a bit of a specific corner case. // closed |
Care has to be taken when using your index as a json-pointer. How are |
That's no problem as i control the generated data. There is a character being used as seperator and that happens to be "/" for this application. |
Hi,
The case is as follows.
In my application i'm receiving text lines. Each lines looks like:
That would be the key structure. The values are also provided. Lets say xxx, yyy, zzz get values of 1, 2, and 3.
This should result in a JSON document that looks like (the parsing i do should make it look like that):
But i just can't seem to get this done. I must be missing something obvious here.
What i try to do is per input line (say /aaa/bbb/xxx) i split it on "/".
Then i say (somewhat like this):
As temp is a reference to jsonDoc i'm expecting it to modify the jsonDoc internally (aka, no copies). The output i would expect is:
But instead i get:
Any idea what i might be doing wrong here? And, how to fix it :)
The text was updated successfully, but these errors were encountered: