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. Write in JSON file - Data not inserted in the correct order

Write in JSON file - Data not inserted in the correct order

Scheduled Pinned Locked Moved Unsolved General and Desktop
qjsonobjectjsonc++
14 Posts 6 Posters 6.8k 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.
  • H Offline
    H Offline
    hobber
    Banned
    wrote on last edited by
    #5
    This post is deleted!
    1 Reply Last reply
    0
    • JonBJ JonB

      @HW-Developer
      You ought not have such a requirement. JSON does not preserve the order of attributes of an object, and you should not need to.

      If you need to retain order put objects into a list/array.

      H Offline
      H Offline
      HW-Developer
      wrote on last edited by
      #6

      @JonB Can you give me an example how to use it with an array ?

      jsulmJ JonBJ 2 Replies Last reply
      0
      • H HW-Developer

        @JonB Can you give me an example how to use it with an array ?

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #7
        This post is deleted!
        1 Reply Last reply
        0
        • H HW-Developer

          @JonB Can you give me an example how to use it with an array ?

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

          @HW-Developer
          To fully control the output order of the JSON you show you will have to stop saving as object/attribute and do whatever to produce something like the following JSON:

          [    // start list/array of objects
            {    // start object
              "102": [    // start list of objects
                {    // start object
                  "NEUTRAL": {
                      "blend": "100"
                  }
                },    // end object
                {    // start object
                  "AE": {
                      "blend": "100"
                  }
                }    // end object
              ]    // end list of objects
            },    // end object
            {
              "105": [
                {
                  "NEUTRAL": {
                      "blend": "100"
                  }
                },
                {
                  "AE": {
                      "blend": "100"
                  }
                }
              ]
            }
          ]    // end list of objects
          

          See how all the objects have been individually put into lists where we need to control their order.

          You will not be able to do these by directly serializing/deserializing your existing C++ objects. Either you must change their structure to match this or you must write code when serializing to produce this JSON format and similar on deserialization.

          As you can see it's quite a bit of effort, and does not lend itself to readability. Which is why you should question why you ever need to control JSON output order like this, you are not intended to ever need to do so.

          H 1 Reply Last reply
          0
          • JonBJ JonB

            @HW-Developer
            To fully control the output order of the JSON you show you will have to stop saving as object/attribute and do whatever to produce something like the following JSON:

            [    // start list/array of objects
              {    // start object
                "102": [    // start list of objects
                  {    // start object
                    "NEUTRAL": {
                        "blend": "100"
                    }
                  },    // end object
                  {    // start object
                    "AE": {
                        "blend": "100"
                    }
                  }    // end object
                ]    // end list of objects
              },    // end object
              {
                "105": [
                  {
                    "NEUTRAL": {
                        "blend": "100"
                    }
                  },
                  {
                    "AE": {
                        "blend": "100"
                    }
                  }
                ]
              }
            ]    // end list of objects
            

            See how all the objects have been individually put into lists where we need to control their order.

            You will not be able to do these by directly serializing/deserializing your existing C++ objects. Either you must change their structure to match this or you must write code when serializing to produce this JSON format and similar on deserialization.

            As you can see it's quite a bit of effort, and does not lend itself to readability. Which is why you should question why you ever need to control JSON output order like this, you are not intended to ever need to do so.

            H Offline
            H Offline
            HW-Developer
            wrote on last edited by
            #9

            @JonB So When trying to read this JSON File if I want it to get me the NEUTRAL before the AE expression. Can It be done?

            JonBJ 1 Reply Last reply
            0
            • H HW-Developer

              @JonB So When trying to read this JSON File if I want it to get me the NEUTRAL before the AE expression. Can It be done?

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

              @HW-Developer
              In the output I show you NEUTRAL does come before AE (because you put them into an array in that order) so you do get it back as you request. In your original for the second element you physically have AE before NEUTRAL in the file, so you cannot help but read it back in that order.

              H 1 Reply Last reply
              0
              • JonBJ JonB

                @HW-Developer
                In the output I show you NEUTRAL does come before AE (because you put them into an array in that order) so you do get it back as you request. In your original for the second element you physically have AE before NEUTRAL in the file, so you cannot help but read it back in that order.

                H Offline
                H Offline
                HW-Developer
                wrote on last edited by
                #11

                @JonB So there is no solution for my first post because I Want "Neutral" comes allways first even if I have other expression than "AE" like "SCh" for example :

                {
                    "0": {
                        "NEUTRAL": {
                            "blend": "100"
                        }
                    },
                    "101": {
                        "NEUTRAL": {
                            "blend": "100"
                        }
                    },
                    "102": {
                        "AE": {
                            "blend": "100"
                        },
                        "NEUTRAL": {
                            "blend": "100"
                        }
                    },
                    "105": {
                        "AE": {
                            "blend": "100"
                        },
                        "NEUTRAL": {
                            "blend": "100"
                        }
                    },
                    "106": {
                        "NEUTRAL": {
                            "blend": "100"
                        },
                        "SCh": {
                            "blend": "100"
                        }
                    }
                }
                
                JonBJ J.HilkJ 2 Replies Last reply
                0
                • H HW-Developer

                  @JonB So there is no solution for my first post because I Want "Neutral" comes allways first even if I have other expression than "AE" like "SCh" for example :

                  {
                      "0": {
                          "NEUTRAL": {
                              "blend": "100"
                          }
                      },
                      "101": {
                          "NEUTRAL": {
                              "blend": "100"
                          }
                      },
                      "102": {
                          "AE": {
                              "blend": "100"
                          },
                          "NEUTRAL": {
                              "blend": "100"
                          }
                      },
                      "105": {
                          "AE": {
                              "blend": "100"
                          },
                          "NEUTRAL": {
                              "blend": "100"
                          }
                      },
                      "106": {
                          "NEUTRAL": {
                              "blend": "100"
                          },
                          "SCh": {
                              "blend": "100"
                          }
                      }
                  }
                  
                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by JonB
                  #12

                  @HW-Developer said in Write in JSON file - Data not inserted in the correct order:

                  So there is no solution for my first post

                  I, and @mchinand, have told you that you cannot control the order in which attributes/members of an object (e.g. AE & Neutral here) are serialized in JSON, As you have already discovered for yourself. Only array/list items appear in a particular order, which is why you would need to change to using them if you must control the order.

                  It does not matter how many times you ask the same question you will get the same answer. It's also why you simply should not, and should not have any need to, dictate the output order. If I were you I would get rid of why ever you have such a requirement, as we have already said.

                  1 Reply Last reply
                  3
                  • H HW-Developer

                    @JonB So there is no solution for my first post because I Want "Neutral" comes allways first even if I have other expression than "AE" like "SCh" for example :

                    {
                        "0": {
                            "NEUTRAL": {
                                "blend": "100"
                            }
                        },
                        "101": {
                            "NEUTRAL": {
                                "blend": "100"
                            }
                        },
                        "102": {
                            "AE": {
                                "blend": "100"
                            },
                            "NEUTRAL": {
                                "blend": "100"
                            }
                        },
                        "105": {
                            "AE": {
                                "blend": "100"
                            },
                            "NEUTRAL": {
                                "blend": "100"
                            }
                        },
                        "106": {
                            "NEUTRAL": {
                                "blend": "100"
                            },
                            "SCh": {
                                "blend": "100"
                            }
                        }
                    }
                    
                    J.HilkJ Offline
                    J.HilkJ Offline
                    J.Hilk
                    Moderators
                    wrote on last edited by J.Hilk
                    #13

                    @HW-Developer said in Write in JSON file - Data not inserted in the correct order:

                    So there is no solution for my first post because I Want "Neutral" comes allways first even if I have other expression than "AE" like "SCh"

                    no, because its not required by the standard,

                    You can of course write your very own json parser/writer and implement that behaviour yourself!

                    you could also use the source code of QJsonObject and change the key ordered QMap to an arbitrary ordered QHash without much hassle, (I think)


                    Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                    Q: What's that?
                    A: It's blue light.
                    Q: What does it do?
                    A: It turns blue.

                    1 Reply Last reply
                    3
                    • H Offline
                      H Offline
                      HW-Developer
                      wrote on last edited by
                      #14

                      I did resolve it using rapidjson instead of QJsonObject
                      Thank you all

                      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