Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Create Json with particular structure with C ++
Forum Updated to NodeBB v4.3 + New Features

Create Json with particular structure with C ++

Scheduled Pinned Locked Moved Unsolved C++ Gurus
13 Posts 6 Posters 2.5k Views 5 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.
  • G Offline
    G Offline
    Gabriela20
    wrote on 26 Jan 2021, 20:44 last edited by
    #1

    Hello everyone, could someone tell me if there is an external qt and c ++ library to create a json with a particular structure, I work with QJsonDocument but the variables are ordered alphabetically.

    J 1 Reply Last reply 26 Jan 2021, 22:58
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 26 Jan 2021, 20:53 last edited by
      #2

      Hi,

      You might want to check the excellent nlohmann json project.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      G 1 Reply Last reply 27 Jan 2021, 01:12
      4
      • G Gabriela20
        26 Jan 2021, 20:44

        Hello everyone, could someone tell me if there is an external qt and c ++ library to create a json with a particular structure, I work with QJsonDocument but the variables are ordered alphabetically.

        J Offline
        J Offline
        JKSH
        Moderators
        wrote on 26 Jan 2021, 22:58 last edited by
        #3

        @Gabriela20 said in Create Json with particular structure with C ++:

        I work with QJsonDocument but the variables are ordered alphabetically.

        May I ask why you need a particular order?

        In JSON, Objects are unordered structures. Therefore {"key1": 1, "key2": 2} is the same value as {"key2": 2, "key1": 1}

        Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

        G 1 Reply Last reply 26 Jan 2021, 23:20
        3
        • J JKSH
          26 Jan 2021, 22:58

          @Gabriela20 said in Create Json with particular structure with C ++:

          I work with QJsonDocument but the variables are ordered alphabetically.

          May I ask why you need a particular order?

          In JSON, Objects are unordered structures. Therefore {"key1": 1, "key2": 2} is the same value as {"key2": 2, "key1": 1}

          G Offline
          G Offline
          Gabriela20
          wrote on 26 Jan 2021, 23:20 last edited by
          #4

          @JKSH I am working with Azure And I only accept a specific structure so that Azure can work

          J J 2 Replies Last reply 26 Jan 2021, 23:42
          0
          • G Gabriela20
            26 Jan 2021, 23:20

            @JKSH I am working with Azure And I only accept a specific structure so that Azure can work

            J Online
            J Online
            JonB
            wrote on 26 Jan 2021, 23:42 last edited by
            #5

            @Gabriela20
            Seems very strange, since JSON is unordered. I assume you'll have to implement your own writer.

            G 1 Reply Last reply 27 Jan 2021, 01:14
            0
            • S SGaist
              26 Jan 2021, 20:53

              Hi,

              You might want to check the excellent nlohmann json project.

              G Offline
              G Offline
              Gabriela20
              wrote on 27 Jan 2021, 01:12 last edited by
              #6

              @SGaist I am a bit new to qt, could you explain me how to install the library

              K 1 Reply Last reply 29 Jan 2021, 22:54
              0
              • J JonB
                26 Jan 2021, 23:42

                @Gabriela20
                Seems very strange, since JSON is unordered. I assume you'll have to implement your own writer.

                G Offline
                G Offline
                Gabriela20
                wrote on 27 Jan 2021, 01:14 last edited by
                #7

                @JonB it works with QJsonDocument and sorts them alphabetically

                J 1 Reply Last reply 27 Jan 2021, 06:45
                0
                • G Gabriela20
                  27 Jan 2021, 01:14

                  @JonB it works with QJsonDocument and sorts them alphabetically

                  J Online
                  J Online
                  JonB
                  wrote on 27 Jan 2021, 06:45 last edited by JonB
                  #8

                  @Gabriela20
                  What works with QJsonDocument and what sorts them alphabetically?
                  Even if something outputs JSON sorted alphabetically --- and anything is free to do so, since JSON is unordered --- does that mean any reader requires the input to be sorted alphabetically in order to work? It certainly should not require this if it supports JSON.

                  F 1 Reply Last reply 28 Jan 2021, 23:28
                  1
                  • J JonB
                    27 Jan 2021, 06:45

                    @Gabriela20
                    What works with QJsonDocument and what sorts them alphabetically?
                    Even if something outputs JSON sorted alphabetically --- and anything is free to do so, since JSON is unordered --- does that mean any reader requires the input to be sorted alphabetically in order to work? It certainly should not require this if it supports JSON.

                    F Offline
                    F Offline
                    fcarney
                    wrote on 28 Jan 2021, 23:28 last edited by
                    #9

                    @JonB Internally QJsonObject converted json objects to a QVariantMap (QMap(QString,QVariant)). This is always sorted by key: "With QMap, the items are always sorted by key."
                    https://doc.qt.io/qt-5/qjsonobject.html

                    So if reading a json document with QJsonDocument you will always get sorted maps of objects. I don't see why this is an issue though. You can access the values in any order.

                    C++ is a perfectly valid school of magic.

                    J 1 Reply Last reply 29 Jan 2021, 08:11
                    0
                    • G Gabriela20
                      26 Jan 2021, 23:20

                      @JKSH I am working with Azure And I only accept a specific structure so that Azure can work

                      J Offline
                      J Offline
                      JKSH
                      Moderators
                      wrote on 29 Jan 2021, 00:31 last edited by
                      #10

                      @Gabriela20 said in Create Json with particular structure with C ++:

                      I am working with Azure And I only accept a specific structure so that Azure can work

                      Really? I would've expected Azure to accept unordered JSON... Do you have a link to any documentation (or an example) that shows that Azure expects a specific order?

                      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                      1 Reply Last reply
                      1
                      • F fcarney
                        28 Jan 2021, 23:28

                        @JonB Internally QJsonObject converted json objects to a QVariantMap (QMap(QString,QVariant)). This is always sorted by key: "With QMap, the items are always sorted by key."
                        https://doc.qt.io/qt-5/qjsonobject.html

                        So if reading a json document with QJsonDocument you will always get sorted maps of objects. I don't see why this is an issue though. You can access the values in any order.

                        J Online
                        J Online
                        JonB
                        wrote on 29 Jan 2021, 08:11 last edited by
                        #11

                        @fcarney said in Create Json with particular structure with C ++:

                        So if reading a json document with QJsonDocument you will always get sorted maps of objects.

                        Indeed. But that is only an implementation. The fact remains that JSON specifies no order for keys.

                        I am with @JKSH

                        Really? I would've expected Azure to accept unordered JSON... Do you have a link to any documentation (or an example) that shows that Azure expects a specific order?

                        I would like to see @Gabriela20 explain/substantiate

                        I am working with Azure And I only accept a specific structure so that Azure can work

                        If whatever s/he is using in Azure to read is JSON-compliant, where does it require, rather than accept, alphabetically-ordered keys?

                        1 Reply Last reply
                        0
                        • K Offline
                          K Offline
                          Kent-Dorfman
                          wrote on 29 Jan 2021, 22:49 last edited by Kent-Dorfman
                          #12

                          I use the nlohmann template class library...in fact, it is going to the moon.

                          1 Reply Last reply
                          0
                          • G Gabriela20
                            27 Jan 2021, 01:12

                            @SGaist I am a bit new to qt, could you explain me how to install the library

                            K Offline
                            K Offline
                            Kent-Dorfman
                            wrote on 29 Jan 2021, 22:54 last edited by
                            #13

                            @Gabriela20 It is a header-only library. Include the header file(s) (per instructions from the git project page) and have fun.

                            1 Reply Last reply
                            0

                            2/13

                            26 Jan 2021, 20:53

                            11 unread
                            • Login

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