Mou Amg
Mou Amg

Reputation: 103

How to parse JSON File using Powershell, with conditions

I would like to work using powershell and json files. I have a JSON file generated from an Invoke-RestMethod on Powershell.

The output json file look like :

[
    {
        "_id": "ID1",
        "isTemplate": true,
        "origin": "SPO_Promoteur_DEV",
        "tenant": "spo",
        "reference": "HbiuOzq1u",
        "date": "2019-02-04T16:01:35.230Z",
        "data":
        {
            "tasks":
            {
                "rows": [
                    {
                        "parentId": "root",
                        "entities": [
                            "Something there"
                        ],
                        "Name": "A name there"
                    },
                    {
                        "parentId": "Z9zgsHzFad",
                        "entities": [
                            "urn:CodeEtape:DONGEN:CodeAction:TEST1"
                        ],
                        "Duration": 0,
                        "index": 0,
                        "Id": "wWotapvig"
                    }
                ]
            }
        }
    },
    {
        "_id": "ID12",
        "isTemplate": true,
        "origin": "SPO_Promoteur_DEV",
        "tenant": "spo",
        "reference": "Hbkjh548u",
        "date": "2019-02-04T16:01:35.230Z",
        "data":
        {
            "tasks":
            {
                "rows": [
                    {
                        "parentId": "root",
                        "entities": [
                            "Something else there"
                        ],
                        "Name": "An other name there"
                    },
                    {
                        "parentId": "Z9zgszffzfHzFad",
                        "entities": [
                            "urn:CodeEtape:DONGEN:CodeAction:TEST1"
                        ],
                        "Duration": 0,
                        "index": 0,
                        "Id": "wWotapvig"
                    }
                ]
            }
        }
    }
]

I would like to separate this json file. How can i generate json files named reference.json (where reference is given on the json main file) AND having isTemplate as true ?

So i'll get X file, each of them having their reference as name, and each of them having the IsTemplate parameter to TRUE.

Thanks for helping

Upvotes: 0

Views: 3882

Answers (1)

Theo
Theo

Reputation: 61218

If I understand the question correctly, you can read in the json file, which contains an array of items and save each item as a separate new json file like below.

Each item gets a filename of reference_XYZ.json where 'XYZ' will be the '_id' value.

$inputFile  = 'FULL PATH TO YOUR INPUT JSON FILE'
$outputPath = Split-Path $inputFile -Parent
$json       = Get-Content -Raw -Path $inputFile | ConvertFrom-Json

foreach ($item in $json) {
    $fileName = Join-Path -Path $outputPath -ChildPath ('reference_{0}.json' -f $item._id)
    $item | ConvertTo-Json -Depth 10 | Out-File $fileName
}

I'm not quite sure what you mean with and each of them having the IsTemplate parameter to TRUE, since both items do have that property set to True..

If you mean to save only items that have this "isTemplate": true and ignore others that are false, just add a Where-Object clause to the foreach loop like this:

foreach ($item in ($json | Where-Object { $_.isTemplate -eq $true})) {
    ...
}

Upvotes: 2

Related Questions