繁体   English   中英

比较 powershell 中的 JSON 文件

[英]Compare JSON files in powershell

################File-1.json####################

{
      "aaa-prod-release-branch": {
        "value": "release/S1.1-000000T01"
      },
      "bbb-prod-release-branch": {
        "value": "release/S2.2-000000T02"
      },
      "ccc-prod-release-branch": {
        "value": "release/S3.3-000000T03"
      }
    }

################File-2.json####################

{
  "aaa-current-release-branch": {
    "value": "release/S1.1-000000T01"
  },
  "bbb-current-release-branch": {
    "value": "release/S2.2-000000T02"
  },
  "ccc-current-release-branch": {
    "value": "release/S3.3-000000T044"
  },
  "ddd-current-release-branch": {
    "value": "releases/R4.4-000000T04"
  }
}

我有两个包含上述内容的 JSON 文件。 我需要比较这两个文件并获得 powershell 的差异。

  1. 仅比较两个文件中存在的存储库的分支名称并获取相应存储库的分支名称。

例如:只比较 aaa、bbb 和 ccc 分支,而不是 ddd,因为它在两个文件中都不存在。

对于 file-1.json 中的每个 repo (aaa, bbb, ccc) 值(存在于两个文件中),我需要比较 File-2.json 中的各个 repo 值。

示例:ccc-prod-release-branch(cc 是 repo 名称)值在 File-2.json 中不同。 在这种情况下,在这种情况下获取分支值和 repo 名称ccc

发布/S3.3-000000T03

发布/S3.3-000000T044

然后使用上面的值来克隆 ccc repo 并比较这两个分支。 我需要一种在 powershell 中执行此操作的方法。

对于ddd-current-release-branch分支,因为它是新分支,我只想运行一个克隆并获取该分支中的所有提交。

如何在 powershell 脚本中执行此操作。 ?

这是一个不错的起点。 我正在对您的真实命名约定等做出一些假设:

# Get your json text as an object
$json1 = Get-Content '.\file1.json' | ConvertFrom-Json
$json2 = Get-Content '.\file2.json' | ConvertFrom-Json

# Your json example comes in as a single object with each repo as a property, which isn't what you want:

aaa-prod-release-branch         bbb-prod-release-branch         ccc-prod-release-branch        
-----------------------         -----------------------         -----------------------        
@{value=release/S1.1-000000T01} @{value=release/S2.2-000000T02} @{value=release/S3.3-000000T03}

因此,将笨拙的 json 转换为 powershell 对象列表:

$prod = Foreach ($repo in $json1.psObject.Properties) {
  [pscustomobject][ordered]@{
    repoName    = $repo.Name -replace '-prod-release-branch'
    branchName  = $repo.Name
    releaseName = $repo.Value.Value
}}
$current = Foreach ($repo in $json2.psObject.Properties) {
  [pscustomobject][ordered]@{
    repoName    = $repo.Name -replace '-current-release-branch'
    branchName  = $repo.Name
    releaseName = $repo.Value.Value
}}

# Outputs:

repoName branchName              releaseName           
-------- ----------              -----------           
aaa      aaa-prod-release-branch release/S1.1-000000T01
bbb      bbb-prod-release-branch release/S2.2-000000T02
ccc      ccc-prod-release-branch release/S3.3-000000T03

然后,根据需要比较它们会容易得多:

# Example: list all differences
Compare-Object $prod $current -Property 'ReleaseName' -IncludeEqual -PassThru

repoName branchName                 releaseName             SideIndicator
-------- ----------                 -----------             -------------
aaa      aaa-prod-release-branch    release/S1.1-000000T01  ==           
bbb      bbb-prod-release-branch    release/S2.2-000000T02  ==           
ccc      ccc-current-release-branch release/S3.3-000000T044 =>           
ccc      ccc-prod-release-branch    release/S3.3-000000T03  <=           
ddd      ddd-current-release-branch releases/R4.4-000000T04 =>           

# Example: repos that need full clone
$ToClone = $current | Where { $_.repoName -NotIn $prod.repoName }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM