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. Delete an entry in json file based on field "Name"
QtWS25 Last Chance

Delete an entry in json file based on field "Name"

Scheduled Pinned Locked Moved Unsolved General and Desktop
14 Posts 6 Posters 1.5k 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 21 Sept 2021, 20:40 last edited by
    #4
    This post is deleted!
    1 Reply Last reply
    0
    • K Kris Revi
      21 Sept 2021, 19:36

      @JonB would it be better to rewrite the json like so

      {
          "Studio Lights":
          {
                  "IPA": "studiolights",
                  "Icon": "fa_bell",
                  "Name": "Studio Lights",
                  "Status": "online",
                  "Type": "Strip",
                  "UVLights": "false"
          },
      
          "newdevice":
          {
                  "IPA": "newdevice",
                  "Icon": "fa_bolt",
                  "Name": "jaddajadda",
                  "Status": "online",
                  "Type": "Strip",
                  "UVLights": "false"
          },
          "newdevice2":
          {
                  "IPA": "newdevice2",
                  "Icon": "fa_bolt",
                  "Name": "lyse.net",
                  "Status": "online",
                  "Type": "Strip",
                  "UVLights": "false"
          }
      }
      
      J Online
      J Online
      JonB
      wrote on 21 Sept 2021, 20:57 last edited by
      #5

      @Kris-Revi
      I'm not with you? That is different JSON, and it's not what you asked?

      1 Reply Last reply
      0
      • K Kris Revi
        21 Sept 2021, 19:36

        @JonB would it be better to rewrite the json like so

        {
            "Studio Lights":
            {
                    "IPA": "studiolights",
                    "Icon": "fa_bell",
                    "Name": "Studio Lights",
                    "Status": "online",
                    "Type": "Strip",
                    "UVLights": "false"
            },
        
            "newdevice":
            {
                    "IPA": "newdevice",
                    "Icon": "fa_bolt",
                    "Name": "jaddajadda",
                    "Status": "online",
                    "Type": "Strip",
                    "UVLights": "false"
            },
            "newdevice2":
            {
                    "IPA": "newdevice2",
                    "Icon": "fa_bolt",
                    "Name": "lyse.net",
                    "Status": "online",
                    "Type": "Strip",
                    "UVLights": "false"
            }
        }
        
        P Offline
        P Offline
        Pablo J. Rogina
        wrote on 22 Sept 2021, 13:05 last edited by
        #6

        @Kris-Revi given the reply you've got in this other post, could this issue be marked as solved as well?

        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

        1 Reply Last reply
        0
        • A Offline
          A Offline
          AxelVienna
          wrote on 22 Sept 2021, 13:11 last edited by
          #7
          root.remove("Studio Lights");
          

          ...does what you want. Just write the file back as you did in the other post.

          C++ and Python walk into a bar. C++ reuses the first glass.

          K 1 Reply Last reply 24 Sept 2021, 11:34
          0
          • A AxelVienna
            22 Sept 2021, 13:11
            root.remove("Studio Lights");
            

            ...does what you want. Just write the file back as you did in the other post.

            K Offline
            K Offline
            Kris Revi
            wrote on 24 Sept 2021, 11:34 last edited by
            #8

            @AxelVienna said in Delete an entry in json file based on field "Name":

            root.remove("Studio Lights");

            this did nothing.

            code:

                        QFile file(save_path + data_folder + device_json);
                        file.open(QIODevice::ReadOnly | QIODevice::Text);
                        QByteArray data = file.readAll();
                        file.close();
            
                        QJsonParseError error;
                        QJsonDocument doc = QJsonDocument::fromJson(data, &error);
            
                        QJsonObject root = doc.object();
            
                        root.remove("Thisisjustatest");
            
                        doc = QJsonDocument(root);
            
                        file.open(QIODevice::WriteOnly | QIODevice::Text);
                        file.write(doc.toJson());
                        file.close();
            

            json file

            {
                "Device": [
                    {
                        "IPA": "newdevice",
                        "Icon": "fa_bell",
                        "Name": "Studio Lights <3",
                        "Status": "online",
                        "Type": "Matrix",
                        "UVLights": "false"
                    },
                    {
                        "IPA": "computerdesk",
                        "Icon": "fa_battery_full",
                        "Name": "Computer Desk",
                        "Status": "online",
                        "Type": "Strip",
                        "UVLights": "false"
                    },
                    {
                        "IPA": "newdevice",
                        "Icon": "fa_bold",
                        "Name": "Thisisjustatest",
                        "Status": "online",
                        "Type": "Strip",
                        "UVLights": "false"
                    }
                ]
            }
            
            J J 2 Replies Last reply 24 Sept 2021, 11:40
            0
            • K Kris Revi
              24 Sept 2021, 11:34

              @AxelVienna said in Delete an entry in json file based on field "Name":

              root.remove("Studio Lights");

              this did nothing.

              code:

                          QFile file(save_path + data_folder + device_json);
                          file.open(QIODevice::ReadOnly | QIODevice::Text);
                          QByteArray data = file.readAll();
                          file.close();
              
                          QJsonParseError error;
                          QJsonDocument doc = QJsonDocument::fromJson(data, &error);
              
                          QJsonObject root = doc.object();
              
                          root.remove("Thisisjustatest");
              
                          doc = QJsonDocument(root);
              
                          file.open(QIODevice::WriteOnly | QIODevice::Text);
                          file.write(doc.toJson());
                          file.close();
              

              json file

              {
                  "Device": [
                      {
                          "IPA": "newdevice",
                          "Icon": "fa_bell",
                          "Name": "Studio Lights <3",
                          "Status": "online",
                          "Type": "Matrix",
                          "UVLights": "false"
                      },
                      {
                          "IPA": "computerdesk",
                          "Icon": "fa_battery_full",
                          "Name": "Computer Desk",
                          "Status": "online",
                          "Type": "Strip",
                          "UVLights": "false"
                      },
                      {
                          "IPA": "newdevice",
                          "Icon": "fa_bold",
                          "Name": "Thisisjustatest",
                          "Status": "online",
                          "Type": "Strip",
                          "UVLights": "false"
                      }
                  ]
              }
              
              J Offline
              J Offline
              jsulm
              Lifetime Qt Champion
              wrote on 24 Sept 2021, 11:40 last edited by
              #9

              @Kris-Revi Why do you post now different Json than what you posted at the beginning? This is confusing...
              It should be clear that with this JSON you can't delete "Thisisjustatest" using root.remove("Thisisjustatest") as root does not have such a key...

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              K 1 Reply Last reply 24 Sept 2021, 12:55
              3
              • K Kris Revi
                24 Sept 2021, 11:34

                @AxelVienna said in Delete an entry in json file based on field "Name":

                root.remove("Studio Lights");

                this did nothing.

                code:

                            QFile file(save_path + data_folder + device_json);
                            file.open(QIODevice::ReadOnly | QIODevice::Text);
                            QByteArray data = file.readAll();
                            file.close();
                
                            QJsonParseError error;
                            QJsonDocument doc = QJsonDocument::fromJson(data, &error);
                
                            QJsonObject root = doc.object();
                
                            root.remove("Thisisjustatest");
                
                            doc = QJsonDocument(root);
                
                            file.open(QIODevice::WriteOnly | QIODevice::Text);
                            file.write(doc.toJson());
                            file.close();
                

                json file

                {
                    "Device": [
                        {
                            "IPA": "newdevice",
                            "Icon": "fa_bell",
                            "Name": "Studio Lights <3",
                            "Status": "online",
                            "Type": "Matrix",
                            "UVLights": "false"
                        },
                        {
                            "IPA": "computerdesk",
                            "Icon": "fa_battery_full",
                            "Name": "Computer Desk",
                            "Status": "online",
                            "Type": "Strip",
                            "UVLights": "false"
                        },
                        {
                            "IPA": "newdevice",
                            "Icon": "fa_bold",
                            "Name": "Thisisjustatest",
                            "Status": "online",
                            "Type": "Strip",
                            "UVLights": "false"
                        }
                    ]
                }
                
                J Online
                J Online
                JonB
                wrote on 24 Sept 2021, 11:52 last edited by JonB
                #10

                @Kris-Revi
                In addition to @jsulm.

                In this thread and another similar of yours I answered that if you wish to delete an object which has some attribute, say Name, set to some value like Thisisjustatest, then you must iterate/recurse to find that object, so that you can remove it. It does not matter how many times you ask, the answer is the same.

                1 Reply Last reply
                2
                • J jsulm
                  24 Sept 2021, 11:40

                  @Kris-Revi Why do you post now different Json than what you posted at the beginning? This is confusing...
                  It should be clear that with this JSON you can't delete "Thisisjustatest" using root.remove("Thisisjustatest") as root does not have such a key...

                  K Offline
                  K Offline
                  Kris Revi
                  wrote on 24 Sept 2021, 12:55 last edited by
                  #11

                  @jsulm said in Delete an entry in json file based on field "Name":

                  Why do you post now different Json than what you posted at the beginning? This is confusing...

                  it is different because keys are ordered alphabetically in a object!
                  i care about the order so i changed the structruce of the json and used an array!

                  1 Reply Last reply
                  0
                  • C Online
                    C Online
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on 24 Sept 2021, 13:24 last edited by Christian Ehrlicher
                    #12

                    @Kris-Revi said in Delete an entry in json file based on field "Name":

                    changed the structruce of the json and used an array!

                    Then you should also change your code which removes a key to use an array? Shouldn't you?

                    i care about the order

                    This will break as soon as you get json from somewhere else. There is no ordering (and not needed, there is not even an API to get keys ordered in any way)

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

                    J 1 Reply Last reply 24 Sept 2021, 13:34
                    1
                    • C Christian Ehrlicher
                      24 Sept 2021, 13:24

                      @Kris-Revi said in Delete an entry in json file based on field "Name":

                      changed the structruce of the json and used an array!

                      Then you should also change your code which removes a key to use an array? Shouldn't you?

                      i care about the order

                      This will break as soon as you get json from somewhere else. There is no ordering (and not needed, there is not even an API to get keys ordered in any way)

                      J Online
                      J Online
                      JonB
                      wrote on 24 Sept 2021, 13:34 last edited by
                      #13

                      @Christian-Ehrlicher said in Delete an entry in json file based on field "Name":

                      There is no ordering (and not needed, there is not even an API to get keys ordered in any way)

                      The OP has previously asked about this and I have discussed/advised him what he can do about his requirement for "order" in https://forum.qt.io/topic/130499/adding-to-json-file-without-deleting-the-content/10.

                      1 Reply Last reply
                      0
                      • A Offline
                        A Offline
                        AxelVienna
                        wrote on 26 Sept 2021, 08:36 last edited by AxelVienna
                        #14

                        I think this topic should be marked solved. It is clear that to delete an object by its key, you have to know both the key and its position in the JSON tree. If you don’t, you have to recurse through the JSON tree until you find what you want to delete. If there is no way to know or find the object of desire, nothing can be done with it at all. Order, btw, does not matter here. This is the beauty of proper recursion.

                        C++ and Python walk into a bar. C++ reuses the first glass.

                        1 Reply Last reply
                        0

                        13/14

                        24 Sept 2021, 13:34

                        • Login

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