Removing elements from a JSON object #347
-
Hi, I'm creating a JSON object to hold a list of subjects each of which has an associated list of fields - the object ends up looking like shown below
I create the elements using the [] notation
I want to be able to delete an object with a given subject - e.g. the entry for "/SYSTEM/USER". There doesn't seem to be an equivalent notation to completely delete it. I tried
That sets the contents to 0 but leaves the element for the subject in. I tried using json_pointer::remove() but it expects '/' as a separator, but my subjects already have '/'s - i.e. I can't use "objects//SYSTEM/USER". Is there any way of achieving this without turning it into an array and using the method you described in #289 ? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
To remove with a JSON Pointer, all occurrences of json val = json::parse(data);
jsonpointer::remove(val, "/objects/~1SYSTEM~1USER"); Alternatively, you could use the
But the simplest way would be to use
|
Beta Was this translation helpful? Give feedback.
-
Thank you. That worked perfectly - I used the erase. |
Beta Was this translation helpful? Give feedback.
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 useAlternatively, you could use the
json_pointer
class to do the escaping for you,But the simplest way would be to use