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

size_type size() const;

Description

Returns the number of elements of a JSON value, i.e. std::distance(begin(), end()).

Return value

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

Value type return value
null 0
boolean 1
string 1
number 1
object ObjectType::size()
array ArrayType::size()

Parameter

None.

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 values {"string", true, 17, 23.42};
 
    std::cout << "values contains " << values.size() << " elements.\n";
}

Output:

values contains 4 elements.

See also

  • empty
    checks whether the container is empty
  • max_size
    returns the maximum possible number of elements
⚠️ **GitHub.com Fallback** ⚠️