使用powershell从JSON提取值并分配给新变量

时间:2018-10-01 20:07:25

标签: json powershell

我有下面的JSON文件,我需要从JSON文件中提取值并将其分配给变量。我需要提取ID和fromref显示ID。任何帮助将不胜感激。

{
"size":  2,
"limit":  25,
"isLastPage":  true,
"values":  [
               {
                   "id":  1857,
                   "version":  7,
                   "title":  "Update schema from excel",
                   "description":  "Running Update schema from excel",
                   "state":  "DECLINED",
                   "open":  false,
                   "closed":  true,
                   "createdDate":  1538081524777,
                   "updatedDate":  1538159225677,
                   "closedDate":  1538159225677,
                   "fromRef":  "@{id=refs/heads/schema/2018.5.63107; displayId=schema/2018.5.63107; latestCommit=39be0e339f87c890f67fa6af426c7d96564eae0b; repository=}",
                   "toRef":  "@{id=refs/heads/develop; displayId=develop; latestCommit=b2bedca4a23e7a2f76fae8201b715edc69aaad23; repository=}",
                   "locked":  false,
                   "author":  "@{user=; role=AUTHOR; approved=False; status=UNAPPROVED}",
                   "reviewers":  "             ",
                   "participants":  "",
                   "properties":  "@{mergeResult=; resolvedTaskCount=0; commentCount=1; openTaskCount=0}",
                   "links":  "@{self=System.Object[]}"
               },
               {
                   "id":  1862,
                   "version":  1,
                   "title":  "Update schema from excel",
                   "description":  "Running Update schema from excel",
                   "state":  "DECLINED",
                   "open":  false,
                   "closed":  true,
                   "createdDate":  1538155196583,
                   "updatedDate":  1538159165303,
                   "closedDate":  1538159165303,
                   "fromRef":  "@{id=refs/heads/schema/2018.7.63125; displayId=schema/2018.7.63125; latestCommit=48e827093ac86a86dff58f5215f0ff558ce9da72; repository=}",
                   "toRef":  "@{id=refs/heads/develop; displayId=develop; latestCommit=b2bedca4a23e7a2f76fae8201b715edc69aaad23; repository=}",
                   "locked":  false,
                   "author":  "@{user=; role=AUTHOR; approved=False; status=UNAPPROVED}",
                   "reviewers":  "             ",
                   "participants":  "",
                   "properties":  "@{mergeResult=; resolvedTaskCount=0; openTaskCount=0}",
                   "links":  "@{self=System.Object[]}"
               }
           ],
"start":  0
}

预期输出类似以下内容。

$id =1862
$fromRefbranch.displayid = schema/2018.7.63125

1 个答案:

答案 0 :(得分:1)

可能有更好的解决方案...

$jsonfile = Get-Content C:\json.txt -Raw | ConvertFrom-Json
$values = $jsonfile.values | Select-Object -Property id,@{Label='displayId';Expression={($_.fromRef.Split(';') | Select-String displayId).Line.Split('=')[1]}}