Skip to content
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

Combine json objects into one? #1165

Closed
Merethiel opened this issue Jul 17, 2018 · 15 comments
Closed

Combine json objects into one? #1165

Merethiel opened this issue Jul 17, 2018 · 15 comments
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@Merethiel
Copy link

Merethiel commented Jul 17, 2018

The title might not correctly describe what I'm attempting but essentially I have one structure that I can convert to a json string but I want to append a list of objects also to be converted into json strings and send that all together as one packet.

Something like this.
`json Packet;

Packet["PacketType"] = config::packet::type::TYPE;

Packet["ArrayOfObjects"] = ArrayOfObjects;`

Or make two separate json objects and combine them.

Another question I have is, does an array of objects have to be set at initialisation?

@pboettch
Copy link
Contributor

pboettch commented Jul 17, 2018

What have you tried? Could you show us some more elaborate code-examples of what you do and how you do it?

To push an object into a list you can do:

json a = json::array();
a.push_back({{"key", "value"}}); // two braces!

// or
json d;
d["key"] = value;
a.push_back(d);

@Merethiel
Copy link
Author

Merethiel commented Jul 17, 2018

Hi thank you for the response!
This is what I tried as an example:

    Packet["Id"] = 10;
    typedef struct Movement_{
        int XPosition;
        int YPosition;
    }Movement;
    std::set<Movement> v;

    for(int x = 0; x < 10; ++x)
    {
        Movement Mov = {0};
        Mov.XPosition = x;
        Mov.YPosition = x+1;
        v.insert(Mov);
    }
    Packet["MovementPackets"] = v;```



Essentially I wanted one "Id" while the rest was a set of objects following it.
It seems like the code tags are being buggy on here.

@pboettch
Copy link
Contributor

pboettch commented Jul 17, 2018

Before proposing something, could you write (by hand if needed) the json you would expect? Where do you see the array, where the object(s)?

@Merethiel
Copy link
Author

It was more of a question than a proposal if it already existed, but the JSON results I would have been expecting from the code above, to look something like this:

    /*
     {
     "Id" : 10,
     "Xposition[0]": 0,
     "YPosition[0]": 1,
     "XPosition[1]" : 1,
     "YPosition[1]" : 2,
     "XPosition[2]" : 2,
     "YPosition[2]" : 3,
     */

And so on until XPosition[9] and YPosition[9]

But I'm thinking a way I can do this wiht the existing code is simply appending the string value of [0],[1] as I loop through?

@pboettch
Copy link
Contributor

pboettch commented Jul 17, 2018

I meant before I can propose something.

However, are you sure your JSON is efficient? Why not:

{"Id": 10,
 "Position": [ 
     { "x": 0, "y": 1 },
     { "x": 1, "y": 2 }
 ]
}

@Merethiel
Copy link
Author

Merethiel commented Jul 17, 2018

I think the way you wrote it is far more efficient and better looking than I how I wrote it!

@pboettch
Copy link
Contributor

pboettch commented Jul 17, 2018

Hmm, what do you mean with unique keys? Here is how you would access this with nlohmann-json:

d["Id"] == 10

d["Position"][0]["x"] == 0

d["Position"][1]["y"] == 2

@Merethiel
Copy link
Author

Merethiel commented Jul 17, 2018

Would that need to be implemented or is there a workaround I can use for it? The other concern I have is, I'm sending this over through a Websocket to a javascript client and I'm not certain if the JSON implementation in Javascript would be compatible if the feature was implemented here.

@pboettch
Copy link
Contributor

This is standard-compliant JSON:

{"Id": 10,
 "Position": [ 
     { "x": 0, "y": 1 },
     { "x": 1, "y": 2 }
 ]
}

@Merethiel
Copy link
Author

Ah okay. Thank you so much! I'll see if I can find a work around until then.

@pboettch
Copy link
Contributor

Workaround for what?

@Merethiel
Copy link
Author

Merethiel commented Jul 17, 2018

What you posted:

{"Id": 10,
 "Position": [ 
     { "x": 0, "y": 1 },
     { "x": 1, "y": 2 }
 ]
}

Oh I think I just realised what you did. You pushed back the previous json into the original. I get it now. I'm sorry for the confusion!

@pboettch
Copy link
Contributor

Of course.

#include <nlohmann/json.hpp>

#include <iostream>

int main(void)
{
	nlohmann::json pos;

	pos["Id"] = 10;
	pos["Position"] = nlohmann::json::array();

	for (int i = 0; i < 10; i++)
		pos["Position"].push_back({{"x", i}, {"y", i + 1}});

	std::cout << std::setw(2) << pos << "\n";
	return 0;
}

Prints

{
  "Id": 10,
  "Position": [
    {
      "x": 0,
      "y": 1
    },
    {
      "x": 1,
      "y": 2
    },
    {
      "x": 2,
      "y": 3
    },
    {
      "x": 3,
      "y": 4
    },
    {
      "x": 4,
      "y": 5
    },
    {
      "x": 5,
      "y": 6
    },
    {
      "x": 6,
      "y": 7
    },
    {
      "x": 7,
      "y": 8
    },
    {
      "x": 8,
      "y": 9
    },
    {
      "x": 9,
      "y": 10
    }
  ]
}

@Merethiel
Copy link
Author

Thank you! That's exactly what I was trying to do!

@pboettch
Copy link
Contributor

You're welcome. Don't forget to close this issue if you have no further problems regarding your initial questions.

@nlohmann nlohmann added kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation labels Jul 17, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation
Projects
None yet
Development

No branches or pull requests

3 participants