Convert irregular format CSV to standard JSON strings
In the following CSV file, the 3rd column contains irregular-format, JSON string-like values.
header1,header2,header3
value1,value2,{"name":"John","age":30,"car":null}
data1,data2,{"name":"Lily","age":28,"car":"Benz"}
Task: Use Java to arrange the CSV file into standard JSON strings.
[{
"header1": "value1",
"header2": "value2",
"header3": "{\"name\":\"John\",\"age\":30,\"car\":null}"
}, {
"header1": "data1",
"header2": "data2",
"header3": "{\"name\":\"Lily\",\"age\":28,\"car\":\"Benz\"}"
}]
SPL code:
=json(file($[data.csv]).import@tcpf())
json()function switches between a JSON string and a two-dimensional table. import() functions imports the file; @t option imports the first row as field names; @c enables using comma as the separator; @p means that the separator within the parentheses won’t be handled; and @f does not parse field values but treats them as strings, otherwise header3 values will be parsed as two-dimensional tables and as a result the final result won’t contain "" (it’s right to not to contain"" when they are output as a file).
Read How to Call a SPL Script in Java to find how to integrate SPL into a Java application.
Source:https://stackoverflow.com/questions/75074834/convert-csv-with-nested-json-object-to-json
SPL Official Website 👉 https://www.scudata.com
SPL Feedback and Help 👉 https://www.reddit.com/r/esProcSPL
SPL Learning Material 👉 https://c.scudata.com
SPL Source Code and Package 👉 https://github.com/SPLWare/esProc
Discord 👉 https://discord.gg/cFTcUNs7
Youtube 👉 https://www.youtube.com/@esProc_SPL