3

I am working with a JSON like that looks like this:

[
    {
        "Ack":  "no",
        "Rule":  "dont",
        "Tags":  [
                     "server"
                 ],
        "Type":  "blue"
    },

    {
        "Ack":  "no1",
        "Rule":  "knock",
        "Tags":  [
                     "yellow",
                     "green"
                 ],
        "Type":  "multiplecolour"
    }

]

I need to convert the Tags array into a comma-separated string [and replace the array with the converted string in the JSON file]. I have tried converting from JSON, but I am struggling to convert the array into string in a clean way, still learning PS so please bear with me.

mr i.o
  • 952
  • 2
  • 10
  • 20

1 Answers1

6

ConvertFrom-Json may work for you. Here's an example of converting your JSON string to an array of PowerShell objects, then joining the tags for each object with a comma delimiter:

$json = @"
[
    {
        "Ack":  "no",
        "Rule":  "dont",
        "Tags":  [
                     "server"
                 ],
        "Type":  "blue"
    },
    {
        "Ack":  "no1",
        "Rule":  "knock",
        "Tags":  [
                     "yellow",
                     "green"
                 ],
        "Type":  "multiplecolour"
    }
]
"@

(ConvertFrom-Json -InputObject $json) `
    | ForEach-Object { $_.Tags = ($_.Tags -join ","); $_ } `
    | ConvertTo-Json `
    | Out-File -FilePath new.json

EDIT: Note (as @mklement0 points out), the parentheses around ConvertFrom-Json are required to force the enumeration of the results as an array of objects through the pipeline.

Glenn
  • 1,687
  • 15
  • 21
  • 4
    Nice, though it's worth pointing out that `(...)` is _required_, because it forces enumeration of the [array that `ConvertFrom-Json` sends _as a whole_ through the pipeline](https://github.com/PowerShell/PowerShell/issues/3424). – mklement0 Oct 11 '19 at 20:15
  • 1
    @mri.o: Please state all requirements up front in the future - it is not clear from your question as currently stated. Perhaps Glenn is willing to update his answer, but the short of it is that you must use `$_.Tags = $_.Tags -join ","` instead, and pipe to `ConvertTo-Json`. – mklement0 Oct 11 '19 at 20:58
  • 1
    $jsonParameters = (ConvertFrom-Json -InputObject $content) $jsonParameters | ForEach-Object { $_.Tags -join "," } | ConvertTo-Json | out-file new.json - this is how I piped it, the content of new.json is only the tag strings. @Glenn. It doesn't create it properly. – mr i.o Oct 11 '19 at 21:05
  • @mri.o So, your intent is to reproduce the JSON with the only difference being that "Tags" is no longer an array, but a string property, which contains one or more comma-delimited values? For example, it becomes this for the second array element? `"Tags": "yellow,green"` – Glenn Oct 11 '19 at 21:16
  • If so, the latest update to the answer should work for you. – Glenn Oct 11 '19 at 21:23
  • 1
    Learnt something new today, thanks @Glenn, latest update works! – mr i.o Oct 11 '19 at 21:32