6

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.

2
  • 2
    what is the use for order the fields?
    – Ratan Paul
    Dec 1, 2015 at 11:06
  • 3
    From json.org "An object is an unordered set of name/value pairs." You should write your JSON processor so that order doesn't matter.
    – powlo
    Dec 1, 2015 at 13:14

3 Answers 3

8

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.

1

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.

1

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .