Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with Powershell -json

Status
Not open for further replies.

jedraw

Technical User
Feb 1, 2003
206
CA
I am new to Powershell.
I have the following script. I am trying to determine the powershell "code" to identify which, if any, "field" is an array or hashtable.
Code:
$txt = @"
{
  "id": "02002010",
  "booktitle": "",
  "pagetitle": "Demo Page",
  "parent": "02002000",
  "img": [
    {
      "imgfile": "02A.png",
      "imgname": "02A.png"
    }
  ],
  "fmt": "",
  "entries": [
    {
      "itemid": "1",
      "partnumber": "1234567",
      "partdescription": "Washer",
      "partqty": "2",
      "Manufacturer": "ACME",
	  "TYPE": "Stainless",
      "partdescriptionlocal": "Washer"
    },
    {
      "itemid": "2",
      "partnumber": "98765-B",
      "partdescription": "Screw",
      "partqty": "8",
      "Manufacturer": "Widget Inc",
      "TYPE": "Galv",
      "partdescriptionlocal": "Screw"
    }]
}
"@
$json=  ConvertFrom-Json -inputobject $txt
foreach($pct in $json) { 
$pct}

The result is
Code:
id        : 02002010
booktitle : 
pagetitle : Demo Page
parent    : 02002000
img       : {@{imgfile=02A.png; imgname=02A.png}}
fmt       : 
entries   : {@{itemid=1; partnumber=1234567; partdescription=Washer; partqty=2; Manufacturer=ACME; TYPE=Stainless; 
            partdescriptionlocal=Washer}, @{itemid=2; partnumber=98765-B; partdescription=Screw; partqty=8; 
            Manufacturer=Widget Inc; TYPE=Galv; partdescriptionlocal=Screw}}

What property/parameter??? identifies img, and entries as "fields" whose values are arrays???
I'm trying to identify such fields (subarrays) without explicitly knowing each and every field name.

Thanks in advance.

UPDATE: I have found an answer to this.

Code:
$json=  ConvertFrom-Json -inputobject $txt
ForEach($pct in $json) { 
$pct.psobject.properties.name | 
foreach-object {
  [PSCustomObject]@{
   Property = $_
   Type = $pct.$_.gettype().basetype
   }
 }
 }

which gives this result:

Code:
Property  Type         
--------  ----         
id        System.Object
booktitle System.Object
pagetitle System.Object
parent    System.Object
img       System.Array 
fmt       System.Object
entries   System.Array


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top