0

There is a JSON URL which produces dynamic content, and there is one particular part of the JSON URL which I am trying to separate two values which have no title or name associated to them other than the parent title (accountUsage) and give them each a unique title, which I can then call upon in PowerShell.

Any ideas how to achieve this?

I need to convert this

accountUsage : @{10.10.2018=5; 09.10.2018=0; 08.10.2018=0; 07.10.2018=0; 06.10.2018=0; 05.10.2018=8; 04.10.2018=1; 03.10.2018=0; 
           02.10.2018=0; 01.10.2018=0}

Into this:

date
----
10.10.2018

value
----
5

date
----
09.10.2018

value
----
0
Moulton
  • 37
  • 1
  • 5

1 Answers1

1
$json = '{"accountUsage":{"06.10.2018":0,"09.10.2018":0,"04.10.2018":1,"08.10.2018":0,"02.10.2018":0,"07.10.2018":0,"03.10.2018":0,"05.10.2018":8,"10.10.2018":5,"01.10.2018":0}}'
$data = $json | ConvertFrom-Json

$data.accountUsage | Get-Member -MemberType NoteProperty | ForEach-Object {
    $key = $_.Name
    [PSCustomObject]@{
        date = $key
        value = $data.accountUsage.$key
    }
}

gives me a list of date/value pairs:

date       value
----       -----
06.10.2018     0
09.10.2018     0
04.10.2018     1
08.10.2018     0
02.10.2018     0
07.10.2018     0
03.10.2018     0
05.10.2018     8
10.10.2018     5
01.10.2018     0

See this earlier answer of mine for some more insight into this.

Tomalak
  • 332,285
  • 67
  • 532
  • 628
  • 1
    Be aware that `date` will not be sorted initially. To do that, you would need to convert the date string into an *actual* date (using `[DateTime]::ParseExact()`, some info on how to do that is [here](http://dusan.kuzmanovic.net/2012/05/07/powershell-parsing-date-and-time/)) and then pipe through `Sort-Object date`. – Tomalak Oct 10 '18 at 18:11