Search code examples
powershell-3.0

Notation for accessing JSON


I have zero experience in Powershell but wanted to deploy a runbook in Azure that's triggered by a webhook that sends a JSON file.

I have the logic worked out but it's in Python before I realized that I had to do this in Powershell and so I'm lost on the proper way to access values based off of known keys.

The JSON schema stays the same and is such:

  "specversion": "1.0",
  "id": "497f6eca-6276-4993-bfeb-53cbbbba6f08",
  "source": "https://quartzy.com",
  "type": "com.quartzy.inventory-item.created",
  "datacontenttype": "application/json",
  "time": "2019-08-24T14:15:22Z",
  "data": {
    "inventory_item": {
      "id": "497f6eca-6276-4993-bfeb-53cbbbba6f08",
      "name": "string",
      "vendor": "string",
      "catalog_number": "string",
      "price": "string",
      "unit_size": "string",
      "quantity": "string",
      "url": "string",
      "technical_details": "string",
      "expiration_date": "2019-08-24",
      "auto_reminder": "string",
      "lot_number": "string",
      "cas_number": "string",
      "vendor_product_id": "afd775e3-efaf-419a-bc05-aa8f21d6286a",
      "lab": {
        "id": "497f6eca-6276-4993-bfeb-53cbbbba6f08",
        "name": "string",
        "organization": {
          "id": "497f6eca-6276-4993-bfeb-53cbbbba6f08",
          "name": "string"
        }
      },
      "type": {
        "id": "497f6eca-6276-4993-bfeb-53cbbbba6f08",
        "name": "string",
        "lab": {
          "id": "497f6eca-6276-4993-bfeb-53cbbbba6f08",
          "name": "string",
          "organization": {
            "id": "497f6eca-6276-4993-bfeb-53cbbbba6f08",
            "name": "string"
          }
        }
      },

      "location": {
        "name": "string"
      },
      "subids": [
          12,
          16
          04
      ]
      "sublocation": {
        "name": "string"
      },
      "added_by": {
        "id": "497f6eca-6276-4993-bfeb-53cbbbba6f08",
        "first_name": "string",
        "last_name": "string",
        "email": "[email protected]"
      },
      "updated_by": {
        "id": "497f6eca-6276-4993-bfeb-53cbbbba6f08",
        "first_name": "string",
        "last_name": "string",
        "email": "[email protected]"
      },
      "app_url": "http://example.com"
    }
  }
}

And so I know the key values and just need to do the equivalent of this python snippet:

substance = json["data"]["inventory_item"]["name"]
vendor = json["data"]["inventory_item"]["vendor"]
sid = json["data"]["subids"][1]
#other variables that come from navigating the json.

The answers I've found on here seem to involve looping through the json and using PsObject.Properties but that seems somewhat cluttered for something that other languages can do in a single line or two. Is there a specific notation for accessing explicit key values?


Solution

  • After cleaning up and sub-setting your example data you can use the following syntax to access your known items.

    Sample Data:

    {"specversion": "1.0",
    "id":"497f6eca-6276-4993-bfeb-53cbbbba6f08",
    "source":"https://quartzy.com",
    "type":"com.quartzy.inventory-item.created",
    "datacontenttype":"application/json",
    "time":"2019-08-24T14:15:22Z",
    "data":{
    "inventory_item":{
    "id":"497f6eca-6276-4993-bfeb-53cbbbba6f08",
    "name":"Inventory Item Name Found",
    "vendor":"Mickey Mouse",
    "catalog_number":"string",
    "price":"string",
    "unit_size":"string",
    "quantity":"string",
    "url":"string",
    "technical_details":"string",
    "expiration_date":"2019-08-24",
    "auto_reminder":"string",
    "lot_number":"string",
    "cas_number":"string",
    "vendor_product_id":"afd775e3-efaf-419a-bc05-aa8f21d6286a",
    "lab":{
    "id":"497f6eca-6276-4993-bfeb-53cbbbba6f08",
    "name":"string",
    "organization":{
    "id":"497f6eca-6276-4993-bfeb-53cbbbba6f08",
    "name":"string"
    }
    }}}}
    

    Code:

    Clear-Host
    $MyJson =  ConvertFrom-Json (Get-Content -raw -path "G:\BEKDocs\Scripts\Test\Test.json")
    $Substance = $MyJson.Data.Inventory_Item.Name
    $Vendor    = $MyJson.Data.Inventory_Item.vendor
    
    "Substance`t= $Substance`nVendor`t`t= $Vendor"
    

    Output:

    Substance   = Inventory Item Name Found
    Vendor      = Mickey Mouse
    PS>