How to read a complex JSON in spring batch?

允我心安 提交于 2019-12-08 07:39:19

问题


I have a complex JSON below. I am reading it using FlatFileItemReader. How can I ignore the last line "]", with my customized ComplexJsonRecordSeparatorPolicy?

[
  {"firstName":"Tom", "lastName":"Cruise"}, 
  {"firstName":"Bruce", "lastName":"Willis"},
  {"firstName":"Liam", "lastName":"Neeson"}
]

My ComplexJsonRecordSeparatorPolicy looks like below. This class is successfully working, when I have "]" in line no 4 but, it throws an error when the line is supplied with only "]" in line no 5, as my post processor deletes the line instead of ignoring it.

public class ComplexJsonRecordSeparatorPolicy extends JsonRecordSeparatorPolicy {

    @Override
    public boolean isEndOfRecord(String line) {

        return StringUtils.countOccurrencesOf(line, "{") == StringUtils.countOccurrencesOf(line, "}")
                && (line.trim().endsWith("}") || line.trim().endsWith(",") || line.trim().endsWith("]"));
    }

    @Override
    public String postProcess(String record) {
        if (record.startsWith("["))
            record = record.substring(1);
        if ((record.endsWith("]") || record.endsWith(",")))
            record = record.substring(0, record.length() - 1);
        return super.postProcess(record);
    }
}

回答1:


I have created a small example for this one. Please take a look and let me know your thoughts

https://github.com/bigzidane/spring-batch-jsonListItem-reader.

For more information, I have created a Reader which is to parse JSON as a List and then map every entry to a POJO through 'classToBound' and then return each by each follow Spring Batch standard.

The example takes an Json file as

[
    {
        "name": "zidane",
        "nation": "france"
    },
    {
        "name": "ronaldo",
        "nation": "brazil"
    },
    {
        "name": "marcelo",
        "nation": "brazil"
    }
]

The job configuration

<job id="exampleJsonReaderJob" xmlns="http://www.springframework.org/schema/batch">
        <step id="stepId">
            <tasklet>
                <chunk reader="exampleJsonReader" writer="exampleWriter" processor="exampleProcessor" commit-interval="5" />
            </tasklet>
        </step>
    </job>

The Reader

<bean id="exampleJsonReader" class="com.itservicesdepot.example.springbatch.jsonreader.reader.JsonFileListItemReader" scope="step">
        <property name="resource" value="classpath:soccers.json" />
        <property name="classToBound" value="com.itservicesdepot.example.springbatch.jsonreader.model.SoccerJsonEntry" />
    </bean>

The Processor and Writer are just normal ones.

In the reader, there is an property "classToBound", that is the Pojo which is top map with an item in Json list.



来源:https://stackoverflow.com/questions/49516398/how-to-read-a-complex-json-in-spring-batch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!