Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QJsonValue::toArray() wraps correct json array inside another array

QJsonValue::toArray() wraps correct json array inside another array

Scheduled Pinned Locked Moved Solved General and Desktop
c++json
4 Posts 4 Posters 176 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Poggar
    wrote on 1 Feb 2025, 02:13 last edited by Poggar 2 Jan 2025, 02:14
    #1

    (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
    
    S J 2 Replies Last reply 1 Feb 2025, 07:59
    0
    • H Offline
      H Offline
      hskoglund
      wrote on 1 Feb 2025, 02:54 last edited by
      #2

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

      1 Reply Last reply
      3
      • P Poggar
        1 Feb 2025, 02:13

        (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
        
        S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 1 Feb 2025, 07:59 last edited by
        #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.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • P Poggar
          1 Feb 2025, 02:13

          (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
          
          J Offline
          J Offline
          JonB
          wrote on 1 Feb 2025, 10:12 last edited by JonB 2 Jan 2025, 10:13
          #4

          @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 use std::copy() to save you writing a for loop yourself if you wish.

          1 Reply Last reply
          0
          • P Poggar has marked this topic as solved on 1 Feb 2025, 11:49

          2/4

          1 Feb 2025, 02:54

          • Login

          • Login or register to search.
          2 out of 4
          • First post
            2/4
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved