Jason Templeman
Jason Templeman

Reputation: 478

How can I parse nested JSON in PowerShell?

I'm trying to parse the results of a cURL command and the information I need is in a structure.

I tried getting to the data unsuccessfully and tried converting to PS Object but not sure how to access the structure as I'm new to PS.

Below is a sample of our cURL response.

I have a git commit hash ('c64a568399a572e82c223d55cb650b87ea1c22b8' matches latestCommit in fromRef for entry id 1101) and I need to find the corresponding displayId ('develop' in toRef)

I've done this in Linux using jq but need to replicate this in PS.

jq '.values | map(select(.fromRef.latestCommit=="'"$HASH"'")) | .[0].toRef.displayId'

I'm having 2 issues.

  1. I can get to fromRef but it looks like @{id=refs/heads/feature/add-support; displayId=feature/add-support; latestCommit=c64a568399a572e82c223d55cb650b87ea1c22b8; repository=} and I cannot figure out how to parse
  2. I'm not sure how to get the id so I can find the correct corresponding toRef

Any help would be greatly appreciated.

{
    "size":  15,
    "limit":  20,
    "isLastPage":  true,
    "values":  [
                   {
                       "id":  1101,
                       "version":  0,
                       "title":  "Added header",
                       "description":  "Added notes in header",
                       "state":  "OPEN",
                       "open":  true,
                       "closed":  false,
                       "createdDate":  1595161367863,
                       "updatedDate":  1595161367863,
                       "fromRef":  "@{id=refs/heads/feature/add-support; displayId=feature/add-support; latestCommit=c64a568399a572e82c223d55cb650b87ea1c22b8; repository=}",
                       "toRef":  "@{id=refs/heads/develop; displayId=develop; latestCommit=58b3e3482bb35f3a735048849c2474cc676fbd9b; repository=}",
                       "locked":  false,
                       "author":  "@{user=; role=AUTHOR; approved=False; status=UNAPPROVED}",
                       "reviewers":  "    ",
                       "participants":  "",
                       "properties":  "@{mergeResult=; resolvedTaskCount=0; openTaskCount=0}",
                       "links":  "@{self=System.Object[]}"
                   },
                   {
                       "id":  1053,
                       "version":  4,
                       "title":  "Help with checking,",
                       "description":  "fixed up code.",
                       "state":  "OPEN",
                       "open":  true,
                       "closed":  false,
                       "createdDate":  1591826401310,
                       "updatedDate":  1595018917357,
                       "fromRef":  "@{id=refs/heads/bugfix/checking-2.7; displayId=bugfix/checking-2.7; latestCommit=cf7d8860262c6a46b0b65ef5b6d66ae8cd698b75; repository=}",
                       "toRef":  "@{id=refs/heads/hotfix/2.7_Improvements; displayId=hotfix/2.7_Improvements; latestCommit=01f1100c559ba41ec317421399c3bfb9a0aea91f; repository=}",
                       "locked":  false,
                       "author":  "@{user=; role=AUTHOR; approved=False; status=UNAPPROVED}",
                       "reviewers":  "    ",
                       "participants":  "",
                       "properties":  "@{mergeResult=; resolvedTaskCount=0; commentCount=4; openTaskCount=0}",
                       "links":  "@{self=System.Object[]}"
                   }
               ],
    "start":  0
}

Upvotes: 1

Views: 3727

Answers (1)

Theo
Theo

Reputation: 61068

Once you have converted the result with ConvertTo-Json and the correct -Depth parameter, you can get the values of the returned object quite easily in PowerShell.

Let's say you have used something like $json = $curlResult | ConvertTo-Json -Depth 100, then finding the displayId from the corresponding toRef can be done like this:

# this is the known hashvalue of the `fromRef` value to look for
$latestCommitHash = "c64a568399a572e82c223d55cb650b87ea1c22b8"

# get the value item. from here you can get all other properties belonging to that item
$valueItem = $json.values | Where-Object { $_.fromRef.latestCommit -eq $latestCommitHash }

# get the displayId value of the corresponding 'toRef' element:
$displayId = $valueItem.toRef.displayId

Returns

develop

Upvotes: 2

Related Questions