You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

从嵌套Elasticsearch文档提取特定字段:丹麦公司API数据处理

Hey there! Let's walk through how to extract the list of business owners and directors from that Danish business API you're accessing. The nested structure sounds a bit tricky, but breaking it down step by step makes it manageable. Here's a practical approach with code examples:

Step 1: Map Out the Nested Data Structure

First, let's recap the hierarchy you mentioned to make sure we're aligned:

  • Top level: List of organization entries
  • Each organization has an attributes array
  • Each attribute in attributes includes a type field and a values array
  • We only care about attributes where:
    1. The type field exactly matches the string 'function'
    2. The first element in the values array falls into your predefined whitelist (e.g., Danish terms for owner/director roles)

Step 2: Define Your Role Whitelist

Start by listing the exact values that correspond to owners and directors in the API. Since this is a Danish business API, these will likely be Danish terminology. For example:

# Adjust this set based on actual values from the API response
ROLE_WHITELIST = {'Ejer', 'Bestyrelsesmedlem', 'Formand', 'Næstformand', 'Deltager i bestyrelsen'}

Step 3: Traverse and Extract the Target Data

Here's a Python example that safely iterates through the nested structure, handling edge cases like missing fields or empty arrays to avoid crashes:

def extract_owners_directors(api_response):
    extracted_data = []
    
    # Loop through each organization in the API response
    for org in api_response.get('organizations', []):
        org_name = org.get('name', 'Unnamed Organization')
        org_attributes = org.get('attributes', [])
        
        # Loop through each attribute of the current organization
        for attr in org_attributes:
            # Skip attributes that aren't of type 'function'
            if attr.get('type') != 'function':
                continue
                
            # Get the first value in the values list (handle empty list scenario)
            first_role_value = attr.get('values', [])[0] if attr.get('values') else None
            # Skip if the role isn't in our whitelist
            if not first_role_value or first_role_value not in ROLE_WHITELIST:
                continue
                
            # Extract relevant person/role details (adjust fields to match actual API structure)
            entry = {
                'organization_name': org_name,
                'role': first_role_value,
                'person_full_name': attr.get('person', {}).get('full_name'),
                'person_unique_id': attr.get('person', {}).get('id')
            }
            extracted_data.append(entry)
    
    return extracted_data

# Example usage:
# api_results = your_api_request_function()
# owners_and_directors = extract_owners_directors(api_results)

Key Tips for Smooth Execution

  • Avoid Key Errors: Always use .get() to access nested fields instead of direct indexing—this prevents crashes if a field is missing from the API response.
  • Refine the Whitelist: Test with a few API responses first to catch any edge case role terms you might have missed (some roles might use slightly phrased alternatives).
  • Add Validation: If the API returns inconsistent data, add checks for empty strings or malformed entries to keep your extracted results clean.

内容的提问来源于stack exchange,提问作者Christian Påbøl Jacobsen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 06:47:36