Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge 2 jsons discarding the difference in specific key value #2579

Closed
rjshrjndrn opened this issue May 5, 2023 · 1 comment
Closed

Merge 2 jsons discarding the difference in specific key value #2579

rjshrjndrn opened this issue May 5, 2023 · 1 comment
Labels

Comments

@rjshrjndrn
Copy link

Hi,
I have a weird mere issue. I want to merge 2 json's equal objects disregarding the ID key

{
################ Rule A ################
  "Rules": [
    {
      "Expiration": {
        "Days": "180"
      },
       ######### Discarding this key ################
      "ID": "chact06u6ntc5uagbv30",
      "Filter": {
        "And": {
          "Tags": [
            {
              "Key": "to_delete_in_days",
              "Value": "180"
            }
          ]
        }
      },
      "Status": "Enabled"
    }
  ]
}

{
################ Rule B ################
  "Rules": [
    {
      "Expiration": {
        "ExpiredObjectDeleteMarker": true
      },
      "ID": "chacfnmu6ntc6vi39hg0",
      "Status": "Enabled"
    }
  ], [
    {
      "Expiration": {
        "Days": 180
      },
       ######### Discarding this key ################
      "ID": "custom rule 1",
      "Filter": {
        "And": {
          "Tags": [
            {
              "Key": "to_delete_in_days",
              "Value": "180"
            }
          ]
        }
      },
      "Status": "Enabled"
    }
  ]
}
@emanuele6
Copy link
Member

emanuele6 commented May 5, 2023

It's hard to understand your question because it is not very clear what exactly you want to do. Do the JSON objects come from different files? From another JSON object? Do you want to remove both duplicates or keep one? Do you prefer keeping the id from the first object over the second object, or viceversa, or does it not matter?

Also because your example is not valid JSON (], [), and in one object the expiration days are a "180" string and in the other a 180 numbers, etc.

Anyway, comparing objects ignoring the value of ID is pretty straight forward, you can use a combination of the del/1 function and the unique_by/1 function to do this.

For example, if you have multiple files containing those objects with just a Rules entry and nothing else, and you want to output, a single object with a Rules entry that contains the union of all the Rules, and you don't care about the order of the rules in the output, and if there are duplicates ignoring ID you want to keep only one duplicate (not preferring the ID from a file over another), you would just use:

jq -s 'map(.Rules[]) | { Rules: unique_by(del(.ID)) }' -- file1.json file2.json file3.json

If that does not match your use case, feel free to provide more details.

@itchyny itchyny added the support label Jun 3, 2023
@itchyny itchyny closed this as completed Jun 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants