DavidKanes
DavidKanes

Reputation: 313

How to extract one single json object as a new json from a bigger json object in C

I want to extract one json key-value pair as a new json object. Sorry I'm pretty new to json, so maybe I'm not stating the question as clear as it could be. So here is an example:

json_t main_object = {"AA":11,"BB":22,"CC":33};

So now I want to extract the "BB":22 key-value pair as a new object. Looks like this:

json_t new_obj = {"BB":22};

Is there any function can directly do that, or you need to declare a new empty json and than fill it with the correponding key and value?

I can only use pre-written functions from this library :https://jansson.readthedocs.io/en/2.13/apiref.html#object

Please let me know what is the best practice how can I do that and please point out if there is any mistake in the description or examples. I wasn't able to find much examples on this library

Thank you

Upvotes: -1

Views: 235

Answers (1)

dreais
dreais

Reputation: 75

honestly not sure this even compiles. {key:obj} is not a C standard way of writing structures. as for your question, you already answered yourself:

json_t *json_object_get(const json_t *object, const char *key)

https://jansson.readthedocs.io/en/2.13/apiref.html#c.json_object_get

if you wanted to do something yourself, parsing strings is also a feasible way (and not that hard if you already have all the details)

Upvotes: 0

Related Questions