JSON convert to csv

Welcome Forums General PowerShell Q&A JSON convert to csv

This topic contains 1 reply, has 2 voices, and was last updated by

 
Participant
7 months, 1 week ago.

  • Author
    Posts
  • #100516

    Participant
    Points: 0
    Rank: Member

    hi,
    I have a json file which i converted to using convertfrom-json and i want selected fields from it. I tried to pipeline the output to select-object but i was able to get the name and not other details. Below is my output of convertfrom-json and my script.

    display_name   : TEL05
    name           : TEL05
    is_muted       : False
    meta           : @{agent_checks=System.Object[]; timezones=System.Object[]; winV=System.Object[]; machine=AMD64; platform=win32; 
                     gohai={"cpu":{"cpu_cores":"12","cpu_logical_processors":"24","family":"6","mhz":"2497","model":"63","model_name":"Intel(R) 
                     Xeon(R) CPU E5-2680 v3 @ 2.50GHz","stepping":"2","vendor_id":"GenuineIntel"},"filesystem":[{"kb_size":"358396","mounted_on":
                     "","name":"\\\\?\\Volume{00241358-f193-11e7-80b3-806e6f6e6963}\\"},{"kb_size":"104499196","mounted_on":"C:\\","name":"\\\\?\
                     \Volume{00241359-f193-11e7-80b3-806e6f6e6963}\\"},{"kb_size":"Unknown","mounted_on":"D:\\","name":"\\\\?\\Volume{0024135d-f1
                     93-11e7-80b3-806e6f6e6963}\\"}],"gohai":{"build_date":"Mon Jun  5 18:30:34 GMT 
                     2017","git_branch":"last-stable","git_hash":"7de20ed","go_version":"go version go1.6.4 windows/amd64"},"memory":{"total":"34
                     261516288"},"network":{"ipaddress":"10.13.52.15","ipaddressv6":"fe80::dcf6:212:7ce0:8feb%16","macaddress":"44-A8-42-3A-9D-E9
                     "},"platform":{"GOOARCH":"amd64","GOOS":"windows","goV":"1.6.4","hostname":"TELXCVOIP04","kernel_name":"Windows","kernel_rel
                     ease":"6.3.9600","machine":"x86_64","os":"Windows Server 2012 R2 Standard","pythonV":"2.7.12"}}; host_id=363974563; 
                     pythonV=2.7.12; processor=Intel64 Family 6 Model 63 Stepping 2, GenuineIntel; agent_version=5.14.0}
    host_name      : TEL05
    has_metrics    : True
    

    I want host name and agent_version(inside meta tag). how do i achieve this?

    i tried below but did not worked.

    $jsonstring = (Get-Content 'servers.JSON') | ConvertFrom-Json
    $jsonstring.rows| select name, meta.agent_version

  • #100522

    Participant
    Points: 200
    Helping Hand
    Rank: Participant

    Maybe someone have a better answer.
    But most likely it's due to the list not being a list of objects and/or part of it is not an object.
    E.g. "Meta" is a nested hashtable and not an object.

    To get around it you could e.g. run it through a foreach loop and pull the information
    You need to replace $rows with e.g. $jsonstring.rows if you want to use that instead.

    foreach($r in $rows)
    {
        $info = @{Name = $r.name; 
                  Agent_version = $r.meta.agent_version
                 }
        
        $info
    }
    

The topic ‘JSON convert to csv’ is closed to new replies.