Skip to content

Removing elements from a JSON object #347

Answered by danielaparker
andysCaplin asked this question in Q&A
Discussion options

You must be logged in to vote

To remove with a JSON Pointer, all occurrences of ~ and / in the member name must be escaped with ~0 and ~1 respectively, so you'd need to use

json val = json::parse(data);
jsonpointer::remove(val, "/objects/~1SYSTEM~1USER");

Alternatively, you could use the json_pointer class to do the escaping for you,

jsonpointer::json_pointer ptr;
ptr /= "objects";
ptr /= "/SYSTEM/USER";
jsonpointer::remove(val, ptr);

But the simplest way would be to use

val["objects"].erase("/SYSTEM/USER");

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by andysCaplin
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants