nlohmann::basic_json::empty - renxiaobo27/json GitHub Wiki

bool empty() const;

Description

Checks if a JSON value has no elements, i.e. whether begin() == end().

Parameters

None.

Return value

The return value depends on the different value types and is defined as follows:

Value type return value
null true
boolean false
string false
number false
object ObjectType::empty()
array ArrayType::empty()

Complexity

Constant, as long as ArrayType and ObjectType satisfy the Container concept.

Exceptions

None. The function's noexcept-specification is noexcept.

Example

The following code uses empty to check if a nlohmann::json contains any elements:

#include <iostream>
#include <json.hpp>
 
int main()
{
    nlohmann::json numbers;
    std::cout << "Initially, numbers.empty(): " << numbers.empty() << '\n';
 
    numbers.push_back(42);
    numbers.push_back(13317); 
    std::cout << "After adding elements, numbers.empty(): " << numbers.empty() << '\n';
}

Output:

Initially, numbers.empty(): 1
After adding elements, numbers.empty(): 0
⚠️ **GitHub.com Fallback** ⚠️