I've been looking for a while and want a way to sort a JSON object like this:
{"results": [
{
"layerId": 5,
"layerName": "Pharmaceutical Entities",
"attributes": {
"OBJECTID": "35",
"FACILITYTYPE": "Pharmacy",
"FACILITYSUBTYPE": "24 Hr Pharmacy",
"COMMERCIALNAME_E": "SADD MAARAB PHARMACY"
},
"geometryType": "esriGeometryPoint",
},
{
"layerId": 5,
"layerName": "Pharmaceutical Entities",
"attributes": {
"OBJECTID": "1",
"FACILITYTYPE": "Pharmacy",
"FACILITYSUBTYPE": "24 Hr Pharmacy",
"COMMERCIALNAME_E": "GAYATHY HOSPITAL PHARMACY"
},
"geometryType": "esriGeometryPoint",
},
{
"layerId": 5,
"layerName": "Pharmaceutical Entities",
"attributes": {
"OBJECTID": "255",
"FACILITYTYPE": "Pharmacy",
"FACILITYSUBTYPE": "24 Hr Pharmacy",
"COMMERCIALNAME_E": "AL DEWAN PHARMACY"
},
"geometryType": "esriGeometryPoint",
}
]}
and sort is alphabetically by value of "COMMERCIALNAME_E" to get:
{"results": [
{
"layerId": 5,
"layerName": "Pharmaceutical Entities",
"attributes": {
"OBJECTID": "255",
"FACILITYTYPE": "Pharmacy",
"FACILITYSUBTYPE": "24 Hr Pharmacy",
"COMMERCIALNAME_E": "AL DEWAN PHARMACY"
},
"geometryType": "esriGeometryPoint"
},
{
"layerId": 5,
"layerName": "Pharmaceutical Entities",
"attributes": {
"OBJECTID": "1",
"FACILITYTYPE": "Pharmacy",
"FACILITYSUBTYPE": "24 Hr Pharmacy",
"COMMERCIALNAME_E": "GAYATHY HOSPITAL PHARMACY"
},
"geometryType": "esriGeometryPoint"
},
{
"layerId": 5,
"layerName": "Pharmaceutical Entities",
"attributes": {
"OBJECTID": "35",
"FACILITYTYPE": "Pharmacy",
"FACILITYSUBTYPE": "24 Hr Pharmacy",
"COMMERCIALNAME_E": "SADD MAARAB PHARMACY"
},
"geometryType": "esriGeometryPoint"
}
]}
I can't find any code that will do this. Can anyone give me some help?
JSON return type is an array of objects. Hence sort method cannot be used directly to sort the array. However, we can use a comparer function as the argument of the 'sort' method to get the sorting implemented.
Example-1: Sort JSON object using json.The value of the sort_keys argument of the dumps() function will require to set True to generate the sorted JSON objects from the array of JSON objects. Create a python file with the following script sort the JSON objects using json. dumps() function.
A JSONArray can parse text from a String to produce a vector-like object and supports java. util. List interface. We can sort a JSONArray in the below example.
The JSON RFC (RFC 4627) says that order of object members does not matter.
The constructor of a JSONObject can be used to convert an external form JSON text into an internal form whose values can be retrieved with the get () and opt () methods or to convert values into a JSON text using the put () and toString () methods. In the below example, we can sort the values of a JSONObject in the descending order.
Following sortByKey () function takes JSON Object as an input and returns JSON Array which is sorted by key. We will create JSON Array of [jsonValue, jsonKey] from JSON Object.
Sorting in Java 1 Arrays.Sort () works for arrays which can be of primitive data type also.#N#import java.util.Arrays;#N#public class GFG... 2 Collections.sort () works for objects Collections like ArrayList and LinkedList.#N#import java.util. 3 ;#N#public class GFG... More ...
The sorted JSONArray is now stored in the sortedJsonArray object. Not the answer you're looking for? Browse other questions tagged java json sorting or ask your own question.
I used JSON simple API to sort this. Here is my code:
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
public class SortJSON {
public static void main(String[] args) {
JSONParser parser = new JSONParser();
try {
JSONObject o = (JSONObject) parser.parse(new FileReader("test3.json"));
JSONArray array = (JSONArray) o.get("results");
ArrayList<JSONObject> list = new ArrayList<>();
for (int i = 0; i < array.size(); i++) {
list.add((JSONObject) array.get(i));
}
Collections.sort(list, new MyJSONComparator());
for (JSONObject obj : list) {
System.out.println(((JSONObject) obj.get("attributes")).get("OBJECTID"));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
class MyJSONComparator implements Comparator<JSONObject> {
@Override
public int compare(JSONObject o1, JSONObject o2) {
String v1 = (String) ((JSONObject) o1.get("attributes")).get("COMMERCIALNAME_E");
String v3 = (String) ((JSONObject) o2.get("attributes")).get("COMMERCIALNAME_E");
return v1.compareTo(v3);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With