@ComponentProfile(summary="Extract a value from a JSON document", tag="service,transform,json,metadata") public class JsonPathService extends ServiceImp
The searching works in much the same way as XPath, for more information on how to build a JSON path see the JSONPath documentation.
For example, if you have a message with the following payload;
{
"store": {
"book": [ {
"category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
}, {
"category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
}, {
"category": "fiction",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99
}, {
"category": "fiction",
"author": "J. R. R. Tolkien",
"title": "The Lord of the Rings",
"isbn": "0-395-19395-8",
"price": 22.99
} ],
"bicycle": {
"color": "red",
"price": 19.95
}
},
"expensive": 10
}
You could configure 2 target destinations, each one creating a new metadata item with the results of the specified search, like
this;
<json-path-service>
<json-path-execution>
<source class="constant-data-input-parameter">
<value>$.store.book[0].title</value>
</source>
<target class="metadata-data-output-parameter">
<metadata-key>metadata-key-1</metadata-key>
</target>
<suppress-path-not-found>true</suppress-path-not-found>
</json-path-execution>
<json-path-execution>
<source class="constant-data-input-parameter">
<value>$.store.book[1].title</value>
</source>
<target class="metadata-data-output-parameter">
<metadata-key>metadata-key-2</metadata-key>
</target>
</json-path-execution>
</json-path-service>
The first target above searches for the first book title, the second target searches for the second book title.
Each target-destination will be executed in the order they are configured and therefore with the two targets shown here, your
message, after the
service has run, will include two new metadata items;
Any results returned by this service will normally include the json brackets wrapping the returned value. However you can
configure this
service to unwrap the result for you, such that a value returned as "[myValue]" will now be returned as "myValue".
The default value is false, but to override simply configure the "unwrap";
<json-path-service>
<unwrap-json>true</unwrap-json>
...
</json-path-service>
In the adapter configuration file this class is aliased as json-path-service which is the preferred alternative to the fully qualified classname when building your configuration.
Modifier and Type | Field and Description |
---|---|
protected com.jayway.jsonpath.Configuration |
jsonConfig |
log
CONFIRMATION_ID_KEY
Constructor and Description |
---|
JsonPathService() |
JsonPathService(DataInputParameter<java.lang.String> source,
Execution... executions) |
JsonPathService(DataInputParameter<java.lang.String> source,
java.util.List<Execution> executions) |
Modifier and Type | Method and Description |
---|---|
protected void |
closeService() |
void |
doService(AdaptrisMessage message)
.
|
java.util.List<Execution> |
getExecutions()
Get the list of execution.
|
DataInputParameter<java.lang.String> |
getSource()
Get the source.
|
java.lang.Boolean |
getSuppressPathNotFound()
Deprecated.
since 3.8.1; use a
JsonPathExecution with JsonPathExecution.setSuppressPathNotFound(Boolean)
instead. |
java.lang.Boolean |
getUnwrapJson()
Get whether the JSON should be unwrapped.
|
protected void |
initService() |
void |
prepare() |
void |
setExecutions(java.util.List<Execution> executions)
Set the list of executions.
|
void |
setSource(DataInputParameter<java.lang.String> source)
Set the source.
|
void |
setSuppressPathNotFound(java.lang.Boolean b)
Deprecated.
since 3.8.1; use a
JsonPathExecution with JsonPathExecution.getSuppressPathNotFound() instead. |
void |
setUnwrapJson(java.lang.Boolean unwrapJson)
Set whether the JSON should be unwrapped.
|
protected boolean |
suppressPathNotFound(Execution exec) |
protected static java.lang.String |
toString(java.lang.Object json,
Execution exec) |
protected static java.lang.String |
unwrap(java.lang.String json,
boolean unwrapJson)
Strip (if necessary) the leading/trailing [] from the JSON.
|
protected boolean |
unwrapJson() |
changeState, close, continueOnFailure, createName, createQualifier, getContinueOnFail, getIsTrackingEndpoint, getLookupName, getUniqueId, init, isBranching, isTrackingEndpoint, requestClose, requestInit, requestStart, requestStop, retrieveComponentState, setContinueOnFail, setIsTrackingEndpoint, setLookupName, setUniqueId, start, stop
public JsonPathService()
public JsonPathService(DataInputParameter<java.lang.String> source, Execution... executions)
public JsonPathService(DataInputParameter<java.lang.String> source, java.util.List<Execution> executions)
public void doService(AdaptrisMessage message) throws ServiceException
ServiceException
protected static java.lang.String toString(java.lang.Object json, Execution exec) throws java.lang.Exception
java.lang.Exception
protected static java.lang.String unwrap(java.lang.String json, boolean unwrapJson)
json
- The JSON string.public void prepare() throws CoreException
CoreException
protected void closeService()
closeService
in class ServiceImp
protected void initService() throws CoreException
initService
in class ServiceImp
CoreException
public DataInputParameter<java.lang.String> getSource()
public void setSource(DataInputParameter<java.lang.String> source)
source
- The source.public java.util.List<Execution> getExecutions()
public void setExecutions(java.util.List<Execution> executions)
executions
- The list of executions.public java.lang.Boolean getUnwrapJson()
public void setUnwrapJson(java.lang.Boolean unwrapJson)
unwrapJson
- Whether the JSON should be unwrapped; default is false.protected boolean unwrapJson()
@Deprecated @Removal(version="3.11.0") public java.lang.Boolean getSuppressPathNotFound()
JsonPathExecution
with JsonPathExecution.setSuppressPathNotFound(Boolean)
instead.@Deprecated @Removal(version="3.11.0") public void setSuppressPathNotFound(java.lang.Boolean b)
JsonPathExecution
with JsonPathExecution.getSuppressPathNotFound()
instead.PathNotFoundException
.b
- to suppress exceptions arising from a json path not being found; default is null (false).protected boolean suppressPathNotFound(Execution exec)