简体   繁体   中英

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

I have two JSON files with the above contents. I need to compare these two files and get differences in 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.

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.

Example: ccc-prod-release-branch (cc is repo name) value is different in File-2.json. In this case get both branch values and repo name in this case ccc .

release/S3.3-000000T03

release/S3.3-000000T044

Then use the above values to clone ccc repo and compare these two branches. I need a way to do this in 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.

How can I do this in powershell script. ?

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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