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.7k 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.
  • Gabriela20G Gabriela20

    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.

    JKSHJ Online
    JKSHJ Online
    JKSH
    Moderators
    wrote on 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

    Gabriela20G 1 Reply Last reply
    3
    • JKSHJ JKSH

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

      Gabriela20G Offline
      Gabriela20G Offline
      Gabriela20
      wrote on last edited by
      #4

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

      JonBJ JKSHJ 2 Replies Last reply
      0
      • Gabriela20G Gabriela20

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

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

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

        Gabriela20G 1 Reply Last reply
        0
        • SGaistS SGaist

          Hi,

          You might want to check the excellent nlohmann json project.

          Gabriela20G Offline
          Gabriela20G Offline
          Gabriela20
          wrote on last edited by
          #6

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

          Kent-DorfmanK 1 Reply Last reply
          0
          • JonBJ JonB

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

            Gabriela20G Offline
            Gabriela20G Offline
            Gabriela20
            wrote on last edited by
            #7

            @JonB it works with QJsonDocument and sorts them alphabetically

            JonBJ 1 Reply Last reply
            0
            • Gabriela20G Gabriela20

              @JonB it works with QJsonDocument and sorts them alphabetically

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on 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.

              fcarneyF 1 Reply Last reply
              1
              • JonBJ JonB

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

                fcarneyF Offline
                fcarneyF Offline
                fcarney
                wrote on 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.

                JonBJ 1 Reply Last reply
                0
                • Gabriela20G Gabriela20

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

                  JKSHJ Online
                  JKSHJ Online
                  JKSH
                  Moderators
                  wrote on 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
                  • fcarneyF fcarney

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

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on 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
                    • Kent-DorfmanK Offline
                      Kent-DorfmanK Offline
                      Kent-Dorfman
                      wrote on 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
                      • Gabriela20G Gabriela20

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

                        Kent-DorfmanK Offline
                        Kent-DorfmanK Offline
                        Kent-Dorfman
                        wrote on 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

                        • Login

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