Difficulty generating a JSON object in the correct order

Asked

Viewed 434 times

0

I am trying to generate a JSON object by following this model:

{
    "schemas": [
        "urn:ietf:params:scim:schemas:extension:oracle:2.0:OIG:User",
        "urn:ietf:params:scim:schemas:core:2.0:User",
        "urn:ietf:params:scim:schemas:extension:oracle:2.0:IDM:User",
        "urn:ietf:params:scim:schemas:extension:enterprise:2.0:User"
    ],
    "urn:ietf:params:scim:schemas:extension:oracle:2.0:OIG:User": {
        "userLoginAttemptsCounter": 0,
        "ldapCommonNameGenerated": 0,
        "userPasswordResetAttemptsCounter": 0,
        "ldapCommonName": "System Administrator",
        "passwordWarnDate": "2015-06-30T01:51:27.000-07:00",
        "lastSuccessfulLoginDate": "2015-03-11T00:00:00.000-07:00",
        "homeOrganization": {
            "value": "1",
            "$ref": "http://HOST_NAME:PORT/idaas/im/scim/v1/Organizations/1"
        },
        "passwordPolicyDescription": [
            {
                "value": "Password must not match or contain first name."
            }
        ],
        "disabled": false,
        "dataLevel": "2",
        "organizations": [
            {
                "value": "1",
                "$ref": "http://HOST_NAME:PORT/idaas/im/scim/v1/Organizations/1",
                "display": "Xellerate Users"
            }
        ]
    }

}

but I’m coming to this model:

{
    "schema": [
        "urn:ietf:params:scim:schemas:extension:oracle:2.0:OIG:User",
        "urn:ietf:params:scim:schemas:core:2.0:User",
        "urn:ietf:params:scim:schemas:extension:oracle:2.0:IDM:User",
        "urn:ietf:params:scim:schemas:extension:enterprise:2.0:User"
    ],
    "urn:ietf:params:scim:schemas:extension:oracle:2.0:OIG:User": {
        "userLoginAttemptsCounter": 0,
        "ldapCommonNameGenerated": 0,
        "passwordWarnDate": "2015-06-30T01:51:27.000-07:00",
        "homeOrganization": {
            "value": "1",
            "$ref": "http:\\HOST_NAME:PORT\\idaas\\im\\scim\\v1\\Organizations\\1"
        },
        "lastSuccessfulLoginDate": "2015-03-11T00:00:00.000-07:00",
        "organizations": [
            {
                "display": "Xellerate Users",
                "value": "1",
                "$ref": "http://HOST_NAME:PORT/idaas/im/scim/v1/Organizations/1"
            }
        ],
        "disabled": false,
        "ldapCommonName": "System Administrator",
        "passwordPolicyDescription": [
            {
                "value": "Password must not match or contain first name."
            }
        ],
        "userPasswordResetAttemptsCounter": 0,
        "dataLevel": "2"
    }
}

My code:

import org.json.JSONException;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

public class Main {

    public static void main(String[] args) throws JSONException {

        //obj principal
        JSONObject obj = new JSONObject();
        //obj usuario
        JSONObject objuser = new JSONObject();
        objuser.put("userLoginAttemptsCounter", 0);
        objuser.put("ldapCommonNameGenerated", 0);
        objuser.put("userPasswordResetAttemptsCounter", 0);
        objuser.put("ldapCommonName", "System Administrator");
        objuser.put("passwordWarnDate", "2015-06-30T01:51:27.000-07:00");
        objuser.put("lastSuccessfulLoginDate", "2015-03-11T00:00:00.000-07:00");


        JSONObject objcompuser = new JSONObject();
        objcompuser.put("value", "1");
        objcompuser.put("$ref", "http:\\HOST_NAME:PORT\\idaas\\im\\scim\\v1\\Organizations\\1");


        objuser.put("homeOrganization", objcompuser);

        JSONObject psw_des = new JSONObject();
        psw_des.put("value", "Password must not match or contain first name.");

        JSONObject org_des = new JSONObject();
        org_des.put("value", "1");
        org_des.put("$ref", "http://HOST_NAME:PORT/idaas/im/scim/v1/Organizations/1");
        org_des.put("display", "Xellerate Users");
        //obj headers
        JSONArray headers = new JSONArray();
        headers.add("urn:ietf:params:scim:schemas:extension:oracle:2.0:OIG:User");
        headers.add("urn:ietf:params:scim:schemas:core:2.0:User");
        headers.add("urn:ietf:params:scim:schemas:extension:oracle:2.0:IDM:User");
        headers.add("urn:ietf:params:scim:schemas:extension:enterprise:2.0:User");
        obj.put("schema", headers);
        obj.put("urn:ietf:params:scim:schemas:extension:oracle:2.0:OIG:User", objuser);

        JSONArray objpass = new JSONArray();
        objpass.add(psw_des);
        JSONArray objorg = new JSONArray();
        objorg.add(org_des);

        objuser.put("passwordPolicyDescription", objpass);
        objuser.put("disabled", false);
        objuser.put("dataLevel", "2");
        objuser.put("organizations", objorg);
        System.out.println(obj.toJSONString());
    }
}

Obs(am using lib simple-json)

  • I think there is no way to write a JSON always in the same order (unless you write in your hand.. Use a Stringbuilder and populate everything in your hand). After all, the idea of JSON is not that the order of the attributes is relevant.. So I don’t think there’s much of a way, anyway, no way...

1 answer

1


According to the specification of JSON:

An Object is an unordered Collection of zero or more name/value pairs, Where a name is a string and a value is a string, number, Boolean, null, Object, or array.

That is: the order of the values of an object is irrelevant, because an object is an unordered collection. You shouldn’t have to worry about that.


If you really needs an orderly JSON for some service that has been implemented in the wrong way, you can use a Linkedhashmap when creating your Jsonobject.

  • True as @Igorventurelli said, for my goal it is easier to ride in hand with Stringbuilder.

  • As I said above, you can continue using exactly the same library, changing only how Jsonobject is created. No need to do everything manually.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.