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

Adding JSON Array to the Array #1216

Closed
prasadnadendla opened this issue Aug 24, 2018 · 2 comments
Closed

Adding JSON Array to the Array #1216

prasadnadendla opened this issue Aug 24, 2018 · 2 comments
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@prasadnadendla
Copy link

prasadnadendla commented Aug 24, 2018

can we have a function , which allows us to add a json array of elements to the another instance of json array at the end.
I guess this feature is not available. please ignore if this is already exits and kindly let me know the function or method name. I have gone through the documentation and not found anything.

ex: json first = json::parse("[{\"name\":\"John\",\"gender\":"male"},{\"name\":\"Ben\",\"gender\":"female"}]");

json second = json::parse("[{\"name\":\"Poole\",\"gender\":"male"},{\"name\":\"Neil\",\"gender\":"female"}]");
// Appending second array of elements to the first json array object 
first.append(second);

@nlohmann
Copy link
Owner

The JSON library uses std::vector internally and uses nearly the same API. So taking the answer from https://stackoverflow.com/questions/2551775/appending-a-vector-to-a-vector:

#include <iostream>
#include "json.hpp"

using json = nlohmann::json;

int main(int argc, const char **argv) {
    json a = {1,2,3};
    json b = {4,5,6};
    
    a.insert(a.end(), b.begin(), b.end());
    
    std::cout << a << std::endl;
}

@nlohmann nlohmann added kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation labels Aug 24, 2018
@prasadnadendla
Copy link
Author

Wow..Thank you.

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

2 participants