TransWikia.com

Field output order when conver object to JSON

Salesforce Asked by ling on December 27, 2020

I need conver from a object to json in this order:

global class ResultDto {
    String ParlorCode;      // 1st
    String DepartmentCode;  // 2nd
    String EmployeeNumber;  // 3rd
    String Name;            // 4th
}

I return this object via custom rest API, or json.serialize this object to string,
the result is this, the field order is wrong:

{
  "ParlorCode" : "002895",
  "Name" : "Dev01",
  "EmployeeNumber" : null,
  "DepartmentCode" : "100091"
}

How can I get the specified order?
Any help would be appreciated.

3 Answers

By nature, JSON objects are unordered (just like maps in Apex or objects in Javascript). Whatever is consuming your JSON should not be order-dependent. However, this may be out of your control. If it is, you will need to use the JSON.createGenerator() method to build the JSON manually (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_class_System_Json.htm#apex_System_Json_createGenerator). This will be incredibly more time consuming than using the standard serialization.

Correct answer by dsharrison on December 27, 2020

If we want to serialize the object into its equivalent JSON along with maintaining order of its attribute

we can make use of a map with key as string and value as an object (can use any as per the requirement )

Note:

Put the key value in reverse order and after the JSON.serialize() you will get the JSON in the desired format

Example: If we want the json for this class with attribute order maintained:

global class ResultDto {
    String ParlorCode;      // 1st
    String DepartmentCode;  // 2nd
    String EmployeeNumber;  // 3rd
    String Name;            // 4th
}

we can make use of map like below:

Map<String, Object> jsonObj = new Map<String, Object>();

// adding keys in reverse order
jsonObj.put('Name', 'some string text');
jsonObj.put('EmployeeNumber', 'some string text');
jsonObj.put('DepartmentCode', 'some string text');
jsonObj.put('ParlorCode', 'some string text');

String jsonOutput = JSON.serialize(jsonObj);

System.debug(jsonOutput);

Output:

{
    "ParlorCode": "some string text",
    "DepartmentCode": "some string text",
    "EmployeeNumber": "some string text",
    "Name": "some string text"
}

Answered by Jasneet Dua on December 27, 2020

Actually this 'is' possible. Attributes are rendered in reverse order, so you can just rename them in reverse alphabetical order:

global class ResultDto {
    String zParlorCode;      // 1st
    String yDepartmentCode;  // 2nd
    String xEmployeeNumber;  // 3rd
    String wName;            // 4th
}

Of course explaining why your attributes are named like is is another question. As others have pointed out JSON should never depend on the order of the attributes being rendered. I have an edge scenario where a (dumb) report displays the first 200 characters of my Json field, and I'd like to therefore serialize the important data first.

Answered by Andrew on December 27, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP