QJsonValue

PyQt6.QtCore.QJsonValue

Description

The QJsonValue class encapsulates a value in JSON.

A value in JSON can be one of 6 basic types:

JSON is a format to store structured data. It has 6 basic data types:

A value can represent any of the above data types. In addition, QJsonValue has one special flag to represent undefined values. This can be queried with isUndefined().

The type of the value can be queried with type() or accessors like isBool(), isString(), and so on. Likewise, the value can be converted to the type stored in it using the toBool(), toString() and so on.

Values are strictly typed internally and contrary to QVariant will not attempt to do any implicit type conversions. This implies that converting to a type that is not stored in the value will return a default constructed return value.

QJsonValueRef

QJsonValueRef is a helper class for QJsonArray and QJsonObject. When you get an object of type QJsonValueRef, you can use it as if it were a reference to a QJsonValue. If you assign to it, the assignment will apply to the element in the QJsonArray or QJsonObject from which you got the reference.

The following methods return QJsonValueRef:

  • QJsonArray::operator[](qsizetype i)

  • QJsonObject::operator[](const QString & key) const

Enums

Type

This enum describes the type of the JSON value.

Member

Value

Description

Array

0x4

An array. Use toArray() to convert to a QJsonArray.

Bool

0x1

A boolean value. Use toBool() to convert to a bool.

Double

0x2

A number value. Use toDouble() to convert to a double, or toInteger() to convert to a qint64.

Null

0x0

A Null value

Object

0x5

An object. Use toObject() to convert to a QJsonObject.

String

0x3

A string. Use toString() to convert to a QString.

Undefined

0x80

The value is undefined. This is usually returned as an error condition, when trying to read an out of bounds value in an array or a non existent key in an object.

Methods

__init__(type: Type = Null)

Creates a QJsonValue of type type.

The default is to create a Null value.


__init__(Union[QJsonValue, Type, Iterable[QJsonValue], dict[Optional[str], QJsonValue], bool, int, float, None, Optional[str]])

Creates a copy of other.


__eq__(Union[QJsonValue, Type, Iterable[QJsonValue], dict[Optional[str], QJsonValue], bool, int, float, None, Optional[str]]) bool

TODO


@staticmethod
fromJson(Union[QByteArray, bytes, bytearray, memoryview]) (QJsonValue, QJsonParseError)

Parses json as a UTF-8 encoded JSON value, and creates a QJsonValue from it.

Returns a valid QJsonValue if the parsing succeeds. If it fails, the returned value will be isUndefined(), and the optional error variable will contain further details about the error.


@staticmethod
fromVariant(Any) QJsonValue

Converts variant to a QJsonValue and returns it.

The conversion will convert QVariant types as follows:

Source type

Destination type

  • Nullptr

Null

Bool

Double

String

Array

Object

String. The conversion will use toString() with flag FullyEncoded, so as to ensure maximum compatibility in parsing the URL

String. Since Qt 5.11, the resulting string will not include braces

Whichever type QCborValue::toJsonValue() returns.

Array. See QCborValue::toJsonValue() for conversion restrictions.

QJsonValue::Map. See QCborValue::toJsonValue() for conversion restrictions and the “stringification” of map keys.

Loss of information and other types

QVariant can carry more information than is representable in JSON. If the QVariant is not one of the types above, the conversion is not guaranteed and is subject to change in future versions of Qt, as the UUID one did. Code should strive not to use any other types than those listed above.

If isNull() returns true, a null QJsonValue is returned or inserted into the list or object, regardless of the type carried by QVariant. Note the behavior change in Qt 6.0 affecting isNull() also affects this function.

A floating point value that is either an infinity or NaN will be converted to a null JSON value. Since Qt 6.0, QJsonValue can store the full precision of any 64-bit signed integer without loss, but in previous versions values outside the range of ±2^53 may lose precision. Unsigned 64-bit values greater than or equal to 2^63 will either lose precision or alias to negative values, so ULongLong should be avoided.

For other types not listed above, a conversion to string will be attempted, usually but not always by calling QVariant::toString(). If the conversion fails the value is replaced by a null JSON value. Note that QVariant::toString() is also lossy for the majority of types. For example, if the passed QVariant is representing raw byte array data, it is recommended to pre-encode it to Base64 (or another lossless encoding), otherwise a lossy conversion using QString::fromUtf8() will be used.

Please note that the conversions via QVariant::toString() are subject to change at any time. Both QVariant and QJsonValue may be extended in the future to support more types, which will result in a change in how this function performs conversions.

See also

toVariant(), QCborValue::fromVariant().


__getitem__(int) QJsonValue

TODO


__getitem__(Optional[str]) QJsonValue

TODO


__hash__() int

TODO


isArray() bool

Returns true if the value contains an array.

See also

toArray().


isBool() bool

Returns true if the value contains a boolean.

See also

toBool().


isDouble() bool

Returns true if the value contains a double.

See also

toDouble().


isNull() bool

Returns true if the value is null.


isObject() bool

Returns true if the value contains an object.

See also

toObject().


isString() bool

Returns true if the value contains a string.

See also

toString().


isUndefined() bool

Returns true if the value is undefined. This can happen in certain error cases as e.g. accessing a non existing key in a QJsonObject.


__ne__(Union[QJsonValue, Type, Iterable[QJsonValue], dict[Optional[str], QJsonValue], bool, int, float, None, Optional[str]]) bool

TODO


swap(QJsonValue)

Swaps this value with other. This operation is very fast and never fails.


toArray() list[QJsonValue]

This is an overloaded function.

Converts the value to an array and returns it.

If type() is not Array, a QJsonArray() will be returned.


toArray(Iterable[Union[QJsonValue, Type, Iterable[QJsonValue], dict[Optional[str], QJsonValue], bool, int, float, None, Optional[str]]]) list[QJsonValue]

Converts the value to an array and returns it.

If type() is not Array, the defaultValue will be returned.


toBool(defaultValue: bool = False) bool

Converts the value to a bool and returns it.

If type() is not bool, the defaultValue will be returned.


toDouble(defaultValue: float = 0) float

Converts the value to a double and returns it.

If type() is not Double, the defaultValue will be returned.


toInt(defaultValue: int = 0) int

Converts the value to an int and returns it.

If type() is not Double or the value is not a whole number, the defaultValue will be returned.


toInteger(defaultValue: int = 0) int

Converts the value to an integer and returns it.

If type() is not Double or the value is not a whole number representable as qint64, the defaultValue will be returned.


toJson(format: JsonFormat = Indented) QByteArray

Converts the QJsonValue to a UTF-8 encoded JSON value in the provided format.

See also

fromJson(), JsonFormat.


toObject() dict[str, QJsonValue]

This is an overloaded function.

Converts the value to an object and returns it.

If type() is not Object, the QJsonObject() will be returned.


toObject(dict[Optional[str], Union[QJsonValue, Type, Iterable[QJsonValue], dict[Optional[str], QJsonValue], bool, int, float, None, Optional[str]]]) dict[str, QJsonValue]

Converts the value to an object and returns it.

If type() is not Object, the defaultValue will be returned.


toString() str

Converts the value to a QString and returns it.

If type() is not String, a null QString will be returned.

See also

QString::isNull().


toString(Optional[str]) str

Converts the value to a QString and returns it.

If type() is not String, the defaultValue will be returned.


toVariant() Any

Converts the value to a __init__().

The QJsonValue types will be converted as follows:

Constant

Description

Null

Nullptr

Bool

Bool

Double

Double or LongLong

String

QString

Array

QVariantList

Object

QVariantMap

Undefined

__init__()

See also

fromVariant().


type() Type

Returns the type of the value.

See also

Type.