Skip to content

How to programmatically fill an n-th dimensional JSON object? #1501

Description

@markg85

Hi,

The case is as follows.
In my application i'm receiving text lines. Each lines looks like:

/aaa/bbb/xxx
/aaa/bbb/yyy
/aaa/bbb/zzz

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):

{
  "aaa" : {
    "bbb" : {
      "xxx" : 1,
      "yyy" : 2,
      "zzz" : 3,
  }
}

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):

json jsonDoc;
json &temp = jsonDoc;
for(auto node : {"aaa", "bbb", "xxx"})
{
  temp[node] = {};
  temp = temp[node];
}

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:

{
  "aaa" : {
    "bbb" : {
      "xxx" : 1,
  }
}

But instead i get:

null

Any idea what i might be doing wrong here? And, how to fix it :)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions