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. Reading Json
Forum Update on Monday, May 27th 2025

Reading Json

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 4 Posters 768 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.
  • K Offline
    K Offline
    Kris Revi
    wrote on last edited by
    #1
        QString jsonStr;
        QString m_DeviceJsonFile("./devices.json");
        QFile file(m_DeviceJsonFile);
    
        if(QFileInfo::exists(m_DeviceJsonFile))
        {
            file.open(QIODevice::ReadWrite | QIODevice::Text);
    
            jsonStr = file.readAll();
            file.close();
    
            QJsonDocument myDock = QJsonDocument::fromJson(jsonStr.toUtf8());
            QJsonObject myObj = myDock.object();
    
            if (myObj.isEmpty())
            {
                qDebug() << "The Json File Is Empty!";
            }
            else
            {
                for (auto i = myObj.begin(); i != myObj.end(); i++)
                {
                    QString myStr = "Key -> " + i.key().toUtf8() + " : Value -> ";
                    myStr.append(i.value().toString());
                    qDebug() << myStr;
                }
            }
    
        }
    

    in my json file i have

    {
        "Name":"Command1", 
        "icon":"",
        "IPA":"test123",
        "UVLights":false,
        "Status":"online"
    },
    {
        "Name":"Command2", 
        "icon":"",
        "IPA":"192.168.10.146",
        "UVLights":false,
        "Status":"online"
    }
    

    why do i get The Json File Is Empty ?

    JonBJ 1 Reply Last reply
    0
    • K Kris Revi
          QString jsonStr;
          QString m_DeviceJsonFile("./devices.json");
          QFile file(m_DeviceJsonFile);
      
          if(QFileInfo::exists(m_DeviceJsonFile))
          {
              file.open(QIODevice::ReadWrite | QIODevice::Text);
      
              jsonStr = file.readAll();
              file.close();
      
              QJsonDocument myDock = QJsonDocument::fromJson(jsonStr.toUtf8());
              QJsonObject myObj = myDock.object();
      
              if (myObj.isEmpty())
              {
                  qDebug() << "The Json File Is Empty!";
              }
              else
              {
                  for (auto i = myObj.begin(); i != myObj.end(); i++)
                  {
                      QString myStr = "Key -> " + i.key().toUtf8() + " : Value -> ";
                      myStr.append(i.value().toString());
                      qDebug() << myStr;
                  }
              }
      
          }
      

      in my json file i have

      {
          "Name":"Command1", 
          "icon":"",
          "IPA":"test123",
          "UVLights":false,
          "Status":"online"
      },
      {
          "Name":"Command2", 
          "icon":"",
          "IPA":"192.168.10.146",
          "UVLights":false,
          "Status":"online"
      }
      

      why do i get The Json File Is Empty ?

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @Kris-Revi

      1. You should at least have verified the file.open(), and printed out jsonStr to be sure.

      2. You do not test the result of myDock = QJsonDocument::fromJson(). Read https://doc.qt.io/qt-5/qjsondocument.html#fromJson. Test myDock.isNull(). Pass in an error.

      3. Your JSON is not an object. It looks like an array. Is it legal? Don't you have to have:

      [
          {
          },
          {
          }
      ]
      

      ?

      K 1 Reply Last reply
      1
      • Christian EhrlicherC Online
        Christian EhrlicherC Online
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @JonB said in Reading Json:

        Your JSON is not an object. It looks like an array. Is it legal?

        Yes, it's fine but then

        QJsonObject myObj = myDock.object();

        is wrong. See also QJsonDocument::isArray() and QJsonDocument::isObject()

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        JonBJ Pablo J. RoginaP 2 Replies Last reply
        0
        • Christian EhrlicherC Christian Ehrlicher

          @JonB said in Reading Json:

          Your JSON is not an object. It looks like an array. Is it legal?

          Yes, it's fine but then

          QJsonObject myObj = myDock.object();

          is wrong. See also QJsonDocument::isArray() and QJsonDocument::isObject()

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @Christian-Ehrlicher said in Reading Json:

          Yes, it's fine

          So JSON allows file of just

          {
          }
          

          as an object, but if it comes across a comma

          {
          },
          {
          }
          

          it changes its mind to an array? Without the []. I didn't know that. So you can append to a JSON file more array items with worrying about the trailing ]?

          EDIT I just tried it at http://json.parser.online.fr/ or https://jsonparseronline.com/. It did not allow what you said (SyntaxError: Unexpected token , in JSON at position 3). It requires the [ ... ] around the two objects. So you sure it's legal?

          1 Reply Last reply
          0
          • JonBJ JonB

            @Kris-Revi

            1. You should at least have verified the file.open(), and printed out jsonStr to be sure.

            2. You do not test the result of myDock = QJsonDocument::fromJson(). Read https://doc.qt.io/qt-5/qjsondocument.html#fromJson. Test myDock.isNull(). Pass in an error.

            3. Your JSON is not an object. It looks like an array. Is it legal? Don't you have to have:

            [
                {
                },
                {
                }
            ]
            

            ?

            K Offline
            K Offline
            Kris Revi
            wrote on last edited by
            #5

            @JonB said in Reading Json:

            @Kris-Revi

            1. You should at least have verified the file.open(), and printed out jsonStr to be sure.

            2. You do not test the result of myDock = QJsonDocument::fromJson(). Read https://doc.qt.io/qt-5/qjsondocument.html#fromJson. Test myDock.isNull(). Pass in an error.

            3. Your JSON is not an object. It looks like an array. Is it legal? Don't you have to have:

            [
                {
                },
                {
                }
            ]
            

            ?

            so i did this

                if (file.open(QIODevice::ReadWrite | QIODevice::Text))
                {
                    jsonStr = file.readAll();
                    qDebug() << jsonStr;
                }
            

            and got

            "{\n    \"Name\":\"Command1\", \n    \"icon\":\"\",\n    \"IPA\":\"test123\",\n    \"UVLights\":false,\n    \"Status\":\"online\"\n},\n{\n    \"Name\":\"Command2\", \n    \"icon\":\"\",\n    \"IPA\":\"192.168.10.146\",\n    \"UVLights\":false,\n    \"Status\":\"online\"\n}
            
            JonBJ 1 Reply Last reply
            0
            • K Kris Revi

              @JonB said in Reading Json:

              @Kris-Revi

              1. You should at least have verified the file.open(), and printed out jsonStr to be sure.

              2. You do not test the result of myDock = QJsonDocument::fromJson(). Read https://doc.qt.io/qt-5/qjsondocument.html#fromJson. Test myDock.isNull(). Pass in an error.

              3. Your JSON is not an object. It looks like an array. Is it legal? Don't you have to have:

              [
                  {
                  },
                  {
                  }
              ]
              

              ?

              so i did this

                  if (file.open(QIODevice::ReadWrite | QIODevice::Text))
                  {
                      jsonStr = file.readAll();
                      qDebug() << jsonStr;
                  }
              

              and got

              "{\n    \"Name\":\"Command1\", \n    \"icon\":\"\",\n    \"IPA\":\"test123\",\n    \"UVLights\":false,\n    \"Status\":\"online\"\n},\n{\n    \"Name\":\"Command2\", \n    \"icon\":\"\",\n    \"IPA\":\"192.168.10.146\",\n    \"UVLights\":false,\n    \"Status\":\"online\"\n}
              
              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #6

              @Kris-Revi
              Fine, so you've proved your input is OK.

              You need to follow the discussion between @Christian-Ehrlicher and myself. Either way (even if it's illegal but QJsonDocument::fromJson() accepts it), if your input works at all it will be an array, not an object, which is the answer to your question.

              1 Reply Last reply
              0
              • Christian EhrlicherC Christian Ehrlicher

                @JonB said in Reading Json:

                Your JSON is not an object. It looks like an array. Is it legal?

                Yes, it's fine but then

                QJsonObject myObj = myDock.object();

                is wrong. See also QJsonDocument::isArray() and QJsonDocument::isObject()

                Pablo J. RoginaP Offline
                Pablo J. RoginaP Offline
                Pablo J. Rogina
                wrote on last edited by
                #7

                @Christian-Ehrlicher said in Reading Json:

                Yes, it's fine but then

                It looks like it's not, according to what @JonB have tested online, and what I also checked with the jsonlint website:

                Error: Parse error on line 7:

                ..."Status": "online"}, { "Name": "Comman

                ----------------------^

                Expecting 'EOF', got ','

                Upvote the answer(s) that helped you solve the issue
                Use "Topic Tools" button to mark your post as Solved
                Add screenshots via postimage.org
                Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                Christian EhrlicherC 1 Reply Last reply
                1
                • Pablo J. RoginaP Pablo J. Rogina

                  @Christian-Ehrlicher said in Reading Json:

                  Yes, it's fine but then

                  It looks like it's not, according to what @JonB have tested online, and what I also checked with the jsonlint website:

                  Error: Parse error on line 7:

                  ..."Status": "online"}, { "Name": "Comman

                  ----------------------^

                  Expecting 'EOF', got ','

                  Christian EhrlicherC Online
                  Christian EhrlicherC Online
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by Christian Ehrlicher
                  #8

                  @Pablo-J-Rogina A json document can either have an object or array as outer element. @JonB's example validates fine

                  [
                      {
                      },
                      {
                      }
                  ]
                  

                  But the json why was posted by @Kris-Revi is neither an array nor an object as base so QJsonParser will not parse it.

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  JonBJ 1 Reply Last reply
                  0
                  • Christian EhrlicherC Christian Ehrlicher

                    @Pablo-J-Rogina A json document can either have an object or array as outer element. @JonB's example validates fine

                    [
                        {
                        },
                        {
                        }
                    ]
                    

                    But the json why was posted by @Kris-Revi is neither an array nor an object as base so QJsonParser will not parse it.

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by JonB
                    #9

                    @Christian-Ehrlicher
                    Yes indeed. But that is not what you said! When I had said to the OP of his { }, { }

                    Your JSON is not an object. It looks like an array. Is it legal?

                    and you said back to me:

                    Yes, it's fine

                    [Although I haven't tested, I don't believe the OP's QJsonDocument::fromJson() is returning an array, I believe it is returning an error.]

                    Anyway, at least now we all seem to be agreeing (right?) that the OP will indeed need [ { }, { } ]. If he wants an array he must enclose it in [ ... ].

                    Christian EhrlicherC 1 Reply Last reply
                    0
                    • JonBJ JonB

                      @Christian-Ehrlicher
                      Yes indeed. But that is not what you said! When I had said to the OP of his { }, { }

                      Your JSON is not an object. It looks like an array. Is it legal?

                      and you said back to me:

                      Yes, it's fine

                      [Although I haven't tested, I don't believe the OP's QJsonDocument::fromJson() is returning an array, I believe it is returning an error.]

                      Anyway, at least now we all seem to be agreeing (right?) that the OP will indeed need [ { }, { } ]. If he wants an array he must enclose it in [ ... ].

                      Christian EhrlicherC Online
                      Christian EhrlicherC Online
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @JonB said in Reading Json:

                      and you said back to me:

                      I just forgot to add the Don't you have to have: in my quote :)

                      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                      Visit the Qt Academy at https://academy.qt.io/catalog

                      1 Reply Last reply
                      1

                      • Login

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