1 # <small>nlohmann::basic_json::</small>value
5 template<class ValueType>
6 ValueType value(const typename object_t::key_type& key,
7 const ValueType& default_value) const;
10 template<class ValueType>
11 ValueType value(const json_pointer& ptr,
12 const ValueType& default_value) const;
15 1. Returns either a copy of an object's element at the specified key `key` or a given default value if no element with
18 The function is basically equivalent to executing
22 } catch(out_of_range) {
27 2. Returns either a copy of an object's element at the specified JSON pointer `ptr` or a given default value if no value
30 The function is basically equivalent to executing
34 } catch(out_of_range) {
41 - Unlike [`at`](at.md), this function does not throw if the given `key`/`ptr` was not found.
42 - Unlike [`operator[]`](operator[].md), this function does not implicitly add an element to the position defined by
43 `key`/`ptr` key. This function is furthermore also applicable to const objects.
45 ## Template parameters
48 : type compatible to JSON values, for instance `#!cpp int` for JSON integer numbers, `#!cpp bool` for JSON booleans,
49 or `#!cpp std::vector` types for JSON arrays. Note the type of the expected value at `key`/`ptr` and the default
50 value `default_value` must be compatible.
55 : key of the element to access
58 : the value to return if key/ptr found no value
61 : a JSON pointer to the element to access
65 1. copy of the element at key `key` or `default_value` if `key` is not found
66 1. copy of the element at JSON Pointer `ptr` or `default_value` if no value for `ptr` is found
70 Strong guarantee: if an exception is thrown, there are no
71 changes to any JSON value.
75 1. The function can throw the following exceptions:
76 - Throws [`type_error.302`](../../home/exceptions.md#jsonexceptiontype_error302) if `default_value` does not match
77 the type of the value at `key`
78 - Throws [`type_error.306`](../../home/exceptions.md#jsonexceptiontype_error306) if the JSON value is not an object;
79 in that case, using `value()` with a key makes no sense.
80 2. The function can throw the following exceptions:
81 - Throws [`type_error.302`](../../home/exceptions.md#jsonexceptiontype_error302) if `default_value` does not match
82 the type of the value at `ptr`
83 - Throws [`type_error.306`](../../home/exceptions.md#jsonexceptiontype_error306) if the JSON value is not an object;
84 in that case, using `value()` with a key makes no sense.
88 1. Logarithmic in the size of the container.
89 2. Logarithmic in the size of the container.
93 ??? example "Example (1): access specified object element with default value"
95 The example below shows how object elements can be queried with a default value.
98 --8<-- "examples/basic_json__value.cpp"
104 --8<-- "examples/basic_json__value.output"
107 ??? example "Example (2): access specified object element via JSON Pointer with default value"
109 The example below shows how object elements can be queried with a default value.
112 --8<-- "examples/basic_json__value_ptr.cpp"
118 --8<-- "examples/basic_json__value_ptr.output"
123 - see [`at`](at.md) for access by reference with range checking
124 - see [`operator[]`](operator%5B%5D.md) for unchecked access by reference
128 1. Added in version 1.0.0.
129 2. Added in version 2.0.2.