Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

How can i sort Nested JSON Array? Like for a JSON below...

{
    "id":"rtmc05.lax.someabc.net",
    "name":"rtmc05.lax.someabc.net",

    "tenants":[{
        "id":"rtmc",
        "name":"rtmc"
    },{
        "id":"hrs",
        "name":"hrs"
    },{
        "id":"amotelbe1",
        "name":"amotelbe"
    },{
        "id":"cds",
        "name":"cds"
    },{
        "id":"idx-server",
        "name":"idx-server",

        "tenants":[{
            "id":"amotelbe",
            "name":"amotelbe",

            "tenants":[{
                "id":"amotelui",
                "name":"amotelui"
            }]
        }]
    }]
}
share|improve this question

3 Answers 3

Parse it into (javascript) objects then write a sort function that sorts an array of such javascript objects.

share|improve this answer

Deserialize it to POJOs(with Gson or Jackson), and write a Comparator for that POJOs.

share|improve this answer

There's a few parts implicit to your question, and it's not clear where you're having trouble:

  1. How do you take a JSON string and make usable Java objects out of it. (I'm assuming Java, not JavaScript, since you've tagged your question with "java".)
  2. How do you sort those objects after they're made?
  3. How do you handle sorting the nested parts? (In your example, "idx-server" has sub-tenants.)

Not sure exactly which parts of this you're having trouble with, so here's some notes for all three.

Part 1: Getting Java objects

I agree with the other guy that Jackson is a good JSON parser to use. Here's a couple lines of code you could use to parse some JSON:

String jsonString = "..."; // Load this in whatever way makes sense for you
ObjectMapper mapper = new ObjectMapper();
Map<String,Object> parsedJson = mapper.readValue(jsonString, Map.class);

If your JSON string is really huge, then there are other readValue overloads that you can use to avoid reading the whole String into memory.

Part 2: Sorting Java objects

Once you've got the parsed JSON, sorting is just a matter of calling Collections.sort(...), passing in the tenants array. Plus you'll need to write a Comparator that defines the ordering that you want. For example, here's a comparator that sorts by name:

public class NameComparator implements Comparator<Map<String,Object>> {
    public int compare(Map<String,Object> o1, Map<String,Object> o2) {
        String name1 = (String) o1.get("name");
        String name2 = (String) o2.get("name");
        return name1.compareTo(name2);
    }
}

Then you get the tenants array out (Jackson makes them into ArrayList objects) and call Collections.sort(...). For example,

List<Map<String,Object>> tenants =
        (List<Map<String,Object>>) parsedJson.get("tenants");
Collections.sort(tenants, new NameComparator());

Part 3: Handling the nesting

The clearest way to do this is to add some extra code to walk through your JSON looking for any object with a tenants array, and sort it. For example, here's a recursive function that should do it:

public static void recursiveSortTenants(Map<String,Object> jsonObject) {
    List<Map<String,Object>> tenants =
            (List<Map<String,Object>>) jsonObject.get("tenants");
    if (tenants != null) {
        Collections.sort(tenants, new NameComparator());
        // For each tenant, see if it has sub-tenants.  If so,
        // call this function again to sort them.
        for (Map<String,Object> tenant : tenants) {
            if (tenants.containsKey("tenants")) {
                recursiveSortTenants(tenant);
            }
        }
    }
}

Hope this helps!

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.