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. How to Minify JSON data in qt?..

How to Minify JSON data in qt?..

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 1.1k 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.
  • A Offline
    A Offline
    Akash_Patil
    wrote on 31 May 2021, 05:31 last edited by
    #1

    I have JSON file and reading it as file. Now i want to Minify JSON file content.
    [
    {
    "updated":1592183850402,
    "country":"Morocco",
    "countryInfo":
    {"_id":504,
    "iso2":"MA",
    "iso3":"MAR",
    "lat":32,
    "continent":"Africa",
    "activePerOneMillion":22.12
    }
    }
    ]

    The output should be

    [{"updated":1592183850402,"country":"Morocco","countryInfo":{"_id":504,"iso2":"MA","iso3":"MAR","lat":32,continent":"Africa","activePerOneMillion":22.12}}]

    J K 2 Replies Last reply 31 May 2021, 05:35
    0
    • A Akash_Patil
      31 May 2021, 05:31

      I have JSON file and reading it as file. Now i want to Minify JSON file content.
      [
      {
      "updated":1592183850402,
      "country":"Morocco",
      "countryInfo":
      {"_id":504,
      "iso2":"MA",
      "iso3":"MAR",
      "lat":32,
      "continent":"Africa",
      "activePerOneMillion":22.12
      }
      }
      ]

      The output should be

      [{"updated":1592183850402,"country":"Morocco","countryInfo":{"_id":504,"iso2":"MA","iso3":"MAR","lat":32,continent":"Africa","activePerOneMillion":22.12}}]

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 31 May 2021, 05:35 last edited by
      #2

      @Akash_Patil Use https://doc.qt.io/qt-5/qjsondocument.html#toJson-1 with QJsonDocument::Compact (https://doc.qt.io/qt-5/qjsondocument.html#JsonFormat-enum)...

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      5
      • A Akash_Patil
        31 May 2021, 05:31

        I have JSON file and reading it as file. Now i want to Minify JSON file content.
        [
        {
        "updated":1592183850402,
        "country":"Morocco",
        "countryInfo":
        {"_id":504,
        "iso2":"MA",
        "iso3":"MAR",
        "lat":32,
        "continent":"Africa",
        "activePerOneMillion":22.12
        }
        }
        ]

        The output should be

        [{"updated":1592183850402,"country":"Morocco","countryInfo":{"_id":504,"iso2":"MA","iso3":"MAR","lat":32,continent":"Africa","activePerOneMillion":22.12}}]

        K Offline
        K Offline
        KroMignon
        wrote on 31 May 2021, 05:39 last edited by
        #3

        @Akash_Patil Take a look a QJSonDocument::toJson().

        You can do something like this:

        QJSonDocument doc = QJSonDocument::fromJson(jsonString);
        
        qDebug() << doc.toJson(QJsonDocument::Compact);
        

        It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

        1 Reply Last reply
        3
        • A Offline
          A Offline
          Akash_Patil
          wrote on 31 May 2021, 06:24 last edited by Akash_Patil
          #4

          @KroMignon I tried the way you have mentioned.

          [void readJson()
          {
              QFile file;
              file.setFileName("sample2.json");
              file.open(QIODevice::ReadOnly );
          
              QByteArray ba2 = file.readAll();
                
              file.close();
               
              QJsonParseError parseError;
              QJsonDocument doc = QJsonDocument::fromJson(ba2, &parseError);
          
              qDebug() << doc.toJson(QJsonDocument::Compact);
          }
          

          Input File Data:

          {
          "firstName": "Joe",
          "lastName": "Jackson",
          "gender": "male",
          "age": 28,
          "address": {
          "streetAddress": "101",
          "city": "San Diego",
          "state": "CA"
          },
          "phoneNumbers": [
          { "type": "home", "number": "7349282382" }
          ]
          }

          In output there is backslash for every parameter.

          "{\"address\":{\"city\":\"San Diego\",\"state\":\"CA\",\"streetAddress\":\"101\"},\"age\":28,\"firstName\":\"Joe\",\"gender\":\"male\",\"lastName\":\"Jackson\",\"phoneNumbers\":[{\"number\":\"7349282382\",\"type\":\"home\"}]}"

          K J 2 Replies Last reply 31 May 2021, 06:33
          0
          • A Akash_Patil
            31 May 2021, 06:24

            @KroMignon I tried the way you have mentioned.

            [void readJson()
            {
                QFile file;
                file.setFileName("sample2.json");
                file.open(QIODevice::ReadOnly );
            
                QByteArray ba2 = file.readAll();
                  
                file.close();
                 
                QJsonParseError parseError;
                QJsonDocument doc = QJsonDocument::fromJson(ba2, &parseError);
            
                qDebug() << doc.toJson(QJsonDocument::Compact);
            }
            

            Input File Data:

            {
            "firstName": "Joe",
            "lastName": "Jackson",
            "gender": "male",
            "age": 28,
            "address": {
            "streetAddress": "101",
            "city": "San Diego",
            "state": "CA"
            },
            "phoneNumbers": [
            { "type": "home", "number": "7349282382" }
            ]
            }

            In output there is backslash for every parameter.

            "{\"address\":{\"city\":\"San Diego\",\"state\":\"CA\",\"streetAddress\":\"101\"},\"age\":28,\"firstName\":\"Joe\",\"gender\":\"male\",\"lastName\":\"Jackson\",\"phoneNumbers\":[{\"number\":\"7349282382\",\"type\":\"home\"}]}"

            K Offline
            K Offline
            KroMignon
            wrote on 31 May 2021, 06:33 last edited by
            #5

            @Akash_Patil said in How to Minify JSON data in qt?..:

            In output there is backslash for every parameter.

            I cannot understand what you mean? Which backslash?

            Perhaps is it only a debug artifact: you could try to use qDebug() << qPrintable(doc.toJson(QJsonDocument::Compact)); which removes the additional quotes at beginning and end of the string.

            It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

            1 Reply Last reply
            2
            • A Akash_Patil
              31 May 2021, 06:24

              @KroMignon I tried the way you have mentioned.

              [void readJson()
              {
                  QFile file;
                  file.setFileName("sample2.json");
                  file.open(QIODevice::ReadOnly );
              
                  QByteArray ba2 = file.readAll();
                    
                  file.close();
                   
                  QJsonParseError parseError;
                  QJsonDocument doc = QJsonDocument::fromJson(ba2, &parseError);
              
                  qDebug() << doc.toJson(QJsonDocument::Compact);
              }
              

              Input File Data:

              {
              "firstName": "Joe",
              "lastName": "Jackson",
              "gender": "male",
              "age": 28,
              "address": {
              "streetAddress": "101",
              "city": "San Diego",
              "state": "CA"
              },
              "phoneNumbers": [
              { "type": "home", "number": "7349282382" }
              ]
              }

              In output there is backslash for every parameter.

              "{\"address\":{\"city\":\"San Diego\",\"state\":\"CA\",\"streetAddress\":\"101\"},\"age\":28,\"firstName\":\"Joe\",\"gender\":\"male\",\"lastName\":\"Jackson\",\"phoneNumbers\":[{\"number\":\"7349282382\",\"type\":\"home\"}]}"

              J Offline
              J Offline
              jsulm
              Lifetime Qt Champion
              wrote on 31 May 2021, 06:40 last edited by
              #6

              @Akash_Patil said in How to Minify JSON data in qt?..:

              In output there is backslash for every parameter.

              The backslash is coming from qDebug(), which is meant to be used for debugging purposes. So, the code works as expected...

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              3

              5/6

              31 May 2021, 06:33

              • Login

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