I am trying to split 1 NDJSON file into multiple NDJSON files. I am able to consume and split the file, but the problem is the resulting files are ion JSON format. Is it possible to output to NDJSON format or do I have to do some string manipulation?
My input file test.json:
{"PERIOD":"2024004","JRNAL_NO":"38115"}
{"PERIOD":"2024004","JRNAL_NO":"38115"}
{"PERIOD":"2024004","JRNAL_NO":"38115"}
{"PERIOD":"2024004","JRNAL_NO":"38115"}
{"PERIOD":"2024004","JRNAL_NO":"38116"}
My powershell script so far:
$json = (Get-Content C:Temptest.json) | ConvertFrom-Json
$jnl_list = $json.JRNAL_NO | select -Unique
ForEach ($jnl in $jnl_list) {
$Array = $json | Where-Object {$_.JRNAL_NO -eq $jnl}
$res = ($Array | ConvertTo-Json)
$res | Out-File -FilePath .JNL$($jnl).json
}
My current output. Here’s the 38115.json file:
[
{
"PERIOD": "2024004",
"JRNAL_NO": "38115"
},
{
"PERIOD": "2024004",
"JRNAL_NO": "38115"
},
{
"PERIOD": "2024004",
"JRNAL_NO": "38115"
},
{
"PERIOD": "2024004",
"JRNAL_NO": "38115"
}
]