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. Do QJsonDocument::fromJson() changes object position while parsing?
Forum Updated to NodeBB v4.3 + New Features

Do QJsonDocument::fromJson() changes object position while parsing?

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 281 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.
  • L Offline
    L Offline
    Learner1985
    wrote on last edited by Learner1985
    #1

    Hello Team,

    I am creating a small application which should do following:

    1. Copy existing Json file (say "abc.json") to a new name (say "pqr.json"
    2. Modify exisintg content of newly created json file "pqr.json".

    Issue which I am facing is that in my newly created Json file ("pqr.json".) position of json object changes.

    To simplify the problem, I have a created sample json file with less content.
    Example:
    "abc.json" file is having below contents:
    {
    "FirstName": "John",
    "LastName": "Wick",
    "Nick Name": "JW",
    "Height": 173
    }

    Modified Json file "pqr.json" changed to:
    {
    "FirstName": "John",
    "Height": 173,
    "LastName": "Rambo",
    "Nick Name": "JR"
    }

    Here, position of "Height" object is changed from 4th to 2nd. I don't want to change the position of objects because in my original file. If position of the object changes then application will not be able to read/parse data properly.

    Below is the code:
    bool operationResult = QFile::copy("abc.json", "pqr.json");

        		if(operationResult)
        
        		{
        
        			QFile fileToUpdate("pqr.json");
        
        			fileToUpdate.open(QIODevice::ReadOnly | QIODevice::Text);
        
        
        
        			QJsonDocument fileToUpdateJsonDocument = QJsonDocument::fromJson(fileToUpdate.readAll());
        
        
        
        			fileToUpdate.close();
        
        
        
        			if(!(fileToUpdateJsonDocument.isNull()))
        
        			{
        
        				QJsonObject fileToUpdateRootObject = fileToUpdateJsonDocument.object();
        
        				if(!fileToUpdateRootObject.isEmpty())
        
        				{
        
        					fileToUpdateRootObject.insert("FirstName", "John");
        
        					fileToUpdateRootObject.insert("LastName", "Rambo");
        
        
        
        					fileToUpdateJsonDocument.setObject(fileToUpdateRootObject);
        
        
        
        					fileToUpdate.open(QFile::WriteOnly | QFile::ExistingOnly);
        
        					fileToUpdate.write(fileToUpdateJsonDocument.toJson(QJsonDocument::Indented));
        
        					fileToUpdate.close();
        
        				}
        
        			}
        
        		}
    

    It seems to methat : "QJsonDocument::fromJson()" function changes position of the object while parsing.

    Please suggest how to solve this issue?

    C Christian EhrlicherC 2 Replies Last reply
    0
    • L Learner1985

      Hello Team,

      I am creating a small application which should do following:

      1. Copy existing Json file (say "abc.json") to a new name (say "pqr.json"
      2. Modify exisintg content of newly created json file "pqr.json".

      Issue which I am facing is that in my newly created Json file ("pqr.json".) position of json object changes.

      To simplify the problem, I have a created sample json file with less content.
      Example:
      "abc.json" file is having below contents:
      {
      "FirstName": "John",
      "LastName": "Wick",
      "Nick Name": "JW",
      "Height": 173
      }

      Modified Json file "pqr.json" changed to:
      {
      "FirstName": "John",
      "Height": 173,
      "LastName": "Rambo",
      "Nick Name": "JR"
      }

      Here, position of "Height" object is changed from 4th to 2nd. I don't want to change the position of objects because in my original file. If position of the object changes then application will not be able to read/parse data properly.

      Below is the code:
      bool operationResult = QFile::copy("abc.json", "pqr.json");

          		if(operationResult)
          
          		{
          
          			QFile fileToUpdate("pqr.json");
          
          			fileToUpdate.open(QIODevice::ReadOnly | QIODevice::Text);
          
          
          
          			QJsonDocument fileToUpdateJsonDocument = QJsonDocument::fromJson(fileToUpdate.readAll());
          
          
          
          			fileToUpdate.close();
          
          
          
          			if(!(fileToUpdateJsonDocument.isNull()))
          
          			{
          
          				QJsonObject fileToUpdateRootObject = fileToUpdateJsonDocument.object();
          
          				if(!fileToUpdateRootObject.isEmpty())
          
          				{
          
          					fileToUpdateRootObject.insert("FirstName", "John");
          
          					fileToUpdateRootObject.insert("LastName", "Rambo");
          
          
          
          					fileToUpdateJsonDocument.setObject(fileToUpdateRootObject);
          
          
          
          					fileToUpdate.open(QFile::WriteOnly | QFile::ExistingOnly);
          
          					fileToUpdate.write(fileToUpdateJsonDocument.toJson(QJsonDocument::Indented));
          
          					fileToUpdate.close();
          
          				}
          
          			}
          
          		}
      

      It seems to methat : "QJsonDocument::fromJson()" function changes position of the object while parsing.

      Please suggest how to solve this issue?

      C Offline
      C Offline
      ChrisW67
      wrote on last edited by
      #2

      @Learner1985 Welcome to the Qt Forums.

      There is no issue. The two data structures are logically identical, the second simply orders the keys alphabetically (probably because the code uses QJsonObject::keys()). The order of keys in a JSON object is not specified in the relevant RFC other than to mention that:

      • some parsers may expose the ordering of object members to callers.
      • interoperable parsers should not rely on order.

      Qt does not expose or preserve the file ordering of object members.

      So,

      If position of the object changes then application will not be able to read/parse data properly.

      Your application parser is fragile and you should focus there.
      If order is important then a JSON Array might be a better choice.

      1 Reply Last reply
      4
      • L Learner1985

        Hello Team,

        I am creating a small application which should do following:

        1. Copy existing Json file (say "abc.json") to a new name (say "pqr.json"
        2. Modify exisintg content of newly created json file "pqr.json".

        Issue which I am facing is that in my newly created Json file ("pqr.json".) position of json object changes.

        To simplify the problem, I have a created sample json file with less content.
        Example:
        "abc.json" file is having below contents:
        {
        "FirstName": "John",
        "LastName": "Wick",
        "Nick Name": "JW",
        "Height": 173
        }

        Modified Json file "pqr.json" changed to:
        {
        "FirstName": "John",
        "Height": 173,
        "LastName": "Rambo",
        "Nick Name": "JR"
        }

        Here, position of "Height" object is changed from 4th to 2nd. I don't want to change the position of objects because in my original file. If position of the object changes then application will not be able to read/parse data properly.

        Below is the code:
        bool operationResult = QFile::copy("abc.json", "pqr.json");

            		if(operationResult)
            
            		{
            
            			QFile fileToUpdate("pqr.json");
            
            			fileToUpdate.open(QIODevice::ReadOnly | QIODevice::Text);
            
            
            
            			QJsonDocument fileToUpdateJsonDocument = QJsonDocument::fromJson(fileToUpdate.readAll());
            
            
            
            			fileToUpdate.close();
            
            
            
            			if(!(fileToUpdateJsonDocument.isNull()))
            
            			{
            
            				QJsonObject fileToUpdateRootObject = fileToUpdateJsonDocument.object();
            
            				if(!fileToUpdateRootObject.isEmpty())
            
            				{
            
            					fileToUpdateRootObject.insert("FirstName", "John");
            
            					fileToUpdateRootObject.insert("LastName", "Rambo");
            
            
            
            					fileToUpdateJsonDocument.setObject(fileToUpdateRootObject);
            
            
            
            					fileToUpdate.open(QFile::WriteOnly | QFile::ExistingOnly);
            
            					fileToUpdate.write(fileToUpdateJsonDocument.toJson(QJsonDocument::Indented));
            
            					fileToUpdate.close();
            
            				}
            
            			}
            
            		}
        

        It seems to methat : "QJsonDocument::fromJson()" function changes position of the object while parsing.

        Please suggest how to solve this issue?

        Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @Learner1985 said in Do QJsonDocument::fromJson() changes object position while parsing?:

        Here, position of "Height" object is changed from 4th to 2nd. I don't want to change the position of objects because in my original file. If position of the object changes then application will not be able to read/parse data properly.

        Then your app is doing something wrong. There is no defined order for json object values.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        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