So, I have the next json:
{
"massive2":[
"a",
"c",
"b"
],
"key5":"val5",
"key2":"val2",
"massive1":[
"3",
"4",
"2",
"1"
],
"key3":"val3",
"key4":"val4",
"key1":"val1"
}
How can I sort all pairs by key and all elements in all massives (to the end of json tree) in alphabetical, or if it is massive in some other order to get json like this:
{
"key1":"val1",
"key2":"val2",
"key3":"val3",
"key4":"val4",
"key5":"val5",
"massive1":[
"1",
"2",
"3",
"4"
],
"massive2":[
"a",
"b",
"c"
]
}
I don't know beforehand how deep my json will be.
Maybe there are some java libraries for this?
let items = {
"massive2":[
"a",
"c",
"b"
],
"key5":"val5",
"key2":"val2",
"massive1":[
"3",
"4",
"2",
"1"
],
"key3":"val3",
"key4":"val4",
"key1":"val1"
};
let data = {};
Object.keys(items).sort().forEach(function(k){
data[k] = items[k];
});
console.log(data);
Related
Hi StackOverflow community,
I have a situation where I need to distinct the Java JSONObect key into a List. So that I can do a if else statement after that. Here is an example of my JSON Object.
{
"E23": "1111",
"E25": "5",
"G1": {
"b": "1",
"c": "1"
},
"G2": {
"d": "1"
},
"E2": "test pdf",
"E3": "1",
"E4": "1",
"E5a": "12121991",
"E6": "1",
"E8": "7110",
"E5b": 28,
"E9": "01",
"E1a": "1",
"H3b": "4",
"E24c1": "06",
"E1b": "i",
"H3a": "12",
"E26a": [
"04",
"07",
"09"
],
"E14a": {
"a": "1",
"b": "1",
"c": "1"
}
}
The end result expected is to become like this ArrayList [E,G,H]
Just FYI, the key.chartAt(0) can be from E-I. I am thinking to looping in each of the Object and check the character at 0 and put it in the List if the List doest contain the character yet. But I think its gonna consume a lot of java processing and my function will become longer. Is there any way that I can achieve my end result without looping through all the Object.
Any advise is appreciated.
You can try like this,
JSONObject json = getJsonObject(); // your JSONObject
Set<String> jsonKeys = json.keySet();
Set<Character> myKeySet = jsonKeys.stream()
.map(key-> key.charAt(0))
.collect(Collectors.toSet());
System.out.println(myKeySet);
Output:
[E,G,H]
Note: Since your keys should not be duplicated you use Set, else you can replace it with ArrayList
Im making an app thats dynamic to a json template (the app reads the template from the internet) and i need to read a JSONArray's key string.
my json:
{
"format": {
"Drive": [
"Mecanum",
"Omni",
"Regular"
],
"Drive-Configuration": [
"H",
"X"
],
"Glyph-Elevator": [
"Parallel Elevator",
"Stick Elevator",
"Strip Elevator"
],
"Glyph-Picker": [
"Strip Picker",
"Dual-Servo Picker"
],
"Relic-Elevator": [
"Horizontal Parallel"
],
"Relic-Holder": [
"Pliers",
"Dual-Servo With Rubber Pads"
],
"CypherBox-Fill": [
"0",
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"10",
"11",
"12"
],
"Autonomous": [
"Poor",
"Minimal",
"Okay",
"Good",
"Almost Perfect",
"Perfect"
]
}
}
what i want is to read the "Drive" and the "Drive-Configuration" names by code.
what i have:
JSONObject reader=new JSONObject(template);
JSONArray config=reader.getJSONArray("format");
for(int type=0;type<config.length();type++){
JSONArray con=config.getJSONArray(type);
//Here I Want To Read The Array's Name
}
Instead of JSON Parser use GSON.!
add this to your gradle
compile 'com.google.code.gson:gson:2.8.1'
Create a ClassMain.!
class MainClass{
#SerializedName("format")
Value format;
}
Class value{
#SerializedName("Drive")
ArrayList<String> drive;
#SerializedName("Drive-Configuration")
ArrayList<String> driveConfiguration;
generate getter and setter.!
}
and then Convert your JSON to GSON.!
MainClass mainClass= new Gson().fromJson(json.toString(), MainClass.class);
mainClass.getFormat().getDirve();
Used JSONObject.names();
to read the names
ArrayList<Template> tm = new ArrayList<>();
JSONObject reader = new JSONObject(format);
JSONObject config = reader.getJSONObject("format");
Iterator<String> types = config.keys();
while (types.hasNext()) {
String name = types.next();
tm.add(new Template(name, config.getJSONArray(name)));
}
Sample JSON
[
{
"id": "1",
"products": [
{
"id": "333",
"status": "Active"
},
{
"id": "222",
"status": "Inactive"
},
{
"id": "111",
"status": "Active"
}
]
},
{
"id": "2",
"products": [
{
"id": "6",
"status": "Active"
},
{
"id": "7",
"status": "Inactive"
}
]
}
]
I want to retrieve list of objects that have at least one active product.
Below code returns list of products but I want a list of ProdcutResponse. Any way to do this?
response.stream()
.map(ProductResponse::getProducts)
.filter(s -> "Active".equals(s.getType()))
.collect(Collectors.toList())
Because you didn't show much code, this will be a shot in the dark. My main suggestion would be to not map the Stream<ProductResponse> to a Stream<List<Product>> before collecting it to a list:
response.stream()
.filter(pr -> pr.getProducts().stream().anyMatch(s -> "Active".equals(s.getType())))
.collect(Collectors.toList());
You can change anyMatch to allMatch if you want the List<ProductResponse> to contain onlyProducts with active types (whatever that means).
Iam new to jolt.
Can you please tell me how can i trasform the below json message with array based upon the position in to the below output json message using jolt.
input message :
[
["20084541", "12020584", "Frohmann Dov", "2017", "2", "75", "T7", "DFZ", "CES", "", "", "0", "90", "2010"],
["20084541", "12020584", "Frohmann Dov", "2017", "3", "21", "T7", "DFZ", "CES", "", "", "0", "90", "2010"],
]
output Message :
[{
"policyReference": "20084541",
"insuredId": "12020584",
"insuredName": "Frohmann Dov",
"uwy": "2017",
"subLOB": "2",
"typeOfRisk": "75",
"aircraftcountryCode": "T7",
"aircraftId": "DFZ",
"manufacturerId": "CES",
"aircraftTypeCode": "",
"aircraftSubTypeCode": "",
"aircraftValueAmt": "0",
"aircraftWorkNo": "90",
"yearBuilt": "2010"
}, {
"policyReference": "20084541",
"insuredId": "12020584",
"insuredName": "Frohmann Dov",
"uwy": "2017",
"subLOB": "2",
"typeOfRisk": "75",
"aircraftcountryCode": "T7",
"aircraftId": "DFZ",
"manufacturerId": "CES",
"aircraftTypeCode": "",
"aircraftSubTypeCode": "",
"aircraftValueAmt": "0",
"aircraftWorkNo": "90",
"yearBuilt": "2010"
}]
Any help appreciated.
Spec
[
{
"operation": "shift",
"spec": {
"*": { // loop thru the outer array
// each item of the outer array, is an array
// match individual array indicies, and then send them to the
// output with the nice name.
"0": "[&1].policyReference",
"1": "[&1].insuredId",
"2": "[&1].insuredName"
// etc
}
}
}
]
I have a list of objects List, for each instance of those 'Place' objects there contains a number of members and lists.
I want to be able to filter the parent list based on the existence of a value in a child list.
These lists of objects are JSON being converted into a plain old Java object using Jackson.
Using the example data below (that has been converted into a java object in my actual code, but is shown here in JSON for readability) I want to filter out all places which do not have product 'C'.
{
"places": [
{
"Name": "ANAME",
"location": "A",
"type": "R",
"date": "2017-03-27",
"availableProducts": [
{
"product": "A",
"price": 15.00
},
{
"product": "B",
"price": 15.00
},
{
"product": "C",
},
{
"product": "D",
}
]
},
{
"Name": "NNAME",
"location": "A",
"type": "R",
"date": "2017-03-27",
"availableProducts": [
{
"product": "E",
"price": 15.00
},
{
"product": "F",
"price": 15.00
}
]
}
]
}
I've gotten this far, which obviously won't work since it will only look at the first product:
places.removeIf(p -> !p.getAvailableProducts().get(0).getProduct.equals("C"));
Try this:
places.removeIf(p -> !p.getAvailableProducts().stream().anyMatch(x -> x.getProduct.equals("C")));