在 powershell 中比较 JSON 文件

Posted

技术标签:

【中文标题】在 powershell 中比较 JSON 文件【英文标题】:Compare JSON files in powershell 【发布时间】:2022-01-18 00:53:17 【问题描述】:

################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中获得差异。

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

例如:只比较 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 脚本中执行此操作。 ?

【问题讨论】:

【参考方案1】:

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

# 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 

【讨论】:

非常感谢上面的回答。抱歉,我有健康问题,所以迟到了评论。 一个后续问题 - 如何比较仅存在于 file1.json 中的分支的值。在这种情况下,只比较 file2.json 中 aaa、bbb 和 ccc 的值。分支 ddd 应该被忽略。只应克隆 ccc 分支。 @Spartan87 我认为您想要的基本结果将通过过滤您想要与where-object 比较的对象来完成,例如:Compare-Object $prod ($current | where-object repoName -ne 'ddd') -IncludeEqual -PassThru 感谢您的回复。我的想法是仅将 file.json 中的 repo 名称的值与 file2.json 中的相应 repo 名称进行比较。在这种情况下,比较后的 ccc 分支值不同,所以我想克隆 ccc repo 并比较分支(在 file.json 和 file2.json 中)以获得不同的提交。如果 file2.json 中有一个较新的 repo 在 file1.json 中不存在,那么我想忽略它而不是比较它。感谢您在这方面的帮助。

以上是关于在 powershell 中比较 JSON 文件的主要内容,如果未能解决你的问题,请参考以下文章

从 powershell 中的 JSON 文件中提取子字符串

在 Powershell 中提取 json 问题

使用 PowerShell 脚本从 Azure Blob 存储读取 JSON 文件并写回 Blob 存储中的另一个文件

野路子出身PowerShell 文件操作实用功能

PowerShell重命名文件夹中的文件For循环错误

如何在powershell中解析json响应