使用jq/bash比较两个JSON文件并仅保留匹配项

m1m5dgzv  于 2023-01-03  发布在  其他
关注(0)|答案(1)|浏览(125)

我有两个文件(我只是复制他们的一小部分,因为他们是相当大):
_标题2.txt:(小样本)

{
  "titleId": "0100000000010800"
}
{
  "titleId": "010000000EEF0800"
}
{
  "titleId": "0100000011D90800"
}
{
  "titleId": "010000001260E800"
}
{
  "titleId": "01000000160F6800"
}
{
  "titleId": "010000100C4B8800"
}

titles.json(同样只是一个小示例)

{
    "1": {
        "bannerUrl": null,
        "category": null,
        "description": null,
        "developer": null,
        "frontBoxArt": null,
        "iconUrl": null,
        "id": "0100000000000816",
        "intro": null,
        "isDemo": null,
        "key": null,
        "languages": null,
        "name": null,
        "nsuId": null,
        "numberOfPlayers": null,
        "publisher": null,
        "rank": 34342,
        "rating": null,
        "ratingContent": null,
        "region": null,
        "regions": null,
        "releaseDate": null,
        "rightsId": null,
        "screenshots": null,
        "size": 0,
        "version": null
    },
    "2": {
        "bannerUrl": "https://img-eshop.cdn.nintendo.net/i/c42553b4fd0312c31e70ec7468c6c9bccd739f340152925b9600631f2d29f8b5.jpg",
        "category": [
            "Platformer",
            "Action"
        ],
        "description": "Explore incredible places far from the Mushroom Kingdom as you join Mario and his new ally Cappy on a massive, globe-trotting 3D adventure. Use amazing new abilities\u2014like the power to capture and control objects, animals, and enemies\u2014to collect Power Moons so you can power up the Odyssey airship and save Princess Peach from Bowser\u2019s wedding plans!\n\nThanks to heroic, hat-shaped Cappy, Mario\u2019s got new moves that\u2019ll make you rethink his traditional run-and-jump gameplay\u2014like cap jump, cap throw, and capture. Use captured cohorts such as enemies, objects, and animals to progress through the game and uncover loads of hidden collectibles. And if you feel like playing with a friend, just pass them a Joy-Con\u2122 controller! Player 1 controls Mario while Player 2 controls Cappy. This sandbox-style 3D Mario adventure\u2014the first since 1996\u2019s beloved Super Mario 64\u2122 and 2002\u2019s Nintendo GameCube\u2122 classic Super Mario Sunshine\u2122\u2014is packed with secrets and surprises, plus exciting new kingdoms to explore.",
        "developer": null,
        "frontBoxArt": null,
        "iconUrl": "https://img-eshop.cdn.nintendo.net/i/ad4d31f664a1ce704f0219da2805f8459595bc3c01c3f04df2e32ba34a05b8c6.jpg",
        "id": "0100000000010000",
        "intro": "Embark on a cap-tivating, globe-trotting adventure",
        "isDemo": false,
        "key": null,
        "language": "en",
        "languages": [
            "ja",
            "en",
            "es",
            "fr",
            "de",
            "it",
            "nl",
            "ru",
            "zh",
            "zh"
        ],
        "name": "Super Mario Odyssey\u2122",
        "nsuId": 70010000001130,
        "numberOfPlayers": 2,
        "publisher": "Nintendo",
        "rank": 2,
        "rating": 10,
        "ratingContent": [
            "Cartoon Violence",
            "Comic Mischief"
        ],
        "region": "US",
        "regions": [
            "CO",
            "AR",
            "CL",
            "PE",
            "KR",
            "HK",
            "NZ",
            "BE",
            "CZ",
            "DK",
            "ES",
            "FI",
            "HU",
            "NL",
            "PL",
            "RU",
            "ZA",
            "MX",
            "CA",
            "FR",
            "DE",
            "JP",
            "AU",
            "GB",
            "US"
        ],
        "releaseDate": 20171027,
        "rightsId": "01000000000100000000000000000003",
        "screenshots": [
            "https://img-eshop.cdn.nintendo.net/i/c497547957d9dd3668e891aa97ff4899a3f40bd1bd430020f8cbdf673f02bdeb.jpg",
            "https://img-eshop.cdn.nintendo.net/i/1839d571921e3fb19ef48da64c145cb8ce573b07d7390c6350f15291b3905048.jpg",
            "https://img-eshop.cdn.nintendo.net/i/d0510e4804381287c64dc3ab374ae9273419e263e9a732e72af3b39cd70f2b5f.jpg",
            "https://img-eshop.cdn.nintendo.net/i/ab713d841b0f0d9bbce38b589d75dd3bba2aef005418e59d3f0fd95389ae7016.jpg",
            "https://img-eshop.cdn.nintendo.net/i/abcf9708cc28ef5b7ab412aa17fafe4ad181f0b957456ffd9b83de2561d9f62a.jpg",
            "https://img-eshop.cdn.nintendo.net/i/7db70ecd883f94d935f65b9f4454b8151b6f8be7c4be95543e87488e48e5a6b9.jpg"
        ],
        "size": 6026166272,
        "version": 0
    },
    "3": {
        "bannerUrl": null,
        "category": null,
        "description": null,
        "developer": null,
        "frontBoxArt": null,
        "iconUrl": null,
        "id": "0100000000010800",
        "intro": null,
        "isDemo": null,
        "key": null,
        "languages": null,
        "name": null,
        "nsuId": null,
        "numberOfPlayers": null,
        "publisher": null,
        "rank": 34341,
        "rating": null,
        "ratingContent": null,
        "region": null,
        "regions": null,
        "releaseDate": null,
        "rightsId": null,
        "screenshots": null,
        "size": 0,
        "version": 262144
    },
    "4": {
        "bannerUrl": null,
        "category": null,
        "description": null,
        "developer": null,
        "frontBoxArt": null,
        "iconUrl": null,
        "id": "010000000E5EE000",
        "intro": null,
        "isDemo": null,
        "key": null,
        "languages": null,
        "name": null,
        "nsuId": null,
        "numberOfPlayers": null,
        "publisher": null,
        "rank": 34340,
        "rating": null,
        "ratingContent": null,
        "region": null,
        "regions": null,
        "releaseDate": null,
        "rightsId": null,
        "screenshots": null,
        "size": 0,
        "version": 0
    },
    "5": {
        "bannerUrl": "https://img-eshop.cdn.nintendo.net/i/74dbbc2f5dd60e8c671a0c0a1ad18034e80e26375589765e90449ea4b5e15739.jpg",
        "category": [
            "Adventure",
            "Action",
            "Simulation"
        ],
        "description": "Explore a darkened office complex and uncover the truth behind the horrifying events that have befallen its inhabitants.\nPlay as one of two characters with unique storylines, monster encounters, and gameplay challenges. \nNavigate a multi-story maze that grows scarier, harder, and more twisted as you struggle to escape the madness.",
        "developer": null,
        "frontBoxArt": null,
        "iconUrl": "https://img-eshop.cdn.nintendo.net/i/4699a0d5b05a057a5d20c252d2d46a7eec5d41cfd46eb33e4e87b6247b3f7486.jpg",
        "id": "010000000EEF0000",
        "intro": "Shadows 2: Perfidia is a survival horror game inspired by classics like the Penumbra series and Layers of Fear.",
        "isDemo": false,
        "key": null,
        "language": "en",
        "languages": [
            "en"
        ],
        "name": "Shadows 2: Perfidia",
        "nsuId": 70010000021900,
        "numberOfPlayers": 1,
        "publisher": "Ultimate Games",
        "rank": 2963,
        "rating": 17,
        "ratingContent": [
            "Blood and Gore",
            "Intense Violence",
            "Nudity",
            "Sexual Content",
            "Strong Language"
        ],
        "region": "US",
        "regions": [
            "BE",
            "CZ",
            "DK",
            "ES",
            "FI",
            "HU",
            "NL",
            "PL",
            "RU",
            "ZA",
            "MX",
            "CA",
            "FR",
            "DE",
            "AU",
            "GB",
            "US"
        ],
        "releaseDate": 20190806,
        "rightsId": null,
        "screenshots": [
            "https://img-eshop.cdn.nintendo.net/i/a40b9ad80b4d6ab05c2ebc5dbf16484e4074a3a2b32b1ca15505dde5d56ebdd5.jpg",
            "https://img-eshop.cdn.nintendo.net/i/4cd32eaf41ee287b4e9854646598c5b2feaf405d431977cf8f5002ef5a3fa428.jpg",
            "https://img-eshop.cdn.nintendo.net/i/e2547655c002df78f573e1a79afd39dc70defce4d7e33fb3b68820fb5468d89d.jpg",
            "https://img-eshop.cdn.nintendo.net/i/85d23d86660204f1f6fb14feece746e12860ce26944cff47ca407b53e8f2c686.jpg",
            "https://img-eshop.cdn.nintendo.net/i/e499c7e25643ed0366cfdcff9c14da9cc5a528f841aba9be7188b531ef2fe219.jpg",
            "https://img-eshop.cdn.nintendo.net/i/6b36b07122f2a4b8ac34df656159ce2e4020a6c19c06f8cdcafc221c9b4b5598.jpg"
        ],
        "size": 2687500288,
        "version": 0
    },

使用jq,我想测试_titles2.txt中的每个对象“titleId”是否作为“id”存在于titles.json中。如果确实存在,我想使用所有“匹配”创建第三个json文件。
我可以用一个'titleId'来完成这个任务,但似乎不知道如何对整个文件执行此操作(除了执行一个巨大的for循环并分别测试每个文件)
希望有人能帮上忙!谢谢。
谢谢peak的回答!工作完美,搜索时间约15秒!
我希望你能帮我修改这个命令,我有完全相同的任务要做,两个文件的格式与_titles2.txt相同(除了大写)
_titles3.txt

{
    "TITLEID": "0100000000010000"
  }
  {
    "TITLEID": "0100000000010800"
  }
  {
    "TITLEID": "010000000E5EE000"
  }
  {
    "TITLEID": "010000000EEF0000"
  }
  {
    "TITLEID": "010000000EEF0800"
  }

简单回顾一下-使用jq,我想测试_titles2.txt中的每个对象'titleId'是否在_titles3.txt中以'TITLEID'的形式存在。如果存在,我想创建第三个json文件,其中包含所有“匹配”。(相同的结果,但我在让命令工作时遇到了问题,因为它不再是一个大对象-我也无法控制数据,所以我只能处理我得到的数据)

cyvaqqii

cyvaqqii1#

如果我正确理解了这个问题,那么就没有必要多次调用jq,或者编写shell循环。
例如,如果您的目标是生成单个JSON对象,则可以使用:

< titles.json jq --slurpfile titleIds _titles2.txt '
  INDEX($titleIds[]; .titleId) as $dict
  | with_entries( select($dict[.value.id]))
'

--
对于后续问题:您可以通过调用INDEX来处理这两种情况,如下所示:

INDEX($titleIds[]; .titleId//.TITLEID)

相关问题