简体   繁体   English

比较 powershell 中的 JSON 文件

[英]Compare JSON files in powershell

################File-1.json#################### ################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#################### ################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"
  }
}

I have two JSON files with the above contents.我有两个包含上述内容的 JSON 文件。 I need to compare these two files and get differences in powershell.我需要比较这两个文件并获得 powershell 的差异。

  1. Only compare branch names for repos that exists in both files and get the branch names for that respective repo.仅比较两个文件中存在的存储库的分支名称并获取相应存储库的分支名称。

Ex: only compare aaa, bbb and ccc branches and NOT ddd since it does not exist in both files.例如:只比较 aaa、bbb 和 ccc 分支,而不是 ddd,因为它在两个文件中都不存在。

For each repo (aaa, bbb, ccc) value (that exists in both files) in file-1.json I need to compare the respective repo value in File-2.json.对于 file-1.json 中的每个 repo (aaa, bbb, ccc) 值(存在于两个文件中),我需要比较 File-2.json 中的各个 repo 值。

Example: ccc-prod-release-branch (cc is repo name) value is different in File-2.json.示例:ccc-prod-release-branch(cc 是 repo 名称)值在 File-2.json 中不同。 In this case get both branch values and repo name in this case ccc .在这种情况下,在这种情况下获取分支值和 repo 名称ccc

release/S3.3-000000T03发布/S3.3-000000T03

release/S3.3-000000T044发布/S3.3-000000T044

Then use the above values to clone ccc repo and compare these two branches.然后使用上面的值来克隆 ccc repo 并比较这两个分支。 I need a way to do this in powershell.我需要一种在 powershell 中执行此操作的方法。

For ddd-current-release-branch branch since it is new I want to just run a clone and get all the commits in that branch.对于ddd-current-release-branch分支,因为它是新分支,我只想运行一个克隆并获取该分支中的所有提交。

How can I do this in powershell script.如何在 powershell 脚本中执行此操作。 ? ?

Here's a decent starting point.这是一个不错的起点。 I'm making some assumptions about your real naming conventions and such:我正在对您的真实命名约定等做出一些假设:

# 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}

So convert the awkward json to lists of powershell objects:因此,将笨拙的 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

Then it's much easier to compare them however you need:然后,根据需要比较它们会容易得多:

# 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