QJsonDocument Class | Qt Core | Qt 6.10.3
Description: The QJsonDocument class provides a way to read and write JSON documents.

- Qt 6.10
- Qt Core
- C++ Classes
- QJsonDocument
- Qt 6.10.3 Reference Documentation

# QJsonDocument Class

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

Header: | `#include <QJsonDocument>`
CMake: | `find_package(Qt6 REQUIRED COMPONENTS Core)` `target_link_libraries(mytarget PRIVATE Qt6::Core)`
qmake: | `QT += core`

- List of all members, including inherited members
- QJsonDocument is part of JSON Support in Qt and Implicitly Shared Classes.

This class is equality-comparable.

Note: All functions in this class are reentrant.

## Public Types

enum | JsonFormat { Indented, Compact }

## Public Functions

| QJsonDocument()
| QJsonDocument(const QJsonArray &array)
| QJsonDocument(const QJsonObject &object)
| QJsonDocument(const QJsonDocument &other)
| QJsonDocument(QJsonDocument &&other)
| ~QJsonDocument()
QJsonArray | array() const
bool | isArray() const
bool | isEmpty() const
bool | isNull() const
bool | isObject() const
QJsonObject | object() const
void | setArray(const QJsonArray &array)
void | setObject(const QJsonObject &object)
void | swap(QJsonDocument &other)
QByteArray | toJson(QJsonDocument::JsonFormat format = JsonFormat::Indented) const
QVariant | toVariant() const
QJsonDocument & | operator=(QJsonDocument &&other)
QJsonDocument & | operator=(const QJsonDocument &other)
const QJsonValue | operator[](const QString &key) const
const QJsonValue | operator[](qsizetype i) const
const QJsonValue | operator[](QLatin1StringView key) const
const QJsonValue | operator[](QStringView key) const

## Static Public Members

QJsonDocument | fromJson(const QByteArray &json, QJsonParseError *error = nullptr)
QJsonDocument | fromVariant(const QVariant &variant)

## Related Non-Members

bool | operator!=(const QJsonDocument &lhs, const QJsonDocument &rhs)
bool | operator==(const QJsonDocument &lhs, const QJsonDocument &rhs)

## 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 QJsonDocument::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.

See also JSON Support in Qt and Saving and Loading a Game.

## Member Type Documentation

### enum QJsonDocument::JsonFormat

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

Constant | Value | Description
--- | --- | ---
`QJsonDocument::Indented` | `0` | Defines human readable output as follows:
         {
             "Array": [
                 true,
                 999,
                 "string"
             ],
             "Key": "Value",
             "null": null
         }
`QJsonDocument::Compact` | `1` | Defines a compact output as follows:
         {"Array":[true,999,"string"],"Key":"Value","null":null}

## Member Function Documentation

### QJsonDocument::QJsonDocument()

Constructs an empty and invalid document.

### `[explicit]` QJsonDocument::QJsonDocument(const QJsonArray &array)

Constructs a QJsonDocument from array.

### `[explicit]` QJsonDocument::QJsonDocument(const QJsonObject &object)

Creates a QJsonDocument from object.

### QJsonDocument::QJsonDocument(const QJsonDocument &other)

Creates a copy of the other document.

### `[noexcept]` QJsonDocument::QJsonDocument(QJsonDocument &&other)

Move-constructs a QJsonDocument from other.

### `[noexcept]` QJsonDocument::~QJsonDocument()

Deletes the document.

Binary data set with fromRawData is not freed.

### QJsonArray QJsonDocument::array() const

Returns the QJsonArray contained in the document.

Returns an empty array if the document contains an object.

See also isArray(), object(), and setArray().

### `[static]` QJsonDocument QJsonDocument::fromJson(const QByteArray &json, QJsonParseError *error = nullptr)

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.

See also toJson(), QJsonParseError, and isNull().

### `[static]` QJsonDocument QJsonDocument::fromVariant(const QVariant &variant)

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

### bool QJsonDocument::isArray() const

Returns `true` if the document contains an array.

See also array() and isObject().

### bool QJsonDocument::isEmpty() const

Returns `true` if the document doesn't contain any data.

### bool QJsonDocument::isNull() const

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.

### bool QJsonDocument::isObject() const

Returns `true` if the document contains an object.

See also object() and isArray().

### QJsonObject QJsonDocument::object() const

Returns the QJsonObject contained in the document.

Returns an empty object if the document contains an array.

See also isObject(), array(), and setObject().

### void QJsonDocument::setArray(const QJsonArray &array)

Sets array as the main object of this document.

See also setObject() and array().

### void QJsonDocument::setObject(const QJsonObject &object)

Sets object as the main object of this document.

See also setArray() and object().

### `[noexcept]` void QJsonDocument::swap(QJsonDocument &other)

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

### QByteArray QJsonDocument::toJson(QJsonDocument::JsonFormat format = JsonFormat::Indented) const

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

See also fromJson() and JsonFormat.

### QVariant QJsonDocument::toVariant() const

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.

See also fromVariant() and QJsonValue::toVariant().

### `[noexcept]` QJsonDocument &QJsonDocument::operator=(QJsonDocument &&other)

Move-assigns other to this document.

### QJsonDocument &QJsonDocument::operator=(const QJsonDocument &other)

Assigns the other document to this QJsonDocument. Returns a reference to this object.

### const QJsonValue QJsonDocument::operator[](const QString &key) const

Returns a QJsonValue representing the value for the key key.

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

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

See also QJsonValue, QJsonValue::isUndefined(), and QJsonObject.

### const QJsonValue QJsonDocument::operator[](qsizetype i) const

Returns a QJsonValue representing the value for index i.

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

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

See also QJsonValue, QJsonValue::isUndefined(), and QJsonArray.

### const QJsonValue QJsonDocument::operator[](QLatin1StringView key) const

This is an overloaded function.

### const QJsonValue QJsonDocument::operator[](QStringView key) const

This is an overloaded function.

## Related Non-Members

### `[noexcept]` bool operator!=(const QJsonDocument &lhs, const QJsonDocument &rhs)

Returns `true` if the lhs document is not equal to rhs document, `false` otherwise.

### `[noexcept]` bool operator==(const QJsonDocument &lhs, const QJsonDocument &rhs)

Returns `true` if the lhs document is equal to rhs document, `false` otherwise.