0

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

3
  • Have you tried using Convertfrom-json ? Commented Feb 18, 2019 at 10:12
  • $LoginResponse | ConvertTo-Json -Depth 10 | Out-File -FilePath .\Process.json I Used this command just after the Invoke-RestMethod one. I don't see the goal on converting to JSON and converting from JSON just after it ? @RanadipDutta Commented Feb 18, 2019 at 10:16
  • You have gotten an answer which is pretty much what you need. Commented Feb 18, 2019 at 10:25

1 Answer 1

2

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})) {
    ...
}
Sign up to request clarification or add additional context in comments.

5 Comments

I think this will help a lot. I'll work with this and let you know. Thanks for your answear.
@LotPings Thanks for the edit. I mistakenly left the test variable name in there.
Can someone explain me why we'd use $_.isTemplate -eq $true instead of $_.isTemplate -eq "true" as the variable $true hasn't been defined in the code before ?
@MouAmg The ConvertFrom-Json cmdlet parses the json file into an object. (in fact an array of objects in this case). Wherever it encounters properties like "isTemplate": true, the value of this property is automatically converted to a Boolean value. In PowerShell you write a true boolean as $true. You can check by examining the type of the isTemplate property after conversion with something like this: ($json[0].isTemplate).gettype()
Thanks you for the answears. Just tested your code, exactly what i needed. Thanks you once again. Solved.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.