I have a JSONObject.
{
"_shards":{
"total":251,
"failed":0,
"successful":251
},
"hits":{
"hits":[
],
"total":7775532,
"max_score":0
},
"took":517,
"timed_out":false,
"facets":{
"terms":{
"total":2287,
"other":0,
"terms":[
{
"count":2268,
"term":"contact"
},
{
"count":19,
"term":""
}
],
"_type":"terms",
"missing":424
}
}
}
I want to get the value of hits.total which here is 7775532.
How can I get that ? Is there some function that can help ? I am using Java
jsonObject.getJSONObject("hits").get("total")
Related
I can't figure out how I would do the ff.
I have the ff. Payload
{
"code": null,
"message": null,
"recCtrlOut": {
},
"acctSumm": [
{
"acctBasic": {
"acctType": "SV",
"acctId": "123",
},
"acctBasic": {
"acctType": "SV",
"acctId": "321",
}
}
]
}
And I just want to get the acctId params and assign it to a new plain array of accountIds. How do I do it in Spring/Java?. Thanks
Try using json path. Library can be found here. E.g. say you had json like this:
{
"code": null,
"message": null,
"recCtrlOut": {
},
"acctSumm": [
{
"acctBasic": {
"acctType": "SV",
"acctId": "123"
}
},
{
"acctBasic": {
"acctType": "SV",
"acctId": "321"
}
}
]
}
Actual code would be something like:
List<String> ids = JsonPath.read(json, "$.acctSumm[*].acctBasic.acctId");
The above list will now hold:
["123","321"]
If you wanna learn json path syntax, you could try using this online tool. Here is also a guide to help you get started with json path.
I have nested json data, like so:
{
"libraries":[
{
"State":"California",
"genres":[
{
"genre":"thriller",
"books":[
{
"title":"Book 1"
},
{
"title":"Book 2"
}
]
},
{
"genre":"mystery",
"books":[
{
"title":"Book 2"
},
{
"title":"Book 3"
}
]
}
]
},
{
"State":"New York",
"genres":[
{
"genre":"horror",
"books":[
{
"title":"Book 2"
},
{
"title":"Book 5"
}
]
},
{
"genre":"comedy",
"books":[
{
"title":"Book 6"
},
{
"title":"Book 7"
}
]
}
]
}
]
}
And I am using the jayway jsonpath library in Scala to parse it.
I could use something like JsonPath.read(myData,"$.libraries[*].genres[*].books[*]") to get an array of all the books pooled from every library. What I want is to know the path for each book, e.g. "$.libraries(0).genres(1).books(0)". Is there a ways to get an array of all the book paths rather than just all the books?
I am new to jsonpaths in general, so forgive me if this is not a good question.
You can use configuration with Option.AS_PATH_LIST:
val conf = Configuration.builder.options(Option.AS_PATH_LIST).build
val paths = JsonPath.parse(json, conf).read[JSONArray]("$.libraries[*].genres[*].books[*]")
for(path <- paths.toArray){
println(path.toString)
}
Which gives in case of your example json:
$['libraries'][0]['genres'][0]['books'][0]
$['libraries'][0]['genres'][0]['books'][1]
$['libraries'][0]['genres'][1]['books'][0]
$['libraries'][0]['genres'][1]['books'][1]
$['libraries'][1]['genres'][0]['books'][0]
$['libraries'][1]['genres'][0]['books'][1]
$['libraries'][1]['genres'][1]['books'][0]
$['libraries'][1]['genres'][1]['books'][1]
I tried searching for a JAVA library that I could use but to no avail.
Is there a gson/jackson/groovy library I could use to combine or merge together multiple JSON Strings into one payload?
Example :
JSON payload A, B and C
I would like both B and C to be added/merged to A.
Also removing any duplicated keys that are null or empty.
Example :
First JSON :
{
"businessUnitHierarchies":[
{
"actionType":"sample123",
"businessUnitHierarchy":{
"businessUnit":"sample123"
}
}
],
"description":{
"EN":"description sample",
"FR":"sample de description"
},
"name":{
"EN":"Coupon by a bot",
"FR":"Coupon par un bot"
},
"discountType":"Cost+",
"quantity":0,
"usageType":"shared",
"notes":"sample notes",
"discounts":[
{
"discountLevel":"SAMPLE",
"discountAmount":"10"
}
],
"couponId":0
}
Second JSON :
{
"effectiveDate":"2020-09-10",
"expiryDate":"2020-09-11",
"quantity":0,
"couponId":0
}
Third JSON
{
"productHierarchies":[
{
"productHierarchy":{
"level":7
},
"businessUnit":"fgl",
"actionType":"include",
"brand":"SAMPLE",
"discountAmount":"35"
}
],
"quantity":0,
"couponId":0
}
My desired output is :
Desired Output :
{
"businessUnitHierarchies":[
{
"actionType":"sample123",
"businessUnitHierarchy":{
"businessUnit":"sample123"
}
}
],
"description":{
"EN":"description sample",
"FR":"sample de description"
},
"name":{
"EN":"Coupon by a bot",
"FR":"Coupon par un bot"
},
"discountType":"Cost+",
"quantity":0,
"usageType":"shared",
"notes":"sample notes",
"discounts":[
{
"discountLevel":"SAMPLE",
"discountAmount":"10"
}
],
"couponId":0,
"effectiveDate":"2020-09-10",
"expiryDate":"2020-09-11",
"quantity":0,
"productHierarchies":[
{
"productHierarchy":{
"level":7
},
"businessUnit":"fgl",
"actionType":"include",
"brand":"SAMPLE",
"discountAmount":"35"
}
]
}
Wouldn't this be all you want? Based on Gson.
void merge(JsonObject dest, JsonObject src) {
for (var entry : src.entrySet()) {
dest.add(entry.getKey(), entry.getValue();
}
}
I started working with MongoDB. I prepared some basic training JSON:
{
"header": {
"Hotel": {
"data": [
{
"name": "Hilton",
"Id": "1231213421"
}
]
},
"standard": "5",
"priceStage": "4"
},
"data": {
"http": {
"strean": {}
}
}
}
and I wrote a query like this:
db.hotel.find( "data": { Id: "1231213421"})
Why query does not return anything?
You're trying to match on an element within an array and you don't need to match on the entire array. So,something like the following will work:
db.hotel.find({"header.Hotel.data": {"$elemMatch": {"Id": "1231213421"}}} );
I need to iterate and get the last values like name, url and color from below JSON response. Using java/gson api. Please help me on this.
{
"Title": {
"desc": [
{
"name": "PRE_DB",
"url": "http://jenkins.example.com/job/my_first_job/",
"color": "blue_anime"
},
{
"name": "SDD_Seller_Dashboard",
"url": "http://jenkins.example.com/job/my_second_job/",
"color": "blue_anime"
}
]
}
}
example output :
name : SDD_Seller_Dashboard
color :blue_anime
JSONObject data = new JSONObject(your_JSON_Repsonse);
JSONArray data_desc=data.getJSONArray(desc);
for(int i=0;i<=data_desc.length();i++)
{
name=data_desc.getString("name");
url=data_desc.getString("url");
color=data_desc.getString("color");
}