eteran / cpp-json

A fast & modern C++17 JSON library

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to determine if an attribute is present?

mcordova1967 opened this issue · comments

Hello, I am using the array/iterator facilities to navigate each object of a certain array, but some fields/attributes may be missing in some of the objects, the library is throwing an error if I try to obtain a value of a non-existent field:

terminate called after throwing an instance of 'json::invalid_index'

I tried is_null but it does not work because I have to retrieve the value first and the exception gets triggered.

Any help will be greatly appreciated.
Thank you.

ps: Very nice library, I started a hello world right away, I am parsing mostly, the iterator is great, I just hit this aparent limitation in the API, I cannot find a contains() or has_attribute() function

If you are looking at a json object and are unsure if it has a specific key, you can use:

if(has_key(obj, "my_key")) {
    const json::value &v = obj["my_key"];
}

Let me know if this helps!

Closing as duplicate of #13