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"
Forum Updated to NodeBB v4.3 + New Features

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 1 Watching
  • 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

    say i have the name of "Studio Lights" how do i delete the entry for "Studio Lights" by it's name in this json file?

    {
        "Device": [
            {
                "IPA": "",
                "Icon": "fa_bell",
                "Name": "Studio Lights",
                "Status": "online",
                "Type": "Strip",
                "UVLights": "false"
            },
            {
                "IPA": "newdevice",
                "Icon": "fa_bolt",
                "Name": "jaddajadda",
                "Status": "online",
                "Type": "Strip",
                "UVLights": "false"
            },
            {
                "IPA": "newdevice",
                "Icon": "fa_bolt",
                "Name": "lyse.net",
                "Status": "online",
                "Type": "Strip",
                "UVLights": "false"
            }
        ]
    }
    

    this is what i got so far

        QFile file(_JsonFile);
        file.open(QIODevice::ReadOnly);
        QByteArray data = file.readAll();
        file.close();
        
        QJsonParseError error;
        QJsonDocument doc = QJsonDocument::fromJson(data, &error);
        
        QJsonObject root = doc.object();
    
    JonBJ 1 Reply Last reply
    0
    • K Kris Revi

      say i have the name of "Studio Lights" how do i delete the entry for "Studio Lights" by it's name in this json file?

      {
          "Device": [
              {
                  "IPA": "",
                  "Icon": "fa_bell",
                  "Name": "Studio Lights",
                  "Status": "online",
                  "Type": "Strip",
                  "UVLights": "false"
              },
              {
                  "IPA": "newdevice",
                  "Icon": "fa_bolt",
                  "Name": "jaddajadda",
                  "Status": "online",
                  "Type": "Strip",
                  "UVLights": "false"
              },
              {
                  "IPA": "newdevice",
                  "Icon": "fa_bolt",
                  "Name": "lyse.net",
                  "Status": "online",
                  "Type": "Strip",
                  "UVLights": "false"
              }
          ]
      }
      

      this is what i got so far

          QFile file(_JsonFile);
          file.open(QIODevice::ReadOnly);
          QByteArray data = file.readAll();
          file.close();
          
          QJsonParseError error;
          QJsonDocument doc = QJsonDocument::fromJson(data, &error);
          
          QJsonObject root = doc.object();
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @Kris-Revi
      At a guess: use QJsonObject::find() or QJsonObject::contains() to locate the Name key, or just iterate, and then delete its parent if that's what you want to do.

      K 1 Reply Last reply
      0
      • JonBJ JonB

        @Kris-Revi
        At a guess: use QJsonObject::find() or QJsonObject::contains() to locate the Name key, or just iterate, and then delete its parent if that's what you want to do.

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

        @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"
            }
        }
        
        JonBJ Pablo J. RoginaP 2 Replies Last reply
        0
        • K Offline
          K Offline
          Kris Revi
          wrote on last edited by
          #4
          This post is deleted!
          1 Reply Last reply
          0
          • K Kris Revi

            @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"
                }
            }
            
            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on 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

              @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"
                  }
              }
              
              Pablo J. RoginaP Offline
              Pablo J. RoginaP Offline
              Pablo J. Rogina
              wrote on 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
              • AxelViennaA Offline
                AxelViennaA Offline
                AxelVienna
                wrote on 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
                0
                • AxelViennaA AxelVienna
                  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 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"
                          }
                      ]
                  }
                  
                  jsulmJ JonBJ 2 Replies Last reply
                  0
                  • K Kris Revi

                    @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"
                            }
                        ]
                    }
                    
                    jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on 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
                    3
                    • K Kris Revi

                      @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"
                              }
                          ]
                      }
                      
                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on 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
                      • jsulmJ jsulm

                        @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 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
                        • Christian EhrlicherC Offline
                          Christian EhrlicherC Offline
                          Christian Ehrlicher
                          Lifetime Qt Champion
                          wrote on 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

                          JonBJ 1 Reply Last reply
                          1
                          • Christian EhrlicherC Christian Ehrlicher

                            @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)

                            JonBJ Offline
                            JonBJ Offline
                            JonB
                            wrote on 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
                            • AxelViennaA Offline
                              AxelViennaA Offline
                              AxelVienna
                              wrote on 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

                              • Login

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