QJsonDocument#

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

Synopsis#

Functions#

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 PySide6.QtCore.QJsonDocument#

PySide6.QtCore.QJsonDocument(array)

PySide6.QtCore.QJsonDocument(other)

PySide6.QtCore.QJsonDocument(object)

Parameters:

Constructs an empty and invalid document.

Constructs a QJsonDocument from array.

Creates a copy of the other document.

Creates a QJsonDocument from object.

PySide6.QtCore.QJsonDocument.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}
PySide6.QtCore.QJsonDocument.array()#
Return type:

PySide6.QtCore.QJsonArray

Returns the QJsonArray contained in the document.

Returns an empty array if the document contains an object.

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

PySide6.QtCore.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 PySide6.QtCore.QJsonDocument.fromVariant(variant)#
Parameters:

variant – object

Return type:

PySide6.QtCore.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()

PySide6.QtCore.QJsonDocument.isArray()#
Return type:

bool

Returns true if the document contains an array.

See also

array() isObject()

PySide6.QtCore.QJsonDocument.isEmpty()#
Return type:

bool

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

PySide6.QtCore.QJsonDocument.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.

PySide6.QtCore.QJsonDocument.isObject()#
Return type:

bool

Returns true if the document contains an object.

See also

object() isArray()

PySide6.QtCore.QJsonDocument.object()#
Return type:

QJsonObject

Returns the QJsonObject contained in the document.

Returns an empty object if the document contains an array.

PySide6.QtCore.QJsonDocument.__ne__(other)#
Parameters:

otherPySide6.QtCore.QJsonDocument

Return type:

bool

returns true if other is not equal to this document

PySide6.QtCore.QJsonDocument.__eq__(other)#
Parameters:

otherPySide6.QtCore.QJsonDocument

Return type:

bool

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

PySide6.QtCore.QJsonDocument.operator(key)#
Parameters:

key – str

Return type:

PySide6.QtCore.QJsonValue

This is an overloaded function.

PySide6.QtCore.QJsonDocument.operator(key)
Parameters:

key – str

Return type:

PySide6.QtCore.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

PySide6.QtCore.QJsonDocument.operator(i)
Parameters:

i – int

Return type:

PySide6.QtCore.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.

PySide6.QtCore.QJsonDocument.setArray(array)#
Parameters:

arrayPySide6.QtCore.QJsonArray

Sets array as the main object of this document.

See also

setObject() array()

PySide6.QtCore.QJsonDocument.setObject(object)#
Parameters:

objectQJsonObject

Sets object as the main object of this document.

See also

setArray() object()

PySide6.QtCore.QJsonDocument.swap(other)#
Parameters:

otherPySide6.QtCore.QJsonDocument

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

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

formatJsonFormat

Return type:

PySide6.QtCore.QByteArray

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

See also

fromJson() JsonFormat

PySide6.QtCore.QJsonDocument.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 .