Search code examples
arraysjsonspring-xdsplitter

How to split incoming Json objects array in Spring XD?


Let's say my incoming data is a json object or an array of json objects. I want to split the incoming json data iff it's an array so that I can insert them into a MongoDB collection (For simplicity, let's just log to console for now).

  1. Define Stream

    xd:>stream create splittest --definition "http | json-to-tuple | splitter | log" --deploy

  2. Post a single object

    xd:>http post --data "{\"dog\": \"chihuahua\"}"

    21T09:05:59-0700 1.2.0.RELEASE INFO pool-44-thread-4 sink.splittest - {"dog":"chihuahua"}

    This is expected output as the input was a single object - nothing to split.

  3. Post an array of Objects

    xd:>http post --data "[{\"dog\": \"poodle\"}, {\"dog\": \"chihuahua\"}, {\"dog\":\"poodle\"}]"

    21T09:43:05-0700 1.2.0.RELEASE INFO pool-44-thread-11 sink.splittest - {}

    In this case, I expected to see three individual dog objects printed in each line but it printed a single empty object.

What am I doing wrong? Can somebody suggest what could/should have been done?


Solution

  • Figured out how to do that. I didn't need to bother using json-to-tuple processor.

    xd:>stream create splittest2 --definition "http | splitter --expression=#jsonPath(payload,'$.[*]') | log" --deploy
    
    xd:>http post --data "[{\"dog\": \"poodle\"}, {\"dog\": \"chihuahua\"}, {\"dog\":\"poodle\"}]"
    
    2015-10-21T10:52:31-0700 1.2.0.RELEASE INFO pool-83-thread-4 sink.splittest2 - {dog=poodle}
    2015-10-21T10:52:31-0700 1.2.0.RELEASE INFO pool-83-thread-4 sink.splittest2 - {dog=chihuahua}
    2015-10-21T10:52:31-0700 1.2.0.RELEASE INFO pool-83-thread-4 sink.splittest2 - {dog=poodle}
    

    So it works on the multiple objects, however, it doesn't handle single object as I expected.

    xd:>http post --data "{\"dog\":\"chihuahua\"}"
    2015-10-21T10:56:11-0700 1.2.0.RELEASE INFO pool-83-thread-10 sink.splittest2 - chihuahua