QJsonValue::toArray() wraps correct json array inside another array
-
wrote on 1 Feb 2025, 02:13 last edited by Poggar 2 Jan 2025, 02:14
(QT 6.8.1 on linux with cmake)I found this weird behavior where the QJsonValue::toArray function returns the correct array, but wrapped inside another array and I'm not sure if I'm using the library correctly:
file.json:{ "success": true, "value_1": false, "array": [ { "some_value_A": "AAA", "value_1" : "111" }, { "some_value_B": "BBB", "value_2" : "222" }, { "some_value_C": "CCC", "value_3" : "333" } ] }
C++ Code:
const QJsonDocument json_doc{ json_document_from_file }; qDebug() << "json_doc.isObject() = " << json_doc.isObject(); // prints true const QJsonObject object{ json_doc.object() }; qDebug() << "object.contains(\"array\") = " << object.contains("array"); //prints true qDebug() << "object.size() = " << object.size(); //prints 3 const QJsonValue value{ object["array"] }; qDebug() << "value.isArray() = " << value.isArray(); //prints true const QJsonArray array{ value.toArray() }; qDebug() << "array.size() = " << array.size() << "\t Should be 3"; //prints 1 qDebug() << "array.at(0).toArray().size() = " << array.at(0).toArray().size() << "\t Should be 3"; //prints 3 qDebug() << "value.toArray().size() = " << value.toArray().size() << "\t Should be 3"; //prints 3
-
wrote on 1 Feb 2025, 02:54 last edited by
Hi, it's easy to wrap JSON in too many layers, I think the culprit is this line
....
const QJsonArray array{ value.toArray() };
...
I think that QJSonArray array is a double wrapper, that's why you get size() = 1, because that's the size of the outer array (i.e. the outer array has 1 element, which is one array which has 3 elements :-). -
(QT 6.8.1 on linux with cmake)I found this weird behavior where the QJsonValue::toArray function returns the correct array, but wrapped inside another array and I'm not sure if I'm using the library correctly:
file.json:{ "success": true, "value_1": false, "array": [ { "some_value_A": "AAA", "value_1" : "111" }, { "some_value_B": "BBB", "value_2" : "222" }, { "some_value_C": "CCC", "value_3" : "333" } ] }
C++ Code:
const QJsonDocument json_doc{ json_document_from_file }; qDebug() << "json_doc.isObject() = " << json_doc.isObject(); // prints true const QJsonObject object{ json_doc.object() }; qDebug() << "object.contains(\"array\") = " << object.contains("array"); //prints true qDebug() << "object.size() = " << object.size(); //prints 3 const QJsonValue value{ object["array"] }; qDebug() << "value.isArray() = " << value.isArray(); //prints true const QJsonArray array{ value.toArray() }; qDebug() << "array.size() = " << array.size() << "\t Should be 3"; //prints 1 qDebug() << "array.at(0).toArray().size() = " << array.at(0).toArray().size() << "\t Should be 3"; //prints 3 qDebug() << "value.toArray().size() = " << value.toArray().size() << "\t Should be 3"; //prints 3
@Poggar hi,
@hskoglund is correct, you are not using a copy constructor but a constructor with an initializer list hence you are creating a new array out of the one you gave it since an array in an array is a valid use case.
The documentation could be clearer about that case though.
-
(QT 6.8.1 on linux with cmake)I found this weird behavior where the QJsonValue::toArray function returns the correct array, but wrapped inside another array and I'm not sure if I'm using the library correctly:
file.json:{ "success": true, "value_1": false, "array": [ { "some_value_A": "AAA", "value_1" : "111" }, { "some_value_B": "BBB", "value_2" : "222" }, { "some_value_C": "CCC", "value_3" : "333" } ] }
C++ Code:
const QJsonDocument json_doc{ json_document_from_file }; qDebug() << "json_doc.isObject() = " << json_doc.isObject(); // prints true const QJsonObject object{ json_doc.object() }; qDebug() << "object.contains(\"array\") = " << object.contains("array"); //prints true qDebug() << "object.size() = " << object.size(); //prints 3 const QJsonValue value{ object["array"] }; qDebug() << "value.isArray() = " << value.isArray(); //prints true const QJsonArray array{ value.toArray() }; qDebug() << "array.size() = " << array.size() << "\t Should be 3"; //prints 1 qDebug() << "array.at(0).toArray().size() = " << array.at(0).toArray().size() << "\t Should be 3"; //prints 3 qDebug() << "value.toArray().size() = " << value.toArray().size() << "\t Should be 3"; //prints 3
wrote on 1 Feb 2025, 10:12 last edited by JonB 2 Jan 2025, 10:13@Poggar
As the above two posts have said. Fastest way to convert Vector to QJsonArray? gives code and confirms there is no faster way than some sort of iteration over the C++ array adding elements to the JSON array, you can usestd::copy()
to save you writing afor
loop yourself if you wish. -
2/4