Adding Member to JSON

Welcome Forums General PowerShell Q&A Adding Member to JSON

Viewing 3 reply threads
  • Author
    Posts
    • #253565
      Participant
      Topics: 1
      Replies: 1
      Points: 14
      Rank: Member

      Hello Everyone,

      I have been searching high and low for a way to add a value to a JSON object. I havent found something that works, but it might just be my lack of understanding.

      What I am trying to do is have a JSON object, that has the Date/Time, the Applications Currently Running, The application CPU %, and the current Disk Utilization.

       

      I want to run the script every 30 minutes, and keep appending to the JSON file.

       

      I think I want my JSON to look like this:

      [{
      “DateTime”:  CurrentTime1
      “Applications”:[{
      “Application”: “AppName#1”,
      “CPU Utilization”: “%CPU”
      },
      {
      “Application”: “AppName#1”,
      “CPU Utilization”: “%CPU”
      }
      ]
      }]

       

      And the next time it runs it adds another member with the next date time, and whatever the new applications/CPU% are.

      The code I wrote is below and it does not do what I want it to do.
      Any thoughts?

       

      $jobj= ""
      $jobj += get-wmiobject Win32_PerfFormattedData_PerfProc_Process |where {$_.PercentProcessorTime -gt 1 -and $_.name -ne "Idle" -and $_.name -ne "_Total"}| select @{n="Application";e={$_.name}},@{n="CPU";e={$_.PercentProcessorTime}} | Sort-Object CPU -Descending | ConvertTo-Json ;
      $toAdd = (get-counter -counter "\physicaldisk(_total)\% disk time").countersamples.cookedvalue
      $jobj | Add-Member -MemberType AliasProperty -Name 'DiskUtilization' -Value $toAdd

       

       

    • #253574
      Participant
      Topics: 0
      Replies: 81
      Points: 362
      Helping Hand
      Rank: Contributor

      You will not want to convert to JSON and then add new members. Add the new members while it is still a PowerShell Object (deserialized). If you are only running this every 30 minutes, you could just write your JSON to disk. Then read the JSON and convert to a PowerShell Object using ConvertFrom-Json on each subsequent run.

      If you do not do it this way, you will be stuck with doing JSON string appends or keeping the script running forever to keep the JSON in memory.

    • #253580
      Participant
      Topics: 15
      Replies: 1776
      Points: 3,218
      Helping Hand
      Rank: Community Hero

      It is more complex to manipulate JSON rather than manipulate a PSObject. If you are storing it externally, then Export-CliXml will export the object as-is and maintain the nested structure. Basically, this does 5 runs importing\exporting each iteration and at the end you import the xml and convert it to json:

    • #253649
      Participant
      Topics: 1
      Replies: 1
      Points: 14
      Rank: Member

      It is more complex to manipulate JSON rather than manipulate a PSObject. If you are storing it externally, then Export-CliXml will export the object as-is and maintain the nested structure. Basically, this does 5 runs importing\exporting each iteration and at the end you import the xml and convert it to json:

      <style class=”darkreader darkreader–sync” media=”screen”></style> <style class=”darkreader darkreader–sync” media=”screen”></style>

       

      This looks to do exactly what I am looking for. I was able to store it as JSON.

      Thank you for writing all that!

      Trying to look over what you did… it is so confusing!

      I am having a hard time understanding how powershell treats convertFrom-JSON…. because when I convert from JSON it wont let me add any new objects (says the system object does not support ADD).

      There are no really clear JSON powershell tutorials out there.

       

      It just seems like there is some piece to the puzzle I am missing on getting this stuff to work correctly.

       

      Is there a powershell.org discord where I could ask more rapid fire questions or hire a tutor?

Viewing 3 reply threads
  • You must be logged in to reply to this topic.