簡體   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