0

I need to get the index number for an item in array using the items value. I can't seem to see in the documentation how to achieve this.

$json = New-Object Chilkat.JsonObject

$jsonStr = "{ `"id`": 1, `"name`": `"A green door`", `"tags`": [`"home`", 22, `"green`"], `"price`": 125 }"

$success = $json.Load($jsonStr)
if ($success -ne $true) {
    $($json.LastErrorText)
    exit
}

#  Get the "tags" array, which contains "home", 22, "green"
$tagsArray = $json.ArrayOf("tags")
if ($tagsArray -eq $null ) {
    $("tags member not found.")
    exit
}
user1513388
  • 7,165
  • 14
  • 69
  • 111

1 Answers1

0

Use the JSON code generator at http://tools.chilkat.io/jsonParse.cshtml

For example, if you past the following into the online tool:

{ "id": 1, "name": "A green door", "tags": ["home", 22, "green"], "price": 125 }

You'll get

$json = New-Object Chilkat.JsonObject
#  Insert code here to load the above JSON into the json object.
$id = $json.IntOf("id")
$name = $json.StringOf("name")
$price = $json.IntOf("price")
$i = 0
$count_i = $json.SizeOfArray("tags")
while ($i -lt $count_i) {
    $json.I = $i
    $strVal = $json.StringOf("tags[i]")
    $i = $i + 1
}
Chilkat Software
  • 1,405
  • 1
  • 9
  • 8