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

递归清理JSON API v1.1对象中空关系字段的技术问询

递归清理JSON API中的空关联关系

我来帮你搞定这个递归清理空关联的问题!针对你遇到的JSON API v1.1适配场景,需要递归遍历对象、移除所有空关联(包括嵌套的),并且在清理后relationships为空时整体移除该字段,下面是完整的实现方案:

核心实现代码

protected removeEmptyRelationships(obj) {
  // 非对象类型直接返回,终止递归
  if (typeof obj !== 'object' || obj === null) {
    return obj;
  }

  // 遍历对象键值对,递归处理每个值并筛选有效字段
  const processedObj = Object.entries(obj).reduce((acc, [key, value]) => {
    const processedValue = this.removeEmptyRelationships(value);

    let shouldKeep = true;

    // 处理relationships字段:如果清理后为空则不保留
    if (key === 'relationships') {
      if (typeof processedValue === 'object' && Object.keys(processedValue).length === 0) {
        shouldKeep = false;
      }
    } else {
      // 处理关联字段(如role、group):判断是否为有效关联
      const isRelationshipEntry = typeof processedValue === 'object' && processedValue !== null;
      if (isRelationshipEntry) {
        // 有效关联需要包含非空的data字段
        const hasValidData = processedValue.hasOwnProperty('data') && 
                             processedValue.data !== undefined && 
                             processedValue.data !== null && 
                             !(typeof processedValue.data === 'object' && Object.keys(processedValue.data).length === 0);
        
        // 如果没有有效data且清理后为空,则移除该字段
        if (!hasValidData && Object.keys(processedValue).length === 0) {
          shouldKeep = false;
        }
      }
    }

    // 仅保留有效且非空的键值对
    if (shouldKeep && processedValue !== undefined && processedValue !== null) {
      acc[key] = processedValue;
    }

    return acc;
  }, {});

  // 如果清理后对象为空,返回undefined让上层过滤
  return Object.keys(processedObj).length > 0 ? processedObj : undefined;
}

代码逻辑解释

  1. 递归终止条件:遇到非对象(如字符串、数字、null)直接返回,避免无效递归。
  2. 遍历处理每个字段:用reduce遍历对象的所有键值对,对每个值先递归清理,确保嵌套的关联也被处理。
  3. 特殊处理relationships字段:如果清理后的relationships是空对象,直接移除整个字段。
  4. 判断有效关联:关联字段(如role、group)需要包含非空的data字段才会被保留;如果关联对象是空(比如{})或者data为空/不存在,就会被移除。
  5. 空对象过滤:清理后的对象如果为空,返回undefined,让上层的reduce自动忽略这个键。

测试你的示例数据

用你提供的请求体测试:

const input = { 
  id: '1', 
  type: 'users', 
  attributes: { firstName: 'John', lastName: 'Doe' }, 
  relationships: { 
    role: { data: { type: 'roles', id: '1' } }, 
    group: { data: { type: 'groups', id: '1', relationships: { policy: {} } } }, 
    type: {} 
  } 
};

const result = this.removeEmptyRelationships(input);

返回的结果会自动移除relationships.type和group.data.relationships.policy,最终输出:

{
  id: '1',
  type: 'users',
  attributes: { firstName: 'John', lastName: 'Doe' },
  relationships: {
    role: { data: { type: 'roles', id: '1' } },
    group: { data: { type: 'groups', id: '1' } }
  }
}

额外注意点

  • 支持任意层级的嵌套关联,不管嵌套多深都会被递归清理。
  • 兼容JSON API中data: null的空关联场景,这种情况也会被自动移除。
  • 如果所有关联都被清理,整个relationships字段会从返回对象中消失。

内容的提问来源于stack exchange,提问作者user8116198

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.13 08:37:54