akheron / jansson

C library for encoding, decoding and manipulating JSON data
http://www.digip.org/jansson/
Other
3.05k stars 808 forks source link

feature request: search for a key into a json document #631

Open brizio73 opened 1 year ago

brizio73 commented 1 year ago

Hi everybody! sorry if this is not the right place, I actually cannot find a better place... I'll try to make it quick. I have been using libjansson for quite a while now (I think we are close to 10 years or so... - great library by the way) but just today I found myself in the need of a feature that I think is missing from the library.

It would be great if one could perform a quick search on a json document to verify if a certain key is present. In my use case, an application requires an information that might be present in a json document that can be requested to a web API (just an http request to a specific address). The document is very large, and the position of that particular key is not exactly known in advance. We could collect a few samples of this document to parse it exactly and dig into the hierarchy until we find it, but the server providing this information is not under our control and may change in the future, sending a slightly different json. That would require us to change the code that parses the received json.

What I was asking is an API that might look like this: jsont_t* json_find(json_t *document, const char *key); that returns NULL if key is not found inside the document or a json_t object in case it founds the key. What happens if there are many instances of that key has to be defined... the API could return an array of json_t* or the function could work as strtok() (that requires multiple calls to advance in the search from the last positive result).

What do you think? Any chances to see this function implemented in future releases?

Thanks for reading! Bye

woodz- commented 1 year ago

This is OSS, so go for a pull request. An approach was already delivered by your own.