0

I'm trying to split very large JSON files using PowerShell into smaller files for a given array. For example, this JSON file:

Input JSON File

{
    "headerName1": "headerVal1",
    "headerName2": "headerVal2",
    "headerName3": [{
        "element1Name1": "element1Value1"
    },
    {
        "element2Name1": "element2Value1"
    },
    {
        "element3Name1": "element3Value1"
    },
    {
        "element4Name1": "element4Value1"
    },
    {
        "element5Name1": "element5Value1"
    },
    {
        "element6Name1": "element6Value1"
    }]
}

would need to be split into these JSON files (assuming each output file could only contain 2 elements of the headerName3 array):

Output JSON File 1

{
    "headerName1": "headerVal1",
    "headerName2": "headerVal2",
    "headerName3": [{
        "element1Name1": "element1Value1"
    },
    {
        "element2Name1": "element2Value1"
    }]
}

Output JSON File 2

{
    "headerName1": "headerVal1",
    "headerName2": "headerVal2",
    "headerName3": [{
        "element3Name1": "element3Value1"
    },
    {
        "element4Name1": "element4Value1"
    }]
}

Output JSON File 3

{
    "headerName1": "headerVal1",
    "headerName2": "headerVal2",
    "headerName3": [{
        "element5Name1": "element5Value1"
    },
    {
        "element6Name1": "element6Value1"
    }]
}

So each output file would need to contain headerName1 and headerName2 every time, but then only contain a certain number of elements from the headerName3 array. (Side note, this is a small example so I am splitting up the file by the number of elements in the array, but in reality I want to split by output file size to make things easier.)

Here is an excellent answer to this very question. However, I have two problems with the answer:

  1. The answer is in C#, not PowerShell.
  2. I did find this explanation for how to run C# in PowerShell. However, I run into the issue of the "Newtonsoft" library not being available to me so I can't get this C#-to-PowerShell solution to work.

So, I'm wondering if there is a straightforward way to do this in PowerShell.

0 Answers0