jmxtrans: Could not write results to opentsdb - java

I am using jmxtrans to collect JMX stats from a remote JVM machine and writing the output to Opentsdb using OpenTsdbWriter, but after starting the jmxtrans service i am able to see that its not able to write the results to Opentsdb.
I tried to find out the sollution over google but not getting the desired answer, could someone help me out to resolve it:-
Below is the stack trace of the exception from Logs:-
[06 Sep 2016 23:13:58] [jmxtrans-result-6] 21573814 WARN (com.googlecode.jmxtrans.jmx.ResultProcessor$1:60) - Could not write results [Result(attributeName=Usage, className=sun.management.MemoryPoolImpl, objDomain=java.lang, typeName=type=MemoryPool,name=Metaspace, values={init=0, committed=20185088, max=-1, used=19304024}, epoch=1473183838891, keyAlias=memorypool), Result(attributeName=Usage, className=sun.management.MemoryPoolImpl, objDomain=java.lang, typeName=type=MemoryPool,name=PS Old Gen, values={init=87031808, committed=87031808, max=1375731712, used=16885688}, epoch=1473183838901, keyAlias=memorypool), Result(attributeName=Usage, className=sun.management.MemoryPoolImpl, objDomain=java.lang, typeName=type=MemoryPool,name=PS Eden Space, values={init=32505856, committed=24117248, max=686817280, used=4032656}, epoch=1473183838903, keyAlias=memorypool), Result(attributeName=Usage, className=sun.management.MemoryPoolImpl, objDomain=java.lang, typeName=type=MemoryPool,name=Compressed Class Space, values={init=0, committed=2359296, max=1073741824, used=2134656}, epoch=1473183838904, keyAlias=memorypool), Result(attributeName=Usage, className=sun.management.MemoryPoolImpl, objDomain=java.lang, typeName=type=MemoryPool,name=Code Cache, values={init=2555904, committed=9633792, max=251658240, used=9103552}, epoch=1473183838905, keyAlias=memorypool), Result(attributeName=Usage, className=sun.management.MemoryPoolImpl, objDomain=java.lang, typeName=type=MemoryPool,name=PS Survivor Space, values={init=5242880, committed=524288, max=524288, used=327680}, epoch=1473183838906, keyAlias=memorypool)] of query Query(objectName=java.lang:type=MemoryPool,name=*, keys=[], attr=[Usage], typeNames=[], resultAlias=memorypool, useObjDomainAsKey=false, allowDottedKeys=false, useAllTypeNames=false, outputWriterInstances=[BaseOutputWriter(typeNames=[], debugEnabled=false, settings={host=10.143.1.43, port=4242}, valueTransformer=com.googlecode.jmxtrans.model.results.IdentityValueTransformer#35a68c72), BaseOutputWriter(typeNames=[], debugEnabled=false, settings={}, valueTransformer=com.googlecode.jmxtrans.model.results.IdentityValueTransformer#3b6164b6)]) to output writer BaseOutputWriter(typeNames=[], debugEnabled=false, settings={host=10.143.1.43, port=4242}, valueTransformer=com.googlecode.jmxtrans.model.results.IdentityValueTransformer#35a68c72)
java.lang.NullPointerException
at com.googlecode.jmxtrans.model.output.support.opentsdb.OpenTSDBMessageFormatter.processOneMetric(OpenTSDBMessageFormatter.java:196)
at com.googlecode.jmxtrans.model.output.support.opentsdb.OpenTSDBMessageFormatter.formatResult(OpenTSDBMessageFormatter.java:159)
at com.googlecode.jmxtrans.model.output.support.opentsdb.OpenTSDBMessageFormatter.access$000(OpenTSDBMessageFormatter.java:54)
at com.googlecode.jmxtrans.model.output.support.opentsdb.OpenTSDBMessageFormatter$1.apply(OpenTSDBMessageFormatter.java:170)
at com.googlecode.jmxtrans.model.output.support.opentsdb.OpenTSDBMessageFormatter$1.apply(OpenTSDBMessageFormatter.java:166)
at com.google.common.collect.Iterators$8.transform(Iterators.java:817)
at com.google.common.collect.TransformedIterator.next(TransformedIterator.java:48)
at com.google.common.collect.TransformedIterator.next(TransformedIterator.java:48)
at com.google.common.collect.Iterators$5.hasNext(Iterators.java:569)
at com.google.common.collect.ImmutableList.copyOf(ImmutableList.java:249)
at com.google.common.collect.ImmutableList.copyOf(ImmutableList.java:209)
at com.google.common.collect.FluentIterable.toList(FluentIterable.java:484)
at com.googlecode.jmxtrans.model.output.support.opentsdb.OpenTSDBMessageFormatter.formatResults(OpenTSDBMessageFormatter.java:173)
at com.googlecode.jmxtrans.model.output.OpenTSDBWriter.internalWrite(OpenTSDBWriter.java:116)
at com.googlecode.jmxtrans.model.output.BaseOutputWriter.doWrite(BaseOutputWriter.java:157)
at com.googlecode.jmxtrans.jmx.ResultProcessor$1.run(ResultProcessor.java:58)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
INFO | jvm 1 | 2016/09/06 23:13:58 | Result(attributeName=SystemLoadAverage, className=sun.management.OperatingSystemImpl, objDomain=java.lang, typeName=type=OperatingSystem, values={SystemLoadAverage=0.09}, epoch=1473183838891, keyAlias=null)
INFO | jvm 1 | 2016/09/06 23:13:58 | Result(attributeName=AvailableProcessors, className=sun.management.OperatingSystemImpl, objDomain=java.lang, typeName=type=OperatingSystem, values={AvailableProcessors=4}, epoch=1473183838891, keyAlias=null)
INFO | jvm 1 | 2016/09/06 23:13:58 | Result(attributeName=TotalPhysicalMemorySize, className=sun.management.OperatingSystemImpl, objDomain=java.lang, typeName=type=OperatingSystem, values={TotalPhysicalMemorySize=8252579840}, epoch=1473183838891, keyAlias=null)
INFO | jvm 1 | 2016/09/06 23:13:58 | Result(attributeName=FreePhysicalMemorySize, className=sun.management.OperatingSystemImpl, objDomain=java.lang, typeName=type=OperatingSystem, values={FreePhysicalMemorySize=170389504}, epoch=1473183838891, keyAlias=null)
INFO | jvm 1 | 2016/09/06 23:13:58 | Result(attributeName=TotalSwapSpaceSize, className=sun.management.OperatingSystemImpl, objDomain=java.lang, typeName=type=OperatingSystem, values={TotalSwapSpaceSize=1073737728}, epoch=1473183838891, keyAlias=null)
INFO | jvm 1 | 2016/09/06 23:13:58 | Result(attributeName=FreeSwapSpaceSize, className=sun.management.OperatingSystemImpl, objDomain=java.lang, typeName=type=OperatingSystem, values={FreeSwapSpaceSize=1073737728}, epoch=1473183838891, keyAlias=null)
INFO | jvm 1 | 2016/09/06 23:13:58 | Result(attributeName=OpenFileDescriptorCount, className=sun.management.OperatingSystemImpl, objDomain=java.lang, typeName=type=OperatingSystem, values={OpenFileDescriptorCount=83}, epoch=1473183838891, keyAlias=null)
INFO | jvm 1 | 2016/09/06 23:13:58 | Result(attributeName=MaxFileDescriptorCount, className=sun.management.OperatingSystemImpl, objDomain=java.lang, typeName=type=OperatingSystem, values={MaxFileDescriptorCount=50000}, epoch=1473183838891, keyAlias=null)
INFO | jvm 1 | 2016/09/06 23:13:58 | Result(attributeName=HeapMemoryUsage, className=sun.management.MemoryImpl, objDomain=java.lang, typeName=type=Memory, values={init=130023424, committed=111673344, max=1834483712, used=21039656}, epoch=1473183838891, keyAlias=heap)
INFO | jvm 1 | 2016/09/06 23:13:58 | Result(attributeName=NonHeapMemoryUsage, className=sun.management.MemoryImpl, objDomain=java.lang, typeName=type=Memory, values={init=2555904, committed=32178176, max=-1, used=30542232}, epoch=1473183838891, keyAlias=heap)
My Config.Json looks like the below:-
{
"servers": [
{
"numQueryThreads": "2",
"host": "xx.xx.xx.xx",
"port": "1099",
"queries": [
{
"outputWriters": [
{
"#class": "com.googlecode.jmxtrans.model.output.StdOutWriter"
},
{
"#class": "com.googlecode.jmxtrans.model.output.OpenTSDBWriter",
"settings": {
"host": "xx.xx.xx.xx",
"port": "4242"
}
}
],
"obj": "java.lang:type=OperatingSystem",
"attr": [
"SystemLoadAverage",
"AvailableProcessors",
"TotalPhysicalMemorySize",
"FreePhysicalMemorySize",
"TotalSwapSpaceSize",
"FreeSwapSpaceSize",
"OpenFileDescriptorCount",
"MaxFileDescriptorCount"
]
},
{
"outputWriters": [
{
"#class": "com.googlecode.jmxtrans.model.output.StdOutWriter"
},
{
"#class": "com.googlecode.jmxtrans.model.output.OpenTSDBWriter",
"settings": {
"host": "xx.xx.xx.xx",
"port": "4242"
}
}
],
"resultAlias": "heap",
"obj": "java.lang:type=Memory",
"attr": [
"HeapMemoryUsage",
"NonHeapMemoryUsage"
]
},
{
"outputWriters": [
{
"#class": "com.googlecode.jmxtrans.model.output.StdOutWriter"
},
{
"#class": "com.googlecode.jmxtrans.model.output.OpenTSDBWriter",
"settings": {
"host": "xx.xx.xx.xx",
"port": "4242"
}
}
],
"resultAlias": "cmsoldgen",
"obj": "java.lang:name=CMS Old Gen,type=MemoryPool",
"attr": [
"Usage"
]
},
{
"outputWriters": [
{
"#class": "com.googlecode.jmxtrans.model.output.StdOutWriter"
},
{
"#class": "com.googlecode.jmxtrans.model.output.OpenTSDBWriter",
"settings": {
"host": "xx.xx.xx.xx",
"port": "4242"
}
}
],
"resultAlias": "gc",
"obj": "java.lang:type=GarbageCollector,name=",
"attr": [
"CollectionCount",
"CollectionTime"
]
},
{
"outputWriters": [
{
"#class": "com.googlecode.jmxtrans.model.output.StdOutWriter",
"settings": {}
},
{
"#class": "com.googlecode.jmxtrans.model.output.OpenTSDBWriter",
"settings": {
"host": "xx.xx.xx.xx",
"port": "4242"
}
}
],
"resultAlias": "memorypool",
"obj": "java.lang:type=MemoryPool,name=",
"attr": [
"Usage"
]
},
{
"outputWriters": [
{
"#class": "com.googlecode.jmxtrans.model.output.StdOutWriter"
},
{
"#class": "com.googlecode.jmxtrans.model.output.OpenTSDBWriter",
"settings": {
"host": "xx.xx.xx.xx",
"port": "4242"
}
}
],
"resultAlias": "threads",
"obj": "java.lang:type=Threading",
"attr": [
"DaemonThreadCount",
"PeakThreadCount",
"ThreadCount",
"TotalStartedThreadCount"
]
},
{
"outputWriters": [
{
"#class": "com.googlecode.jmxtrans.model.output.StdOutWriter"
},
{
"#class": "com.googlecode.jmxtrans.model.output.OpenTSDBWriter",
"settings": {
"host": "xx.xx.xx.xx",
"port": "4242"
}
}
],
"resultAlias": "tomcat8-connectors",
"obj": "Catalina:type=ThreadPool,name=",
"attr": [
"currentThreadCount",
"currentThreadsBusy"
]
},
{
"outputWriters": [
{
"#class": "com.googlecode.jmxtrans.model.output.StdOutWriter"
},
{
"#class": "com.googlecode.jmxtrans.model.output.OpenTSDBWriter",
"settings": {
"host": "xx.xx.xx.xx",
"port": "4242"
}
}
],
"resultAlias": "tomcat8-requests",
"obj": "Catalina:type=GlobalRequestProcessor,name=",
"attr": [
"bytesReceived",
"bytesSent",
"errorCount",
"processingTime",
"requestCount"
]
}
]
}
]
}

This is a bug in jmxtrans (https://github.com/jmxtrans/jmxtrans/issues/487) triggered when the typeNames settings hasn't been set.
Add "typeNames": [] to your OpenTSDBWriter settings as a workaround. You'll need to add it to all of the relevant sections.

Related

Optional Properties json schema

the items are only required for the order_type: ORDER, but I can't make a conditional that only requires this property for that specific case, that is, if any order_type different from ORDER comes without items it can be validated correctly, but if an ORDER comes without them I mark it incorrectly.
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "http://json-schema.org/draft-07/schema#",
"title": "Validaciones sobre el esquema Order",
"type": "object",
"properties": {
"warehouse_id": {
"type": [
"string"
]
},
"operation_type": {
"type": [
"string"
]
},
"order_id": {
"type": [
"number"
]
},
"items": {
"type": [
"array"
],
"minItems": 1,
"items": {
"type": [
"object"
],
"properties": {
"sku": {
"type": [
"string"
],
"minLength": 1,
"maxLength": 50
},
"quantity": {
"type": [
"integer"
],
"minimum": 1
}
},
"required": [
"sku",
"quantity"
],
"additionalProperties": false
}
},
"attributes": {
"type": [
"object"
],
"properties": {
"order_type": {
"type": [
"string"
],
"enum": [
"ORDER",
"TRANSFER",
"WITHDRAWAL",
"DISPOSAL",
"FRESH"
]
},
"etd": {
"type": [
"string"
],
"pattern": "^(\\d{4})-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])(?:T)(0[0-9]|1[0-9]|2[0-3]):(0[0-9]|1[0-9]|2[0-9]|3[0-9]|4[0-9]|5[0-9]):(0[0-9]|1[0-9]|2[0-9]|3[0-9]|4[0-9]|5[0-9])(?:Z)$"
},
"volume": {
"type": [
"integer", "null"
],
"minimum": 0
},
"typology": true
},
"required": [
"order_type"
],
"allOf": [
{
"if": {
"properties": {
"order_type": {
"const": "ORDER"
}
},
"required": [
"order_type",
"items"
]
},
"then": {
"properties": {
"order_type": true,
"etd": true,
"volume": true,
"typology": {
"type": [
"string", "null"
],
"enum": [
"CONVEYABLE",
"NON_CONVEYABLE"
]
}
},
"required": [
"order_type",
"etd"
],
"additionalProperties": false
}
},
{
"if": {
"properties": {
"order_type": {
"const": "TRANSFER"
}
},
"required": [
"order_type"
]
},
"then": {
"properties": {
"order_type": true,
"etd": true,
"volume": true
},
"required": [
"order_type",
"etd"
],
"additionalProperties": false
}
},
{
"if": {
"properties": {
"order_type": {
"const": "WITHDRAWAL"
}
},
"required": [
"order_type"
]
},
"then": {
"properties": {
"order_type": true,
"etd": true,
"volume": true
},
"required": [
"order_type",
"etd"
],
"additionalProperties": false
}
},
{
"if": {
"properties": {
"order_type": {
"const": "DISPOSAL"
}
},
"required": [
"order_type"
]
},
"then": {
"properties": {
"order_type": true,
"etd": true,
"volume": true
},
"required": [
"order_type",
"etd"
],
"additionalProperties": false
}
}
]
}
},
"required": [
"warehouse_id",
"operation_type",
"attributes"
],
"additionalProperties": false
}
try to make a conditional with an if at the end of the properties like this
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "http://json-schema.org/draft-07/schema#",
"title": "Validaciones sobre el esquema Order",
"type": "object",
"properties": {
"warehouse_id": {
"type": [
"string"
]
},
"operation_type": {
"type": [
"string"
]
},
"order_id": {
"type": [
"number"
]
},
"items": {
"type": [
"array"
],
"minItems": 1,
"items": {
"type": [
"object"
],
"properties": {
"sku": {
"type": [
"string"
],
"minLength": 1,
"maxLength": 50
},
"quantity": {
"type": [
"integer"
],
"minimum": 1
}
},
"required": [
"sku",
"quantity"
],
"additionalProperties": false
}
},
"attributes": {
"type": [
"object"
],
"properties": {
"order_type": {
"type": [
"string"
],
"enum": [
"ORDER",
"TRANSFER",
"WITHDRAWAL",
"DISPOSAL",
"FRESH"
]
},
"etd": {
"type": [
"string"
],
"pattern": "^(\\d{4})-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])(?:T)(0[0-9]|1[0-9]|2[0-3]):(0[0-9]|1[0-9]|2[0-9]|3[0-9]|4[0-9]|5[0-9]):(0[0-9]|1[0-9]|2[0-9]|3[0-9]|4[0-9]|5[0-9])(?:Z)$"
},
"volume": {
"type": [
"integer", "null"
],
"minimum": 0
},
"typology": true
},
"required": [
"order_type"
],
"allOf": [
{
"if": {
"properties": {
"order_type": {
"const": "ORDER"
}
},
"required": [
"order_type",
"items"
]
},
"then": {
"properties": {
"order_type": true,
"etd": true,
"volume": true,
"typology": {
"type": [
"string", "null"
],
"enum": [
"CONVEYABLE",
"NON_CONVEYABLE"
]
}
},
"required": [
"order_type",
"etd"
],
"additionalProperties": false
}
},
{
"if": {
"properties": {
"order_type": {
"const": "TRANSFER"
}
},
"required": [
"order_type"
]
},
"then": {
"properties": {
"order_type": true,
"etd": true,
"volume": true
},
"required": [
"order_type",
"etd"
],
"additionalProperties": false
}
},
{
"if": {
"properties": {
"order_type": {
"const": "WITHDRAWAL"
}
},
"required": [
"order_type"
]
},
"then": {
"properties": {
"order_type": true,
"etd": true,
"volume": true
},
"required": [
"order_type",
"etd"
],
"additionalProperties": false
}
},
{
"if": {
"properties": {
"order_type": {
"const": "DISPOSAL"
}
},
"required": [
"order_type"
]
},
"then": {
"properties": {
"order_type": true,
"etd": true,
"volume": true
},
"required": [
"order_type",
"etd"
],
"additionalProperties": false
}
}
]
}
},
"allOf": [
{
"if": {
"properties": {
"order_type": {
"const": "ORDER"
}
},
"required": [
"order_type"
]
},
"then": {
"properties": {
"items": true,
"order_type": true,
"etd": true,
"volume": true
},
"required": [
"order_type",
"etd",
"items"
],
"additionalProperties": false
}
}
],
"required": [
"warehouse_id",
"operation_type",
"attributes"
],
"additionalProperties": false
}
this request should be marked as incorrect, cause dont have any items, that are required:
{
"warehouse_id": "ARTW01",
"operation_type": "outbound",
"order_id": 41789301078,
"attributes": {
"volume": 1350,
"etd": "2022-11-11T18:25:00Z",
"order_type": "ORDER"
}
}
Here's the assertion you described with all the unrelated parts removed. The trick is that the if need to describe the nested path to the value being checked.
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"attributes": {
"type": "object",
"properties": {
"order_type": { "enum": ["ORDER", "TRANSFER", "WITHDRAWAL", "DISPOSAL", "FRESH"] }
},
"required": ["order_type"]
},
"items": { "type": "array" }
},
"allOf": [
{
"if": {
"type": "object",
"properties": {
"attributes": {
"type": "object",
"properties": {
"order_type": { "const": "ORDER" }
},
"required": ["order_type"]
}
},
"required": ["attributes"]
},
"then": { "required": ["items"] }
}
]
}

How to get Max and Min Value in MongoDB based on specific key?

I want to get the max and min value of the emp salary based on currency. every employee has a salary range based on currency also all the details in response should be unique. while I am using aggregation function min and max but it fetches the max and min value of salary amount but I need to get max and min based on currency field.
Sample Data:
[
{
"id": "1",
"emp_name": "emp1",
"data": [
{
"emp_country": "country1",
"emp_city": "city1",
"salary": [
{
"currency": "INR",
"amount": 5000
},
{
"currency": "INR",
"amount": 600
},
{
"currency": "MXN",
"amount": 400
}
]
},
{
"emp_country": "country1",
"emp_city": "city2",
"salary": [
{
"currency": "DOLLER",
"amount": 5000
},
{
"currency": "DOLLER",
"amount": 200
},
{
"currency": "MXN",
"amount": 400
}
]
}
]
},
{
"id": "2",
"emp_name": "emp2",
"data": [
{
"emp_country": "country2",
"emp_city": "city2",
"salary": [
{
"currency": "INR",
"amount": 5000
},
{
"currency": "MXN",
"amount": 200
},
{
"currency": "MXN",
"amount": 400
}
]
}
]
},
{
"id": "3",
"emp_name": "emp3",
"data": [
{
"emp_country": "country1",
"emp_city": "city1",
"salary": [
{
"currency": "MXN",
"amount": 400
}
]
}
]
},
{
"id": "4",
"emp_name": "emp4",
"data": [
{
"emp_country": "country1",
"emp_city": "city2",
"salary": [
{
"currency": "DOLLER",
"amount": 200
}
]
}
]
}
]
Expected Output:
city, country, the name should be unique and salary have max and min based on currency.
[
{
"emp_city": "city1",
"emp_country": "country1",
"emp_name": "emp1",
"emp_salary": [{
"currency": "INR",
"max": 5000,
"min": 600
},
{
"currency": "MXN",
"max": 400,
"min": 400
}]
},
{
"emp_city": "city2",
"emp_country": "country1",
"emp_name": "emp1",
"emp_salary":[ {
"currency": "DOLLER",
"max": 5000,
"min": 200
},
{
"currency": "MXN",
"max": 400,
"min": 400
}]
},
{
"emp_city": "city2",
"emp_country": "country2",
"emp_name": "emp2",
"emp_salary": [{
"currency": "INR",
"max": 5000,
"min": 5000
},
{
"currency": "MXN",
"max": 400,
"min": 200
}]
},
{
"emp_city": "city1",
"emp_country": "country1",
"emp_name": "emp3",
"emp_salary": [{
"currency": "MXN",
"max": 400,
"min": 400
}]
},
{
"emp_city": "city2",
"emp_country": "country1",
"emp_name": "emp4",
"emp_salary": [{
"currency": "DOLLER",
"max": 200,
"min": 200
}]
}
]
We can achieve the above result using an aggregation query.
$unwind - to deconstruct array of salary
$group - group by emp city, country, name, currency with min and max value of salary
$group - use another to push in emp_salary
$project - to show fields in your format
db.collection.aggregate([
{
"$unwind": {
"path": "$data"
}
},
{
"$unwind": {
"path": "$data.salary"
}
},
{
"$group": {
"_id": {
"emp_city": "$data.emp_city",
"emp_country": "$data.emp_country",
"emp_name": "$emp_name",
"currency": "$data.salary.currency",
},
"max": {
"$max": "$data.salary.amount"
},
"min": {
"$min": "$data.salary.amount"
}
}
},
{
"$group": {
"_id": {
"emp_city": "$_id.emp_city",
"emp_country": "$_id.emp_country",
"emp_name": "$_id.emp_name",
},
"emp_salary": {
"$push": {
"currency": "$_id.currency",
"min": "$min",
"max": "$max"
}
}
}
},
{
"$project": {
"_id": 0,
"emp_city": "$_id.emp_city",
"emp_country": "$_id.emp_country",
"emp_name": "$_id.emp_name",
"emp_salary": 1,
}
}
])
Mongo Playground

Elasticsearch sorting - not getting expected results

I have data in elastic field name as "Amit 111", "amit 111", "Amit 222".
I am trying to sort it using:
searchSourceBuilder.query(query).sort("name.keyword", SortOrder.ASC)
It returns result as:
"Amit 111", "Amit 222", "amit 111"
But I want results as :
"Amit 111", "amit 111", "Amit 222"
Please help.
Another approach is to use the fielddata as on text field you can apply the sort, more details on the linked URL.
Java code for that you need to change the index mapping as shown after java code.
searchSourceBuilder.query(query).sort("name", SortOrder.ASC)
Create index with field data enabled on the name field
{
"mappings": {
"properties": {
"name": {
"type": "text",
"fielddata": true
}
}
}
}
Index example documents
{
"name" : "amit 111"
}
{
"name" : "Amit 111"
}
{
"name" : "Amit 222"
}
Your search query with sort on name field
{
"sort": [
{
"name": "asc"
}
]
}
Result
"hits": [
{
"_index": "key",
"_type": "_doc",
"_id": "1",
"_score": null,
"_source": {
"name": "amit 111"
},
"sort": [
"111"
]
},
{
"_index": "key",
"_type": "_doc",
"_id": "2",
"_score": null,
"_source": {
"name": "Amit 111"
},
"sort": [
"111"
]
},
{
"_index": "key",
"_type": "_doc",
"_id": "3",
"_score": null,
"_source": {
"name": "Amit 222"
},
"sort": [
"222"
]
}
]
keyword fields are stored as it is so sorting on keyword fields is case sensitive.Normalizer with lowercase filter can be used to index keyword fields.
The normalizer property of keyword fields is similar to analyzer
except that it guarantees that the analysis chain produces a single
token.
Mapping:
{
"settings": {
"analysis": {
"normalizer": {
"my_normalizer": {
"type": "custom",
"filter": [
"lowercase"
]
}
}
}
},
"mappings": {
"properties": {
"name": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"normalizer": "my_normalizer"
}
}
}
}
}
}
Query: Both sort on name.keyword and term query on name.keyword will be case insensitive
{
"query": {
"match_all": {}
},
"sort": [
{
"name.keyword": {
"order": "asc"
}
}
]
}
Result:"
"hits" : [
{
"_index" : "index84",
"_type" : "_doc",
"_id" : "SBvLT3IB8mx5yKbJQ7EC",
"_score" : null,
"_source" : {
"name" : "Amit 111"
},
"sort" : [
"amit 111"
]
},
{
"_index" : "index84",
"_type" : "_doc",
"_id" : "SRvLT3IB8mx5yKbJULFl",
"_score" : null,
"_source" : {
"name" : "amit 111"
},
"sort" : [
"amit 111"
]
},
{
"_index" : "index84",
"_type" : "_doc",
"_id" : "ShvLT3IB8mx5yKbJaLFg",
"_score" : null,
"_source" : {
"name" : "Amit 222"
},
"sort" : [
"amit 222"
]
}
]

Azure, java sdk, Deploying with ARM template - failure to convert Parameter file

I'm trying to write a java JUnit test that will deploy template-defined resource groups using the java-sdk for azure.
I came across a couple examples and followed them to create this code segment:
public void azurePoC() throws Exception {
Azure azure = Azure.configure()
.withLogLevel(LogLevel.BODY)
.authenticate(new File("C:/somePath/credentials.azure"))
.withDefaultSubscription();
final ObjectMapper mapper = new ObjectMapper();
JsonNode template = mapper.readTree(new File("C:/somePath/template.json"));
Logger.report("template", template.toString());
JsonNode params = mapper.readTree(new File("C:/somePath/parameters.json"));
Logger.report("params", params.toString());
azure.deployments()
.define("myNewResource")
.withNewResourceGroup("myNewResourceGroup", Region.US_WEST2)
.withTemplate(template)
.withParameters(params)
.withMode(DeploymentMode.INCREMENTAL)
.create();
Logger.report("Request for deployment done.");
}
I have verified that the API allows me to lookup resources using azure.virtualMachines().list() method, therefore i conclude that my credentials are okay.
I am using one of the quick-start templates to for my PoC, just to see if i can deploy some resource.
My template.json file:
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"adminUsername": {
"type": "string",
"metadata": {
"description": "User name for the Virtual Machine."
}
},
"adminPassword": {
"type": "securestring",
"metadata": {
"description": "Password for the Virtual Machine."
}
},
"dnsLabelPrefix": {
"type": "string",
"metadata": {
"description": "Unique DNS Name for the Public IP used to access the Virtual Machine."
}
},
"ubuntuOSVersion": {
"type": "string",
"defaultValue": "16.04.0-LTS",
"allowedValues": [
"12.04.5-LTS",
"14.04.5-LTS",
"15.10",
"16.04.0-LTS"
],
"metadata": {
"description": "The Ubuntu version for the VM. This will pick a fully patched image of this given Ubuntu version."
}
},
"location": {
"type": "string",
"defaultValue": "[resourceGroup().location]",
"metadata": {
"description": "Location for all resources."
}
}
},
"variables": {
"storageAccountName": "[concat(uniquestring(resourceGroup().id), 'salinuxvm')]",
"imagePublisher": "Canonical",
"imageOffer": "UbuntuServer",
"nicName": "myVMNic",
"addressPrefix": "10.0.0.0/16",
"subnetName": "Subnet",
"subnetPrefix": "10.0.0.0/24",
"storageAccountType": "Standard_LRS",
"publicIPAddressName": "myPublicIP",
"publicIPAddressType": "Dynamic",
"vmName": "MyUbuntuVM",
"vmSize": "Standard_A1",
"virtualNetworkName": "MyVNET",
"subnetRef": "[resourceId('Microsoft.Network/virtualNetworks/subnets', variables('virtualNetworkName'), variables('subnetName'))]"
},
"resources": [
{
"type": "Microsoft.Storage/storageAccounts",
"name": "[variables('storageAccountName')]",
"apiVersion": "2018-07-01",
"location": "[parameters('location')]",
"sku": {
"name": "[variables('storageAccountType')]"
},
"kind": "Storage",
"properties": {}
},
{
"apiVersion": "2018-10-01",
"type": "Microsoft.Network/publicIPAddresses",
"name": "[variables('publicIPAddressName')]",
"location": "[parameters('location')]",
"properties": {
"publicIPAllocationMethod": "[variables('publicIPAddressType')]",
"dnsSettings": {
"domainNameLabel": "[parameters('dnsLabelPrefix')]"
}
}
},
{
"apiVersion": "2018-10-01",
"type": "Microsoft.Network/virtualNetworks",
"name": "[variables('virtualNetworkName')]",
"location": "[parameters('location')]",
"properties": {
"addressSpace": {
"addressPrefixes": [
"[variables('addressPrefix')]"
]
},
"subnets": [
{
"name": "[variables('subnetName')]",
"properties": {
"addressPrefix": "[variables('subnetPrefix')]"
}
}
]
}
},
{
"apiVersion": "2018-10-01",
"type": "Microsoft.Network/networkInterfaces",
"name": "[variables('nicName')]",
"location": "[parameters('location')]",
"dependsOn": [
"[resourceId('Microsoft.Network/publicIPAddresses/', variables('publicIPAddressName'))]",
"[resourceId('Microsoft.Network/virtualNetworks/', variables('virtualNetworkName'))]"
],
"properties": {
"ipConfigurations": [
{
"name": "ipconfig1",
"properties": {
"privateIPAllocationMethod": "Dynamic",
"publicIPAddress": {
"id": "[resourceId('Microsoft.Network/publicIPAddresses',variables('publicIPAddressName'))]"
},
"subnet": {
"id": "[variables('subnetRef')]"
}
}
}
]
}
},
{
"apiVersion": "2018-10-01",
"type": "Microsoft.Compute/virtualMachines",
"name": "[variables('vmName')]",
"location": "[parameters('location')]",
"dependsOn": [
"[resourceId('Microsoft.Storage/storageAccounts/', variables('storageAccountName'))]",
"[resourceId('Microsoft.Network/networkInterfaces/', variables('nicName'))]"
],
"properties": {
"hardwareProfile": {
"vmSize": "[variables('vmSize')]"
},
"osProfile": {
"computerName": "[variables('vmName')]",
"adminUsername": "[parameters('adminUsername')]",
"adminPassword": "[parameters('adminPassword')]"
},
"storageProfile": {
"imageReference": {
"publisher": "[variables('imagePublisher')]",
"offer": "[variables('imageOffer')]",
"sku": "[parameters('ubuntuOSVersion')]",
"version": "latest"
},
"osDisk": {
"createOption": "FromImage"
},
"dataDisks": [
{
"diskSizeGB": 1023,
"lun": 0,
"createOption": "Empty"
}
]
},
"networkProfile": {
"networkInterfaces": [
{
"id": "[resourceId('Microsoft.Network/networkInterfaces',variables('nicName'))]"
}
]
},
"diagnosticsProfile": {
"bootDiagnostics": {
"enabled": true,
"storageUri": "[concat(reference(concat('Microsoft.Storage/storageAccounts/', variables('storageAccountName')), '2016-01-01').primaryEndpoints.blob)]"
}
}
}
}
],
"outputs": {
"hostname": {
"type": "string",
"value": "[reference(variables('publicIPAddressName')).dnsSettings.fqdn]"
},
"sshCommand": {
"type": "string",
"value": "[concat('ssh ', parameters('adminUsername'), '#', reference(variables('publicIPAddressName')).dnsSettings.fqdn)]"
}
}
}
My parameters.json file:
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"adminUsername": {
"value": "ghuser"
},
"adminPassword": {
"value": "GEN-PASSWORD"
},
"dnsLabelPrefix": {
"value": "GEN-UNIQUE"
}
}
}
Whenever I run the application I am confronted with this exception:
com.microsoft.azure.CloudException: The request content was invalid and could not be deserialized: 'Error converting value "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#" to type 'Microsoft.WindowsAzure.ResourceStack.Frontdoor.Data.Definitions.DeploymentParameterDefinition'. Path 'properties.parameters.$schema', line 1, position 4459.'.: The request content was invalid and could not be deserialized: 'Error converting value "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#" to type 'Microsoft.WindowsAzure.ResourceStack.Frontdoor.Data.Definitions.DeploymentParameterDefinition'. Path 'properties.parameters.$schema', line 1, position 4459.'.
at com.microsoft.azure.AzureClient.createExceptionFromResponse(AzureClient.java:740)
at com.microsoft.azure.AzureClient.access$100(AzureClient.java:33)
at com.microsoft.azure.AzureClient$3.call(AzureClient.java:160)
at com.microsoft.azure.AzureClient$3.call(AzureClient.java:157)
at rx.internal.operators.OnSubscribeMap$MapSubscriber.onNext(OnSubscribeMap.java:69)
at retrofit2.adapter.rxjava.CallArbiter.deliverResponse(CallArbiter.java:120)
at retrofit2.adapter.rxjava.CallArbiter.emitResponse(CallArbiter.java:102)
at retrofit2.adapter.rxjava.CallExecuteOnSubscribe.call(CallExecuteOnSubscribe.java:46)
at retrofit2.adapter.rxjava.CallExecuteOnSubscribe.call(CallExecuteOnSubscribe.java:24)
at rx.Observable.unsafeSubscribe(Observable.java:10327)
at rx.internal.operators.OnSubscribeMap.call(OnSubscribeMap.java:48)
at rx.internal.operators.OnSubscribeMap.call(OnSubscribeMap.java:33)
at rx.Observable.unsafeSubscribe(Observable.java:10327)
at rx.internal.operators.OnSubscribeSingle.call(OnSubscribeSingle.java:81)
at rx.internal.operators.OnSubscribeSingle.call(OnSubscribeSingle.java:27)
at rx.internal.operators.SingleToObservable.call(SingleToObservable.java:39)
at rx.internal.operators.SingleToObservable.call(SingleToObservable.java:27)
at rx.Observable.unsafeSubscribe(Observable.java:10327)
at rx.internal.operators.OnSubscribeMap.call(OnSubscribeMap.java:48)
at rx.internal.operators.OnSubscribeMap.call(OnSubscribeMap.java:33)
at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:48)
at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30)
at rx.Observable.unsafeSubscribe(Observable.java:10327)
at rx.internal.operators.DeferredScalarSubscriber.subscribeTo(DeferredScalarSubscriber.java:153)
at rx.internal.operators.OnSubscribeTakeLastOne.call(OnSubscribeTakeLastOne.java:32)
at rx.internal.operators.OnSubscribeTakeLastOne.call(OnSubscribeTakeLastOne.java:22)
at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:48)
at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30)
at rx.Observable.unsafeSubscribe(Observable.java:10327)
at rx.internal.operators.OnSubscribeMap.call(OnSubscribeMap.java:48)
at rx.internal.operators.OnSubscribeMap.call(OnSubscribeMap.java:33)
at rx.Observable.unsafeSubscribe(Observable.java:10327)
at rx.internal.operators.OnSubscribeMap.call(OnSubscribeMap.java:48)
at rx.internal.operators.OnSubscribeMap.call(OnSubscribeMap.java:33)
at rx.Observable.unsafeSubscribe(Observable.java:10327)
at rx.internal.operators.OnSubscribeMap.call(OnSubscribeMap.java:48)
at rx.internal.operators.OnSubscribeMap.call(OnSubscribeMap.java:33)
at rx.Observable.unsafeSubscribe(Observable.java:10327)
at rx.internal.operators.OperatorSubscribeOn$SubscribeOnSubscriber.call(OperatorSubscribeOn.java:100)
at rx.internal.schedulers.CachedThreadScheduler$EventLoopWorker$1.call(CachedThreadScheduler.java:230)
at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:55)
at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(Unknown Source)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: rx.exceptions.OnErrorThrowable$OnNextValue: OnError while emitting onNext value: retrofit2.Response.class
at rx.exceptions.OnErrorThrowable.addValueAsLastCause(OnErrorThrowable.java:118)
at rx.internal.operators.OnSubscribeMap$MapSubscriber.onNext(OnSubscribeMap.java:73)
... 43 more
Any and all help in the matter will be greatly appreciated.
For anyone who comes across this question.
Turns out the problem was the parameter file json syntax.
The schema, parameters and version fields are redundant
this is the expected syntax using Azure Java SDK:
{
"adminUsername": {
"value": "ghuser"
},
"adminPassword": {
"value": "GEN-PASSWORD"
},
"dnsLabelPrefix": {
"value": "GEN-UNIQUE"
}
}

get single value from json string

i have Json string like this where the function return List:
[
{
"name": "DEFAULT",
"effectiveDate": "Apr 19, 2018 3:35:31 PM",
"currencies": [
"IDR"
],
"products": [
{
"type": "BASE",
"name": "PostPaid",
"plans": [
{
"name": "postpaid-monthly-fix",
"billingPeriod": "MONTHLY",
"phases": [
{
"type": "EVERGREEN",
"prices": [
{
"currency": "IDR",
"value": 300000
}
],
"fixedPrices": [],
"duration": {
"unit": "UNLIMITED",
"number": -1
},
"usages": []
}
]
},
{
"name": "postpaid-monthly-trial1",
"billingPeriod": "MONTHLY",
"phases": [
{
"type": "TRIAL",
"prices": [],
"fixedPrices": [
{
"currency": "IDR",
"value": 0
}
],
"duration": {
"unit": "DAYS",
"number": 1
},
"usages": []
},
{
"type": "EVERGREEN",
"prices": [
{
"currency": "IDR",
"value": 250000
}
],
"fixedPrices": [],
"duration": {
"unit": "UNLIMITED",
"number": -1
},
"usages": []
}
]
},
{
"name": "postpaid-monthly-trial7",
"billingPeriod": "MONTHLY",
"phases": [
{
"type": "TRIAL",
"prices": [],
"fixedPrices": [
{
"currency": "IDR",
"value": 0
}
],
"duration": {
"unit": "DAYS",
"number": 7
},
"usages": []
},
{
"type": "EVERGREEN",
"prices": [
{
"currency": "IDR",
"value": 350000
}
],
"fixedPrices": [],
"duration": {
"unit": "UNLIMITED",
"number": -1
},
"usages": []
}
]
}
],
"included": [],
"available": []
},
{
"type": "BASE",
"name": "PrePaid",
"plans": [
{
"name": "prepaid-signature-item",
"billingPeriod": "NO_BILLING_PERIOD",
"phases": [
{
"type": "EVERGREEN",
"prices": [],
"fixedPrices": [],
"duration": {
"unit": "UNLIMITED",
"number": -1
},
"usages": [
{
"billingPeriod": "DAILY",
"tiers": [
{
"blocks": [
{
"unit": "item",
"size": "1.0",
"max": "5.0",
"prices": [
{
"currency": "IDR",
"value": 250000
}
]
}
],
"limits": [],
"fixedPrice": [],
"recurringPrice": []
}
]
}
]
}
]
}
],
"included": [],
"available": []
}
],
"priceLists": [
{
"name": "DEFAULT",
"plans": [
"postpaid-monthly-fix",
"postpaid-monthly-trial1",
"postpaid-monthly-trial7",
"prepaid-signature-item"
]
}
]
},
{
"name": "DEFAULT",
"effectiveDate": "Apr 20, 2018 3:35:31 PM",
"currencies": [
"IDR"
],
"products": [
{
"type": "BASE",
"name": "PostPaid",
"plans": [
{
"name": "postpaid-monthly-fix",
"billingPeriod": "MONTHLY",
"phases": [
{
"type": "EVERGREEN",
"prices": [
{
"currency": "IDR",
"value": 300000
}
],
"fixedPrices": [],
"duration": {
"unit": "UNLIMITED",
"number": -1
},
"usages": []
}
]
},
{
"name": "postpaid-monthly-trial1",
"billingPeriod": "MONTHLY",
"phases": [
{
"type": "TRIAL",
"prices": [],
"fixedPrices": [
{
"currency": "IDR",
"value": 0
}
],
"duration": {
"unit": "DAYS",
"number": 1
},
"usages": []
},
{
"type": "EVERGREEN",
"prices": [
{
"currency": "IDR",
"value": 250000
}
],
"fixedPrices": [],
"duration": {
"unit": "UNLIMITED",
"number": -1
},
"usages": []
}
]
},
{
"name": "postpaid-monthly-trial7",
"billingPeriod": "MONTHLY",
"phases": [
{
"type": "TRIAL",
"prices": [],
"fixedPrices": [
{
"currency": "IDR",
"value": 0
}
],
"duration": {
"unit": "DAYS",
"number": 7
},
"usages": []
},
{
"type": "EVERGREEN",
"prices": [
{
"currency": "IDR",
"value": 350000
}
],
"fixedPrices": [],
"duration": {
"unit": "UNLIMITED",
"number": -1
},
"usages": []
}
]
}
],
"included": [],
"available": []
},
{
"type": "BASE",
"name": "PrePaid",
"plans": [
{
"name": "prepaid-signature-item",
"billingPeriod": "NO_BILLING_PERIOD",
"phases": [
{
"type": "EVERGREEN",
"prices": [],
"fixedPrices": [],
"duration": {
"unit": "UNLIMITED",
"number": -1
},
"usages": [
{
"billingPeriod": "NO_BILLING_PERIOD",
"tiers": [
{
"blocks": [
{
"unit": "item",
"size": "1.0",
"max": "5.0",
"prices": [
{
"currency": "IDR",
"value": 250000
}
]
}
],
"limits": [],
"fixedPrice": [],
"recurringPrice": []
}
]
}
]
}
]
}
],
"included": [],
"available": []
}
],
"priceLists": [
{
"name": "DEFAULT",
"plans": [
"postpaid-monthly-fix",
"postpaid-monthly-trial1",
"postpaid-monthly-trial7",
"prepaid-signature-item"
]
}
]
},
{
"name": "DEFAULT",
"effectiveDate": "Apr 21, 2018 3:35:31 PM",
"currencies": [
"IDR"
],
"products": [
{
"type": "BASE",
"name": "PostPaid",
"plans": [
{
"name": "postpaid-monthly-fix",
"billingPeriod": "MONTHLY",
"phases": [
{
"type": "EVERGREEN",
"prices": [
{
"currency": "IDR",
"value": 300000
}
],
"fixedPrices": [],
"duration": {
"unit": "UNLIMITED",
"number": -1
},
"usages": []
}
]
},
{
"name": "postpaid-monthly-trial1",
"billingPeriod": "MONTHLY",
"phases": [
{
"type": "TRIAL",
"prices": [],
"fixedPrices": [
{
"currency": "IDR",
"value": 0
}
],
"duration": {
"unit": "DAYS",
"number": 1
},
"usages": []
},
{
"type": "EVERGREEN",
"prices": [
{
"currency": "IDR",
"value": 250000
}
],
"fixedPrices": [],
"duration": {
"unit": "UNLIMITED",
"number": -1
},
"usages": []
}
]
},
{
"name": "postpaid-monthly-trial7",
"billingPeriod": "MONTHLY",
"phases": [
{
"type": "TRIAL",
"prices": [],
"fixedPrices": [
{
"currency": "IDR",
"value": 0
}
],
"duration": {
"unit": "DAYS",
"number": 7
},
"usages": []
},
{
"type": "EVERGREEN",
"prices": [
{
"currency": "IDR",
"value": 350000
}
],
"fixedPrices": [],
"duration": {
"unit": "UNLIMITED",
"number": -1
},
"usages": []
}
]
}
],
"included": [],
"available": []
},
{
"type": "BASE",
"name": "PrePaid",
"plans": [
{
"name": "prepaid-signature-item",
"billingPeriod": "NO_BILLING_PERIOD",
"phases": [
{
"type": "EVERGREEN",
"prices": [],
"fixedPrices": [],
"duration": {
"unit": "UNLIMITED",
"number": -1
},
"usages": [
{
"billingPeriod": "DAILY",
"tiers": [
{
"blocks": [
{
"unit": "item",
"size": "1.0",
"max": "5.0",
"prices": [
{
"currency": "IDR",
"value": 250000
}
]
}
],
"limits": [],
"fixedPrice": [],
"recurringPrice": []
}
]
}
]
}
]
}
],
"included": [],
"available": []
}
],
"priceLists": [
{
"name": "DEFAULT",
"plans": [
"postpaid-monthly-fix",
"postpaid-monthly-trial1",
"postpaid-monthly-trial7",
"prepaid-signature-item"
]
}
]
},
{
"name": "DEFAULT",
"effectiveDate": "Apr 21, 2018 3:38:31 PM",
"currencies": [
"IDR"
],
"products": [
{
"type": "BASE",
"name": "PostPaid",
"plans": [
{
"name": "postpaid-monthly-fix",
"billingPeriod": "MONTHLY",
"phases": [
{
"type": "EVERGREEN",
"prices": [
{
"currency": "IDR",
"value": 300000
}
],
"fixedPrices": [],
"duration": {
"unit": "UNLIMITED",
"number": -1
},
"usages": []
}
]
},
{
"name": "postpaid-monthly-trial1",
"billingPeriod": "MONTHLY",
"phases": [
{
"type": "TRIAL",
"prices": [],
"fixedPrices": [
{
"currency": "IDR",
"value": 0
}
],
"duration": {
"unit": "DAYS",
"number": 1
},
"usages": []
},
{
"type": "EVERGREEN",
"prices": [
{
"currency": "IDR",
"value": 250000
}
],
"fixedPrices": [],
"duration": {
"unit": "UNLIMITED",
"number": -1
},
"usages": []
}
]
},
{
"name": "postpaid-monthly-trial7",
"billingPeriod": "MONTHLY",
"phases": [
{
"type": "TRIAL",
"prices": [],
"fixedPrices": [
{
"currency": "IDR",
"value": 0
}
],
"duration": {
"unit": "DAYS",
"number": 7
},
"usages": []
},
{
"type": "EVERGREEN",
"prices": [
{
"currency": "IDR",
"value": 350000
}
],
"fixedPrices": [],
"duration": {
"unit": "UNLIMITED",
"number": -1
},
"usages": []
}
]
}
],
"included": [],
"available": []
},
{
"type": "BASE",
"name": "PrePaid",
"plans": [
{
"name": "prepaid-signature-item",
"billingPeriod": "NO_BILLING_PERIOD",
"phases": [
{
"type": "EVERGREEN",
"prices": [],
"fixedPrices": [],
"duration": {
"unit": "UNLIMITED",
"number": -1
},
"usages": [
{
"billingPeriod": "DAILY",
"tiers": [
{
"blocks": [
{
"unit": "item",
"size": "1.0",
"max": "5.0",
"prices": [
{
"currency": "IDR",
"value": 250000
}
]
}
],
"limits": [],
"fixedPrice": [],
"recurringPrice": []
}
]
}
]
}
]
}
],
"included": [],
"available": []
}
],
"priceLists": [
{
"name": "DEFAULT",
"plans": [
"postpaid-monthly-fix",
"postpaid-monthly-trial1",
"postpaid-monthly-trial7",
"prepaid-signature-item"
]
}
]
}
]
where json build by this code:
String json = new Gson().toJson(price);
then i want to get single value, for example :
"plans":["postpaid-monthly-fix","postpaid-monthly-trial1","postpaid-monthly-trial7","prepaid-signature-item"]
i try with json.get("plans") but get syntax erro, any clue ?
Try the following:
String s = "[{\"name\":\"DEFAULT\",\"effectiveDate\":\"Apr 19, 2018 3:35:31 PM\",\"currencies\":[\"IDR\"],\"products\":[{\"type\":\"BASE\",\"name\":\"PostPaid\",\"plans\":[{\"name\":\"postpaid-monthly-fix\",\"billingPeriod\":\"MONTHLY\",\"phases\":[{\"type\":\"EVERGREEN\",\"prices\":[{\"currency\":\"IDR\",\"value\":300000}],\"fixedPrices\":[],\"duration\":{\"unit\":\"UNLIMITED\",\"number\":-1},\"usages\":[]}]},{\"name\":\"postpaid-monthly-trial1\",\"billingPeriod\":\"MONTHLY\",\"phases\":[{\"type\":\"TRIAL\",\"prices\":[],\"fixedPrices\":[{\"currency\":\"IDR\",\"value\":0}],\"duration\":{\"unit\":\"DAYS\",\"number\":1},\"usages\":[]},{\"type\":\"EVERGREEN\",\"prices\":[{\"currency\":\"IDR\",\"value\":250000}],\"fixedPrices\":[],\"duration\":{\"unit\":\"UNLIMITED\",\"number\":-1},\"usages\":[]}]},{\"name\":\"postpaid-monthly-trial7\",\"billingPeriod\":\"MONTHLY\",\"phases\":[{\"type\":\"TRIAL\",\"prices\":[],\"fixedPrices\":[{\"currency\":\"IDR\",\"value\":0}],\"duration\":{\"unit\":\"DAYS\",\"number\":7},\"usages\":[]},{\"type\":\"EVERGREEN\",\"prices\":[{\"currency\":\"IDR\",\"value\":350000}],\"fixedPrices\":[],\"duration\":{\"unit\":\"UNLIMITED\",\"number\":-1},\"usages\":[]}]}],\"included\":[],\"available\":[]},{\"type\":\"BASE\",\"name\":\"PrePaid\",\"plans\":[{\"name\":\"prepaid-signature-item\",\"billingPeriod\":\"NO_BILLING_PERIOD\",\"phases\":[{\"type\":\"EVERGREEN\",\"prices\":[],\"fixedPrices\":[],\"duration\":{\"unit\":\"UNLIMITED\",\"number\":-1},\"usages\":[{\"billingPeriod\":\"DAILY\",\"tiers\":[{\"blocks\":[{\"unit\":\"item\",\"size\":\"1.0\",\"max\":\"5.0\",\"prices\":[{\"currency\":\"IDR\",\"value\":250000}]}],\"limits\":[],\"fixedPrice\":[],\"recurringPrice\":[]}]}]}]}],\"included\":[],\"available\":[]}],\"priceLists\":[{\"name\":\"DEFAULT\",\"plans\":[\"postpaid-monthly-fix\",\"postpaid-monthly-trial1\",\"postpaid-monthly-trial7\",\"prepaid-signature-item\"]}]},{\"name\":\"DEFAULT\",\"effectiveDate\":\"Apr 20, 2018 3:35:31 PM\",\"currencies\":[\"IDR\"],\"products\":[{\"type\":\"BASE\",\"name\":\"PostPaid\",\"plans\":[{\"name\":\"postpaid-monthly-fix\",\"billingPeriod\":\"MONTHLY\",\"phases\":[{\"type\":\"EVERGREEN\",\"prices\":[{\"currency\":\"IDR\",\"value\":300000}],\"fixedPrices\":[],\"duration\":{\"unit\":\"UNLIMITED\",\"number\":-1},\"usages\":[]}]},{\"name\":\"postpaid-monthly-trial1\",\"billingPeriod\":\"MONTHLY\",\"phases\":[{\"type\":\"TRIAL\",\"prices\":[],\"fixedPrices\":[{\"currency\":\"IDR\",\"value\":0}],\"duration\":{\"unit\":\"DAYS\",\"number\":1},\"usages\":[]},{\"type\":\"EVERGREEN\",\"prices\":[{\"currency\":\"IDR\",\"value\":250000}],\"fixedPrices\":[],\"duration\":{\"unit\":\"UNLIMITED\",\"number\":-1},\"usages\":[]}]},{\"name\":\"postpaid-monthly-trial7\",\"billingPeriod\":\"MONTHLY\",\"phases\":[{\"type\":\"TRIAL\",\"prices\":[],\"fixedPrices\":[{\"currency\":\"IDR\",\"value\":0}],\"duration\":{\"unit\":\"DAYS\",\"number\":7},\"usages\":[]},{\"type\":\"EVERGREEN\",\"prices\":[{\"currency\":\"IDR\",\"value\":350000}],\"fixedPrices\":[],\"duration\":{\"unit\":\"UNLIMITED\",\"number\":-1},\"usages\":[]}]}],\"included\":[],\"available\":[]},{\"type\":\"BASE\",\"name\":\"PrePaid\",\"plans\":[{\"name\":\"prepaid-signature-item\",\"billingPeriod\":\"NO_BILLING_PERIOD\",\"phases\":[{\"type\":\"EVERGREEN\",\"prices\":[],\"fixedPrices\":[],\"duration\":{\"unit\":\"UNLIMITED\",\"number\":-1},\"usages\":[{\"billingPeriod\":\"NO_BILLING_PERIOD\",\"tiers\":[{\"blocks\":[{\"unit\":\"item\",\"size\":\"1.0\",\"max\":\"5.0\",\"prices\":[{\"currency\":\"IDR\",\"value\":250000}]}],\"limits\":[],\"fixedPrice\":[],\"recurringPrice\":[]}]}]}]}],\"included\":[],\"available\":[]}],\"priceLists\":[{\"name\":\"DEFAULT\",\"plans\":[\"postpaid-monthly-fix\",\"postpaid-monthly-trial1\",\"postpaid-monthly-trial7\",\"prepaid-signature-item\"]}]},{\"name\":\"DEFAULT\",\"effectiveDate\":\"Apr 21, 2018 3:35:31 PM\",\"currencies\":[\"IDR\"],\"products\":[{\"type\":\"BASE\",\"name\":\"PostPaid\",\"plans\":[{\"name\":\"postpaid-monthly-fix\",\"billingPeriod\":\"MONTHLY\",\"phases\":[{\"type\":\"EVERGREEN\",\"prices\":[{\"currency\":\"IDR\",\"value\":300000}],\"fixedPrices\":[],\"duration\":{\"unit\":\"UNLIMITED\",\"number\":-1},\"usages\":[]}]},{\"name\":\"postpaid-monthly-trial1\",\"billingPeriod\":\"MONTHLY\",\"phases\":[{\"type\":\"TRIAL\",\"prices\":[],\"fixedPrices\":[{\"currency\":\"IDR\",\"value\":0}],\"duration\":{\"unit\":\"DAYS\",\"number\":1},\"usages\":[]},{\"type\":\"EVERGREEN\",\"prices\":[{\"currency\":\"IDR\",\"value\":250000}],\"fixedPrices\":[],\"duration\":{\"unit\":\"UNLIMITED\",\"number\":-1},\"usages\":[]}]},{\"name\":\"postpaid-monthly-trial7\",\"billingPeriod\":\"MONTHLY\",\"phases\":[{\"type\":\"TRIAL\",\"prices\":[],\"fixedPrices\":[{\"currency\":\"IDR\",\"value\":0}],\"duration\":{\"unit\":\"DAYS\",\"number\":7},\"usages\":[]},{\"type\":\"EVERGREEN\",\"prices\":[{\"currency\":\"IDR\",\"value\":350000}],\"fixedPrices\":[],\"duration\":{\"unit\":\"UNLIMITED\",\"number\":-1},\"usages\":[]}]}],\"included\":[],\"available\":[]},{\"type\":\"BASE\",\"name\":\"PrePaid\",\"plans\":[{\"name\":\"prepaid-signature-item\",\"billingPeriod\":\"NO_BILLING_PERIOD\",\"phases\":[{\"type\":\"EVERGREEN\",\"prices\":[],\"fixedPrices\":[],\"duration\":{\"unit\":\"UNLIMITED\",\"number\":-1},\"usages\":[{\"billingPeriod\":\"DAILY\",\"tiers\":[{\"blocks\":[{\"unit\":\"item\",\"size\":\"1.0\",\"max\":\"5.0\",\"prices\":[{\"currency\":\"IDR\",\"value\":250000}]}],\"limits\":[],\"fixedPrice\":[],\"recurringPrice\":[]}]}]}]}],\"included\":[],\"available\":[]}],\"priceLists\":[{\"name\":\"DEFAULT\",\"plans\":[\"postpaid-monthly-fix\",\"postpaid-monthly-trial1\",\"postpaid-monthly-trial7\",\"prepaid-signature-item\"]}]},{\"name\":\"DEFAULT\",\"effectiveDate\":\"Apr 21, 2018 3:38:31 PM\",\"currencies\":[\"IDR\"],\"products\":[{\"type\":\"BASE\",\"name\":\"PostPaid\",\"plans\":[{\"name\":\"postpaid-monthly-fix\",\"billingPeriod\":\"MONTHLY\",\"phases\":[{\"type\":\"EVERGREEN\",\"prices\":[{\"currency\":\"IDR\",\"value\":300000}],\"fixedPrices\":[],\"duration\":{\"unit\":\"UNLIMITED\",\"number\":-1},\"usages\":[]}]},{\"name\":\"postpaid-monthly-trial1\",\"billingPeriod\":\"MONTHLY\",\"phases\":[{\"type\":\"TRIAL\",\"prices\":[],\"fixedPrices\":[{\"currency\":\"IDR\",\"value\":0}],\"duration\":{\"unit\":\"DAYS\",\"number\":1},\"usages\":[]},{\"type\":\"EVERGREEN\",\"prices\":[{\"currency\":\"IDR\",\"value\":250000}],\"fixedPrices\":[],\"duration\":{\"unit\":\"UNLIMITED\",\"number\":-1},\"usages\":[]}]},{\"name\":\"postpaid-monthly-trial7\",\"billingPeriod\":\"MONTHLY\",\"phases\":[{\"type\":\"TRIAL\",\"prices\":[],\"fixedPrices\":[{\"currency\":\"IDR\",\"value\":0}],\"duration\":{\"unit\":\"DAYS\",\"number\":7},\"usages\":[]},{\"type\":\"EVERGREEN\",\"prices\":[{\"currency\":\"IDR\",\"value\":350000}],\"fixedPrices\":[],\"duration\":{\"unit\":\"UNLIMITED\",\"number\":-1},\"usages\":[]}]}],\"included\":[],\"available\":[]},{\"type\":\"BASE\",\"name\":\"PrePaid\",\"plans\":[{\"name\":\"prepaid-signature-item\",\"billingPeriod\":\"NO_BILLING_PERIOD\",\"phases\":[{\"type\":\"EVERGREEN\",\"prices\":[],\"fixedPrices\":[],\"duration\":{\"unit\":\"UNLIMITED\",\"number\":-1},\"usages\":[{\"billingPeriod\":\"DAILY\",\"tiers\":[{\"blocks\":[{\"unit\":\"item\",\"size\":\"1.0\",\"max\":\"5.0\",\"prices\":[{\"currency\":\"IDR\",\"value\":250000}]}],\"limits\":[],\"fixedPrice\":[],\"recurringPrice\":[]}]}]}]}],\"included\":[],\"available\":[]}],\"priceLists\":[{\"name\":\"DEFAULT\",\"plans\":[\"postpaid-monthly-fix\",\"postpaid-monthly-trial1\",\"postpaid-monthly-trial7\",\"prepaid-signature-item\"]}]}]\n";
JSONArray jsonArray = (JSONArray) JSONValue.parseWithException(s);
List plans = (List)((Map)((List)((Map)jsonArray.get(0)).get("priceLists")).get(0)).get("plans");
System.out.println(plans);
System.out.println(plans.get(0));
System.out.println(plans.get(1));
System.out.println(plans.get(2));
System.out.println(plans.get(3));
Output:
["postpaid-monthly-fix","postpaid-monthly-trial1","postpaid-monthly-trial7","prepaid-signature-item"]
postpaid-monthly-fix
postpaid-monthly-trial1
postpaid-monthly-trial7
prepaid-signature-item

Categories

Resources