如何找出一个JSON文件中不存在于另一个JSON文件中的id?
Got it, let's break this down step by step so you can track down those missing IDs easily. The core idea is to extract all IDs from both datasets, then find the ones that only exist in your first JSON. Here's how to do it with practical code examples in two common languages:
Step-by-Step Approach
- Parse the JSON data: Convert the raw JSON strings (or files) into usable objects/dictionaries in your language of choice.
- Extract IDs from the first JSON: Gather all
idvalues you want to check. - Extract all relevant IDs from the second JSON: Dig into the
responsearray and collect every ID from theidslists. - Find the difference: Filter the first set of IDs to keep only those that don't appear in the second set.
Example 1: JavaScript (Node.js or Browser)
Let's assume your first JSON might be an array of objects (adjust if it's a single object):
// Sample data (or load from files using fs in Node.js) const firstJsonData = [ { "id": "TestLinkOne", "displayText": "Test Link Text" }, { "id": "TestLinkThree", "displayText": "Another Link" } ]; const secondJsonData = { "id": "Test", "response": [ { "text": "Hello", "ids": [ "TestLinkOne", "TestLinkTwo" ] }, { "text": "Hi", "ids": [ "TestLinkFour" ] } ] }; // 1. Extract IDs from first JSON const firstIds = firstJsonData.map(item => item.id); // If first JSON is a single object: const firstIds = [firstJsonData.id]; // 2. Extract all IDs from second JSON's response.ids arrays const secondAllIds = secondJsonData.response.flatMap(item => item.ids); // 3. Use a Set for fast lookups const secondIdSet = new Set(secondAllIds); // 4. Find IDs from first JSON not present in the second const missingIds = firstIds.filter(id => !secondIdSet.has(id)); console.log(missingIds); // Output: ["TestLinkThree"]
If you're loading from files in Node.js, add this to read the JSON:
const fs = require('fs'); const firstJsonData = JSON.parse(fs.readFileSync('./first.json', 'utf8')); const secondJsonData = JSON.parse(fs.readFileSync('./second.json', 'utf8'));
Example 2: Python
Python makes this straightforward with list comprehensions and sets:
import json # Sample data (or load from files) first_json_data = [ {"id": "TestLinkOne", "displayText": "Test Link Text"}, {"id": "TestLinkThree", "displayText": "Another Link"} ] second_json_data = { "id": "Test", "response": [ {"text": "Hello", "ids": ["TestLinkOne", "TestLinkTwo"]}, {"text": "Hi", "ids": ["TestLinkFour"]} ] } # 1. Extract IDs from first JSON first_ids = [item["id"] for item in first_json_data] # If first JSON is a single object: first_ids = [first_json_data["id"]] # 2. Extract all IDs from second JSON's response.ids arrays second_all_ids = [id for resp_item in second_json_data["response"] for id in resp_item["ids"]] # 3. Use a set for efficient lookups second_id_set = set(second_all_ids) # 4. Find missing IDs missing_ids = [id for id in first_ids if id not in second_id_set] print(missing_ids) # Output: ['TestLinkThree']
To load from JSON files instead of hardcoding:
with open('first.json', 'r') as f: first_json_data = json.load(f) with open('second.json', 'r') as f: second_json_data = json.load(f)
This approach works regardless of how many entries are in either JSON—just adjust the extraction steps if your actual JSON structure varies slightly from the examples.
内容的提问来源于stack exchange,提问作者James McKinney

