OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

PowerShell - ConvertTo-Json how to get ndjson result

  • Thread starter Thread starter anhtle
  • Start date Start date
A

anhtle

Guest
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 in JSON format. Is it possible to output to NDJSON format or do I have to do some string manipulation?

My input file test.json:

Code:
{"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:

Code:
$json = (Get-Content C:\Temp\test.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:

Code:
[
    {
        "PERIOD":  "2024004",
        "JRNAL_NO":  "38115"
    },
    {
        "PERIOD":  "2024004",
        "JRNAL_NO":  "38115"
    },
    {
        "PERIOD":  "2024004",
        "JRNAL_NO":  "38115"
    },
    {
        "PERIOD":  "2024004",
        "JRNAL_NO":  "38115"
    }
]

I need the output file to be NDJSON, basically the same format as the input file. 38115.json should be:

Code:
{"PERIOD":"2024004","JRNAL_NO":"38115"}
{"PERIOD":"2024004","JRNAL_NO":"38115"}
{"PERIOD":"2024004","JRNAL_NO":"38115"}
{"PERIOD":"2024004","JRNAL_NO":"38115"}

38116.json should be:

Code:
{"PERIOD":"2024004","JRNAL_NO":"38116"}
<p>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 in JSON format. Is it possible to output to NDJSON format or do I have to do some string manipulation?</p>
<p>My input file test.json:</p>
<pre><code>{"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"}
</code></pre>
<p>My powershell script so far:</p>
<pre><code>$json = (Get-Content C:\Temp\test.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
}
</code></pre>
<p>My current output. Here's the 38115.json file:</p>
<pre><code>[
{
"PERIOD": "2024004",
"JRNAL_NO": "38115"
},
{
"PERIOD": "2024004",
"JRNAL_NO": "38115"
},
{
"PERIOD": "2024004",
"JRNAL_NO": "38115"
},
{
"PERIOD": "2024004",
"JRNAL_NO": "38115"
}
]

</code></pre>
<p>I need the output file to be NDJSON, basically the same format as the input file.
38115.json should be:</p>
<pre><code>{"PERIOD":"2024004","JRNAL_NO":"38115"}
{"PERIOD":"2024004","JRNAL_NO":"38115"}
{"PERIOD":"2024004","JRNAL_NO":"38115"}
{"PERIOD":"2024004","JRNAL_NO":"38115"}
</code></pre>
<p>38116.json should be:</p>
<pre><code>{"PERIOD":"2024004","JRNAL_NO":"38116"}
</code></pre>
Continue reading...
 
Top