Below is the JSON feed:
{
"list" : {
"meta" : {
"type" : "resource-list",
"start" : 0,
"count" : 2
},
"resources" : [
{
"resource" : {
"classname" : "Quote",
"fields" : {
"name" : "USD/KRW",
"price" : "1151.295044",
"symbol" : "KRW=X",
"ts" : "1437357550",
"type" : "currency",
"utctime" : "2015-07-20T01:59:10+0000",
"volume" : "0"
}
}
}
,
{
"resource" : {
"classname" : "Quote",
"fields" : {
"name" : "SILVER 1 OZ 999 NY",
"price" : "0.067476",
"symbol" : "XAG=X",
"ts" : "1437169614",
"type" : "currency",
"utctime" : "2015-07-17T21:46:54+0000",
"volume" : "62"
}
}
}
,
{
"resource" : {
"classname" : "Quote",
"fields" : {
"name" : "USD/VND",
"price" : "21815.500000",
"symbol" : "VND=X",
"ts" : "1437357540",
"type" : "currency",
"utctime" : "2015-07-20T01:59:00+0000",
"volume" : "0"
}
}
}
]
}
}
How do I go about finding the "price" of the JSON object who's symbol is ("symbol" : "XAG=X") for example. In this case the answer is ("price" : "0.067476"). I need to perform this lookup programatically since the JSON is rather larger than the one presented here and the only parameter given to me will be the "symbol".
Is this possible? Any detailed help on how to do this would be greatly appreciated.
This is your Json object Format
Try this for getting correct result -
JSONObject list = new JSONObject(content).getJSONObject("list");
JSONArray resources = list.getJSONArray("resources");
for (int j = 0; j < resources.length(); j++) {
JSONObject resource = resources.getJSONObject(j).getJSONObject("resource");
JSONObject fields = resource.getJSONObject("fields");
if(fields.getString("symbol").equals("XAG=X")){
System.out.println("Price of symbol(XAG=X) is"+ fields.getString("price"));
}
}
Assuming content represents the json string
import org.json.JSONArray;
import org.json.JSONObject;
JSONObject list = new JSONObject(content).getJSONObject("list");
JSONArray resources = list.getJSONArray("resources");
for (int j = 0; j < resources.length(); j++) {
JSONObject resource = resources.getJSONObject(j).getJSONObject("resource");
JSONObject fields = resource.getJSONObject("fields");
System.out.println(fields.get("symbol"));
System.out.println(fields.get("price"));
}
Related
I have an String called inputJson that contains
{"listPruebas": [
{
"nombrePrueba" : "pruebaA",
"id" : 1,
"tipoPrueba" : "PRUEBABASE1",
"elementoBase" : "tipoA",
"listaMarca": [
{
"elemento": "elemento1 ",
"tipo": "ABC",
"cadena": "SFSG34235WF32"
},
{
"elemento":"elemento2",
"tipo":"DEF",
"cadena":"DJRT64353GSDG"
},
{
"elemento" : "elemento3",
"formato ":"JPG"
}
]},
{
"nombrePrueba" : "pruebaB",
"id" : 2,
"tipoPrueba" : "PRUEBABASE2",
"elementoBase" : "imagenPrueba",
"listaMarca2": [
{
"elemento" : "imagen",
"tipo": "tipo5",
"cadena": "iVBORw0KGgoAAAANSUhEUgAAAgAAAA"
}
]
}
],
"listaBuscar":
[
{
"tipoBusqueda":"busqueda1",
"id" : 1,
"operacion" : "operacion1",
"valor" : "12"
},
{
"tipoBusqueda":"binario",
"id" : 2,
"operacion" : "operacion2",
"valor" : "13"
},
{
"tipoFiltro":"numerico",
"id" : 31,
"operacion" : "MENOR_QUE",
"valor" : "1980",
"intervalo" : 1
}
]
}
and I converted the String to JSONObject of this way
JSONObject object = new JSONObject(inputJson);
and I got this
jsonObject::{"listaBuscar":[{"valor":"12","id":1,"operacion":"operacion1","tipoBusqueda":"busqueda1"},{"valor":"13","id":2,"operacion":"operacion2","tipoBusqueda":"binario"},{"tipoFiltro":"numerico","intervalo":1,"valor":"1980","id":31,"operacion":"MENOR_QUE"}],"listPruebas":[{"listaMarca":[{"tipo":"ABC","elemento":"elemento1","cadena":"SFSG34235WF32"},{"tipo":"DEF","elemento":"elemento2","cadena":"DJRT64353GSDG"},{"elemento":"elemento3","formato":"JPG"}],"elementoBase":"tipoA","tipoPrueba":"PRUEBABASE1","nombrePrueba":"pruebaA","id":1},{"elementoBase":"imagenPrueba","tipoPrueba":"PRUEBABASE2","listaMarca2":[{"tipo":"tipo5","elemento":"imagen","cadena":"iVBORw0KGgoAAAANSUhEUgAAAgAAAA"}],"nombrePrueba":"pruebaB","id":2}]}
and now I need to extract information but I dont know how to do, for example I try this
object.getString("elemento1");
but I got this error
Caused by: org.json.JONException: JSONObject["elemento1"] not found
help me please
You can't get a nest JSON object from the top level. It's like a treemap. You need to convert it into a java object or get it level by level.
check this post, a lot of ways.
You json contains two json arrays, fetch them as -
JSONArray listaBuscArray = jsonObj.getJSONArray("listaBuscar");
JSONArray listPruebasArray = jsonObj.getJSONArray("listPruebas");
Now you can process and use them as -
for(int i=0; i<listaBuscArray.length; i++){
JSONObject obj = listaBuscArray.getJSONObject(i);
.... your logic
}
i want to get from this JSONObject Data , the content of field "_source",
JSONObject jsonObj =
{
"hits" : [
{
"_index" : "try1",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"target" : {
"br_id" : 0,
"wo_id" : 2,
"process" : [
"element 1",
"element 2"
]
},
"explanation" : {
"an_id" : 1311,
"pa_name" : "micha"
},
"text" : "hello world"
}
}
]
}
the Result should be like bellow ,
String result =
{
"target" : {
"br_id" : 0,
"wo_id" : 2,
"process" : [
"element 1",
"element 2"
]
},
"explanation" : {
"an_id" : 1311,
"pa_name" : "micha"
},
"text" : "hello world"
}
I tried this , but it always could not recognize the "_source" field,
JSONObject main = jsonObj.getJSONObject("hits");
JSONObject content = main.getJSONObject("_source");
JSONObject field = content.getJSONObject("target");
JSONObject["_source"] not found
JSONObject["target"] not found
Please any suggestion or Advices, so i can get the Content from "_source" as Result?
thx.
_source is inside of JSONArray hits, so first get the JSONArray
JSONArray main = jsonObj.getJSONArray("hits");
And then get the first JSONObject from array
JSONObject obj = main.getJSONObject(0);
JSONObject source = obj.getJSONObject("_source"); //now get the _source
Elasticsearch Java client's QueryBuider instance is adding properties like
1) order
2) min_doc_count
3) shard_min_doc_count
4) show_term_doc_count_error
5) lang
6) gap_policy
to the final Json query. My query works as expected without those properties. I want to prevent those properties from being added to my final query.
Java:
FilterAggregationBuilder aggregation = AggregationBuilders.filter("id", QueryBuilders.termsQuery("id",
"my-name"));
TermsAggregationBuilder lev1Agg = AggregationBuilders.terms("id").field("id");
lev1Agg.size(1);
lev1Agg.subAggregation(AggregationBuilders.sum("familyMemberCount").field("membersInFamily"));
lev1Agg.subAggregation(AggregationBuilders.sum("totalKidsInFamily").field("kidsInFamily"));
Map<String, String> bucketsPathsMap = new HashMap<>();
bucketsPathsMap.put("familyMemberCount", "familyMemberCount");
bucketsPathsMap.put("totalKidsInFamily", "totalKidsInFamily");
Script script = new Script("params.familyMemberCount / params.totalKidsInFamily");
BucketScriptPipelineAggregationBuilder bs = PipelineAggregatorBuilders
.bucketScript("myScript", bucketsPathsMap, script);
lev1Agg.subAggregation(bs);
aggregation.subAggregation(lev1Agg);
searchSourceBuilder = new SearchSourceBuilder().aggregation(aggregation);
searchSourceBuilder.size(0);
Query built by above code
GET my-alias/_search
{
"size" : 0,
"aggregations" : {
"id" : {
"filter" : {
"terms" : {
"name" : [
"my-name"
],
"boost" : 1.0
}
},
"aggregations" : {
"id" : {
"terms" : {
"field" : "name",
"size" : 1,
"min_doc_count" : 1,
"shard_min_doc_count" : 0,
"show_term_doc_count_error" : false,
"order" : [
{
"_count" : "desc"
},
{
"_term" : "asc"
}
]
},
"aggregations" : {
"familyMemberCount" : {
"sum" : {
"field" : "membersInFamily"
}
},
"totalKidsInFamily" : {
"sum" : {
"field" : "kidsInFamily"
}
},
"myScript" : {
"bucket_script" : {
"buckets_path" : {
"familyMemberCount" : "familyMemberCount",
"totalKidsInFamily" : "totalKidsInFamily"
},
"script" : {
"source" : "params.familyMemberCount / params.totalKidsInFamily",
"lang" : "painless"
},
"gap_policy" : "skip"
}
}
}
}
}
}
}
}
I have written elasticsearch mapping its only only with alphabets. how to do the same for numeric values.
PUT /documents_test8
{
"settings" : {
"analysis" : {
"analyzer" : {
"filename_search" : {
"tokenizer" : "filename",
"filter" : ["lowercase"]
},
"filename_index" : {
"tokenizer" : "filename",
"filter" : ["lowercase","edge_ngram"]
}
},
"tokenizer" : {
"filename" : {
"pattern" : "[^\\p{L}\\d]+",
"type" : "pattern"
}
},
"filter" : {
"edge_ngram" : {
"side" : "front",
"max_gram" : 20,
"min_gram" : 1,
"type" : "edgeNGram"
}
}
}
},
"mappings" : {
"doc" : {
"properties" : {
"filename" : {
"type" : "text",
"search_analyzer" : "filename_search",
"index_analyzer" : "filename_index"
}
}
}
}
}
For numeric, you can define the mapping like this using type as "long"
"type": "long"
And for floating point number, use using type as "float"
"type": "float"
I need to find the record/count where eventDetails.eventDelivery.stateCode = "Y-FINISH". It could be in any element in the array list.
Just want to find where stateCode = "Y-FINISH" present in the array list.
Sample Data #1:
{
"_id" : ObjectId("5a2fb09736cf07f6a1146691"),
"activityID" : "",
"eventDetails" : [
{
"eventDelivery" : {
"digital" : {
"secureid" : "1231321212"
},
"stateCode" : "X-FINISH",
"state" : "SUCCESS"
},
{
"eventDelivery" : {
"digital" : {
"secureid" : "8762871121"
},
"stateCode" : "Y-FINISH",
"state" : "SUCCESS"
}
],
}
Sample Data #2:
{
"_id" : ObjectId("5a2fb09736cf07f6a1146691"),
"activityID" : "",
"eventDetails" : [
{
"eventDelivery" : {
"digital" : {
"secureid" : "1231321212"
},
"stateCode" : "X-FINISH",
"state" : "SUCCESS"
},
{
"eventDelivery" : {
"digital" : {
"secureid" : "8762871121"
},
"stateCode" : "Y-FINISH",
"state" : "SUCCESS"
},
{
"eventDelivery" : {
"digital" : {
"secureid" : "7651327152
},
"stateCode" : "Z-FINISH",
"state" : "SUCCESS"
}
],
}
Need to read this using Java.
Using a 3.x version of the Mongo Java driver ...
MongoClient mongoClient = ...;
MongoCollection<Document> collection = mongoClient.getDatabase("...")
.getCollection("...");
Bson filter = Filters.eq("eventDetails.stateCode", "Y-FINISH");
long count = collection.count(filter);