Class JsonPathStreamingService

  • All Implemented Interfaces:
    AdaptrisComponent, ComponentLifecycle, ComponentLifecycleExtension, MessageEventGenerator, Service, StateManagedComponent

    @ComponentProfile(summary="Extract a value from a large JSON document",
                      tag="service,transform,json,metadata,streaming,large")
    public class JsonPathStreamingService
    extends com.adaptris.core.services.path.json.JsonPathServiceImpl
    This service allows you to search JSON content and the results are then set back into the message. The advantage of this implementation is that it doesn't need to parse the entire JSON document and so is able to handle arbitrarily large documents.

    The searching works in much the same way as XPath, for more information on how to build a JSON path see the JSONPath documentation and the JSON Surfer 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-streaming-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-streaming-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;
    • metadata-key-1 = "Sayings of the Century"
    • metadata-key-2 = "Sword of Honour"

    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-streaming-service which is the preferred alternative to the fully qualified classname when building your configuration.

    • Constructor Detail

      • JsonPathStreamingService

        public JsonPathStreamingService​(DataInputParameter<java.io.InputStream> source,
                                        Execution... executions)
      • JsonPathStreamingService

        public JsonPathStreamingService​(DataInputParameter<java.io.InputStream> source,
                                        java.util.List<Execution> executions)
      • JsonPathStreamingService

        public JsonPathStreamingService()
    • Method Detail

      • prepare

        public void prepare()
      • getSource

        @NotNull
        @NonNull
        public @NotNull @NonNull DataInputParameter<java.io.InputStream> getSource()
        The source for executing the jsonpath against.
      • setSource

        public void setSource​(@NotNull @NonNull
                              @NotNull @NonNull DataInputParameter<java.io.InputStream> source)
        The source for executing the jsonpath against.