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

1 year, 1 month ago.

  • Author
  • #100516

    Topics: 10
    Replies: 3
    Points: 11
    Rank: Member

    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; 
                     Xeon(R) CPU E5-2680 v3 @ 2.50GHz","stepping":"2","vendor_id":"GenuineIntel"},"filesystem":[{"kb_size":"358396","mounted_on":
                     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
                     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

    Topics: 12
    Replies: 220
    Points: 407
    Helping Hand
    Rank: Contributor

    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

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