Adding a specific property to every json object in an array in powershell

I have an array of JSON objects, and I want to add a specific property to every object present.

For example, an array looks like this:

[
    {
        "Average":  1.3085,
        "ExtendedStatistics":  {

                               },
        "Maximum":  0,
        "Minimum":  0,
        "SampleCount":  0,
        "Sum":  0,
        "Timestamp":  "\/Date(1496972280000)\/",
        "Unit":  {
                     "Value":  "Percent"
                 }
    },
    {
        "Average":  1.4324999999999999,
        "ExtendedStatistics":  {

                               },
        "Maximum":  0,
        "Minimum":  0,
        "SampleCount":  0,
        "Sum":  0,
        "Timestamp":  "\/Date(1496944680000)\/",
        "Unit":  {
                     "Value":  "Percent"
                 }
    }
]

I want to add "source": "CPU" to all objects. How should I do it? I am new to PowerShell and could not do this.

+4
source share
2 answers

You can do the following:

$JSON | ConvertFrom-Json | ForEach-Object { 
    $_ | Add-Member -MemberType NoteProperty -Name 'Source' -Value 'CPU' -PassThru
} | ConvertTo-Json

This assumes that your JSON input is in a variable with a name $JSON, you will need to replace this, however you will get access to your JSON content (for example, Get-Content yourfile.json).

JSON, ConvertFrom-JSON PowerShell.

ForEach-Object, Add-Member, ( $_) "" "CPU". mklement0 -PassThru, .

ConvertTo-JSON, .

+5

Mark Wragg , , Add-Member , ForEach-Object :

, , (Windows PowerShell v5.1, PowerShell Core v6-beta.2) :

# !! Currently does NOT work as expected.
$JSON | ConvertFrom-Json | 
  Add-Member -MemberType NoteProperty -Name 'Source' -Value 'CPU' -PassThru 

, Add-Member , , -PassThru ( Add-Member ).

, , , , ConvertFrom-Json , , , .

PowerShell GitHub.

, , ; " " , .

  • (...), :
# Enclosing the ConvertFrom-Json command in (...) forces enumeration.
($JSON | ConvertFrom-Json) | 
  Add-Member -MemberType NoteProperty -Name 'Source' -Value 'CPU' -PassThru 

, , , (...) , . , PetSerAl, , ConvertFrom-Json .

  • : Write-Output -NoEnumerate (Windows PowerShell)/
    Write-Output (PowerShell Core v6 post-beta 8, - ),
# Inserting Write-Output [-NoEnumerate] between ConvertFrom-Json and Add-Member
# forces enumeration of the array elements.

# *Windows PowerShell*, as of v5.1:
$JSON | ConvertFrom-Json | Write-Output -NoEnumerate |
  Add-Member -MemberType NoteProperty -Name 'Source' -Value 'CPU' -PassThru 

# PowerShell *Core*:
$JSON | ConvertFrom-Json | Write-Output |
  Add-Member -MemberType NoteProperty -Name 'Source' -Value 'CPU' -PassThru 

: Write-Output:

Windows PowerShell v5.1:

- , Write-Output , , -NoEnumerate.

, -NoEnumerate - ! - Write-Output : () ( , PetSerAl); :

# !! Unexpectedly returns 4(!): enumerates the outer 2-element array
# !! *and* its elements.
# (In PowerShell *Core*, this yields 2, as expected.)
Write-Output -InputObject (1, 2), (3, 4) | Measure-Object

# BETTER: yields 2, because only the outer 2-element array is enumerated
# (In PowerShell *Core*, this yields 1, as expected.)
Write-Output -NoEnumerate -InputObject (1, 2), (3, 4) | Measure-Object

PowerShell Core:

​​, , - - -NoEnumerate, Write-Output , ( 1 ).

, : [PSObject[]]:

> (1 | Write-Output -NoEnumerate).GetType().Name
PSObject[]

PetSerAl .

+4

Source: https://habr.com/ru/post/1678930/


All Articles