Home > Software engineering >  Fetch data from JSON with powershell
Fetch data from JSON with powershell

Time:11-09

I have without success tried to figure out how to most efficiently fetch data with powershell from the below JSON that is returned to me by a REST API:

    {
  "$schema": "api:standardResponse",
  "links": [
    {
      "rel": "canonical",
      "href": "http://localhost:8501/services/v2/replicats",
      "mediaType": "application/json"
    },
    {
      "rel": "self",
      "href": "http://localhost:8501/services/v2/replicats",
      "mediaType": "application/json"
    },
    {
      "rel": "describedby",
      "href": "http://localhost:8501/services/v2/metadata-catalog/replicats",
      "mediaType": "application/schema json"
    }
  ],
  "messages": [],
  "response": {
    "$schema": "ogg:collection",
    "items": [
      {
        "links": [
          {
            "rel": "parent",
            "href": "http://localhost:8501/services/v2/replicats",
            "mediaType": "application/json"
          },
          {
            "rel": "canonical",
            "href": "http://localhost:8501/services/v2/replicats/RNIMDA00",
            "mediaType": "application/json"
          }
        ],
        "$schema": "ogg:collectionItem",
        "name": "RNIMDA00"
      },
      {
        "links": [
          {
            "rel": "parent",
            "href": "http://localhost:8501/services/v2/replicats",
            "mediaType": "application/json"
          },
          {
            "rel": "canonical",
            "href": "http://localhost:8501/services/v2/replicats/RNIMDA01",
            "mediaType": "application/json"
          }
        ],
        "$schema": "ogg:collectionItem",
        "name": "RNIMDA01"
      },
      {
        "links": [
          {
            "rel": "parent",
            "href": "http://localhost:8501/services/v2/replicats",
            "mediaType": "application/json"
          },
          {
            "rel": "canonical",
            "href": "http://localhost:8501/services/v2/replicats/RNIMDA02",
            "mediaType": "application/json"
          }
        ],
        "$schema": "ogg:collectionItem",
        "name": "RNIMDA02"
      }
    ]
  }
}

I only need the data from the "name": node, and the data from the "href": node.

I have done some searching and found som examples where the JSON is converted with ConvertFrom-Json and then iterated with a foreach like the pseudo code below:

$users = $response | ConvertFrom-Json
foreach ($user in $users)

{

write-host "$($user.name) has the email: $($user.email)"

}

But I wonder if there is a better way of fetching data with powershell from a object that contains JSON.

Thanks :)

CodePudding user response:

It is not really clear what your desired output should be.

Let's assume you have the JSON data converted in a variable $json using $json = $response | ConvertFrom-Json

Then if you do:

$json.response.items | Select-Object name, @{Name = 'href'; Expression = {$_.links.href}}

You will get objects with a name and a href property, where the href will be an array.

PowerShell would output this on screen as

name     href                                                                                               
----     ----                                                                                               
RNIMDA00 {http://localhost:8501/services/v2/replicats, http://localhost:8501/services/v2/replicats/RNIMDA00}
RNIMDA01 {http://localhost:8501/services/v2/replicats, http://localhost:8501/services/v2/replicats/RNIMDA01}
RNIMDA02 {http://localhost:8501/services/v2/replicats, http://localhost:8501/services/v2/replicats/RNIMDA02}

If however you would like to return an object for each of the href values inside the links nodes you can do:

$json.response.items | ForEach-Object {
    $name = $_.name
    foreach ($link in $_.links) {
        [PsCustomObject]@{
            name = $name
            href = $link.href
       }
    }
}

which will output

name     href                                                
----     ----                                                
RNIMDA00 http://localhost:8501/services/v2/replicats         
RNIMDA00 http://localhost:8501/services/v2/replicats/RNIMDA00
RNIMDA01 http://localhost:8501/services/v2/replicats         
RNIMDA01 http://localhost:8501/services/v2/replicats/RNIMDA01
RNIMDA02 http://localhost:8501/services/v2/replicats         
RNIMDA02 http://localhost:8501/services/v2/replicats/RNIMDA02

CodePudding user response:

jq is what you need.

Just install the windows version, add it to your PATH (depending on how you installed it) and use the following command:

curl -s "https://your-url" | jq -r '.response.items.links.href'

  • Related