class QJsonDocument#

The QJsonDocument class provides a way to read and write JSON documents. More

Synopsis#

Methods#

Static functions#

Note

This documentation may contain snippets that were automatically translated from C++ to Python. We always welcome contributions to the snippet translation. If you see an issue with the translation, you can also let us know by creating a ticket on https:/bugreports.qt.io/projects/PYSIDE

Detailed Description#

QJsonDocument is a class that wraps a complete JSON document and can read this document from, and write it to, a UTF-8 encoded text-based representation.

A JSON document can be converted from its text-based representation to a QJsonDocument using fromJson() . toJson() converts it back to text. The parser is very fast and efficient and converts the JSON to the binary representation used by Qt.

Validity of the parsed document can be queried with ! isNull()

A document can be queried as to whether it contains an array or an object using isArray() and isObject() . The array or object contained in the document can be retrieved using array() or object() and then read or manipulated.

class JsonFormat#

Warning

This section contains snippets that were automatically translated from C++ to Python and may contain errors.

This value defines the format of the JSON byte array produced when converting to a QJsonDocument using toJson() .

Constant

Description

QJsonDocument.Indented

Defines human readable output as follows:

"Array": [
    True,
    999,
    "string"
],
"Key": "Value",
"null": null

QJsonDocument.Compact

Defines a compact output as follows:

{"Array":[True,999,"string"],"Key":"Value","null":null}
__init__(array)#
Parameters:

arrayQJsonArray

Constructs a QJsonDocument from array.

__init__(other)
Parameters:

otherQJsonDocument

Creates a copy of the other document.

__init__(object)
Parameters:

objectQJsonObject

Creates a QJsonDocument from object.

__init__()

Constructs an empty and invalid document.

array()#
Return type:

QJsonArray

Returns the QJsonArray contained in the document.

Returns an empty array if the document contains an object.

static fromJson(json[, error=None])#
Parameters:
Return type:

QJsonDocument

Parses json as a UTF-8 encoded JSON document, and creates a QJsonDocument from it.

Returns a valid (non-null) QJsonDocument if the parsing succeeds. If it fails, the returned document will be null, and the optional error variable will contain further details about the error.

static fromVariant(variant)#
Parameters:

variant – object

Return type:

QJsonDocument

Creates a QJsonDocument from the QVariant variant.

If the variant contains any other type than a QVariantMap , QVariantHash , QVariantList or QStringList , the returned document is invalid.

See also

toVariant()

isArray()#
Return type:

bool

Returns true if the document contains an array.

See also

array() isObject()

isEmpty()#
Return type:

bool

Returns true if the document doesn’t contain any data.

isNull()#
Return type:

bool

returns true if this document is null.

Null documents are documents created through the default constructor.

Documents created from UTF-8 encoded text or the binary format are validated during parsing. If validation fails, the returned document will also be null.

isObject()#
Return type:

bool

Returns true if the document contains an object.

See also

object() isArray()

object()#
Return type:

QJsonObject

Returns the QJsonObject contained in the document.

Returns an empty object if the document contains an array.

__ne__(other)#
Parameters:

otherQJsonDocument

Return type:

bool

returns true if other is not equal to this document

__eq__(other)#
Parameters:

otherQJsonDocument

Return type:

bool

Returns true if the other document is equal to this document.

operator(key)#
Parameters:

keyQLatin1String

Return type:

QJsonValue

operator(key)
Parameters:

key – str

Return type:

QJsonValue

This is an overloaded function.

operator(key)
Parameters:

key – str

Return type:

QJsonValue

Returns a QJsonValue representing the value for the key key.

Equivalent to calling object() .value(key).

The returned QJsonValue is Undefined if the key does not exist, or if isObject() is false.

See also

QJsonValue isUndefined() QJsonObject

operator(i)
Parameters:

i – int

Return type:

QJsonValue

Returns a QJsonValue representing the value for index i.

Equivalent to calling array() .at(i).

The returned QJsonValue is Undefined , if i is out of bounds, or if isArray() is false.

setArray(array)#
Parameters:

arrayQJsonArray

Sets array as the main object of this document.

See also

setObject() array()

setObject(object)#
Parameters:

objectQJsonObject

Sets object as the main object of this document.

See also

setArray() object()

swap(other)#
Parameters:

otherQJsonDocument

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

toJson([format=QJsonDocument.JsonFormat.Indented])#
Parameters:

formatJsonFormat

Return type:

QByteArray

Converts the QJsonDocument to a UTF-8 encoded JSON document in the provided format.

toVariant()#
Return type:

object

Returns a QVariant representing the Json document.

The returned variant will be a QVariantList if the document is a QJsonArray and a QVariantMap if the document is a QJsonObject .