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.7k 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 HW-Developer
    27 Jun 2022, 20:45

    @mchinand thanks for the reply but I have to create the JSON file in the correct order of the objects inserted.
    So how can I resolve this issue?

    J Offline
    J Offline
    JonB
    wrote on 27 Jun 2022, 22:27 last edited by
    #4

    @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 1 Reply Last reply 28 Jun 2022, 07:07
    2
    • H Offline
      H Offline
      hobber
      Banned
      wrote on 28 Jun 2022, 01:03 last edited by
      #5
      This post is deleted!
      1 Reply Last reply
      0
      • J JonB
        27 Jun 2022, 22:27

        @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 28 Jun 2022, 07:07 last edited by
        #6

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

        J J 2 Replies Last reply 28 Jun 2022, 07:22
        0
        • H HW-Developer
          28 Jun 2022, 07:07

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

          J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 28 Jun 2022, 07:22 last edited by
          #7
          This post is deleted!
          1 Reply Last reply
          0
          • H HW-Developer
            28 Jun 2022, 07:07

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

            J Offline
            J Offline
            JonB
            wrote on 28 Jun 2022, 08:56 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 28 Jun 2022, 09:12
            0
            • J JonB
              28 Jun 2022, 08:56

              @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 28 Jun 2022, 09:12 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?

              J 1 Reply Last reply 28 Jun 2022, 09:16
              0
              • H HW-Developer
                28 Jun 2022, 09:12

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

                J Offline
                J Offline
                JonB
                wrote on 28 Jun 2022, 09:16 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 28 Jun 2022, 10:07
                0
                • J JonB
                  28 Jun 2022, 09:16

                  @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 28 Jun 2022, 10:07 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"
                          }
                      }
                  }
                  
                  J J 2 Replies Last reply 28 Jun 2022, 10:30
                  0
                  • H HW-Developer
                    28 Jun 2022, 10:07

                    @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 Offline
                    J Offline
                    JonB
                    wrote on 28 Jun 2022, 10:30 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
                      28 Jun 2022, 10:07

                      @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 Offline
                      J Offline
                      J.Hilk
                      Moderators
                      wrote on 28 Jun 2022, 10:53 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 29 Jun 2022, 16:04 last edited by
                        #14

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

                        1 Reply Last reply
                        1

                        13/14

                        28 Jun 2022, 10:53

                        • 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