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

使用Jolt规范删除扁平化JSON中空值对应的属性相关信息

处理扁平化JSON:移除值为空的属性相关字段

问题描述

给定一个扁平化的JSON对象,要求当某个属性对应的.....values[0].value字段值为空字符串时,删除该属性的所有相关键值对。例如输入中height和width的values[0].value为空,因此它们的所有关联字段都需要被移除,仅保留minimum这类有效值对应的字段。

输入示例

{
  "p-unit.myProduct.attributes[0].height.identifier": "height",
  "p-unit.myProduct.attributes[0].height.type": "int",
  "p-unit.myProduct.attributes[0].height.values[0].value": "",
  "p-unit.myProduct.attributes[0].height.values[0].uom.value": "cm",
  "p-unit.myProduct.attributes[1].width.identifier": "width",
  "p-unit.myProduct.attributes[1].width.type": "int",
  "p-unit.myProduct.attributes[1].width.values[0].value": "",
  "p-unit.myProduct.attributes[1].width.values[0].uom.value": "cm",
  "p-unit.myProduct.attributes[2].minimum.identifier": "minimum",
  "p-unit.myProduct.attributes[2].minimum.type": "int",
  "p-unit.myProduct.attributes[2].minimum.values[0].value": "15",
  "p-unit.myProduct.attributes[2].minimum.values[0].uom.value": "cm"
}

期望输出

{
  "p-unit.myProduct.attributes[2].minimum.identifier": "minimum",
  "p-unit.myProduct.attributes[2].minimum.type": "int",
  "p-unit.myProduct.attributes[2].minimum.values[0].value": "15",
  "p-unit.myProduct.attributes[2].minimum.values[0].uom.value": "cm"
}

解决方案

JavaScript实现

function filterFlatJson(flatJson) {
  // 收集所有需要删除的属性前缀
  const prefixesToRemove = new Set();
  for (const key in flatJson) {
    if (key.endsWith('.values[0].value') && flatJson[key] === '') {
      const prefix = key.slice(0, key.lastIndexOf('.values[0].value'));
      prefixesToRemove.add(prefix);
    }
  }

  // 过滤掉目标前缀下的所有键
  const filtered = {};
  for (const key in flatJson) {
    const shouldKeep = !Array.from(prefixesToRemove).some(prefix => key.startsWith(prefix + '.'));
    if (shouldKeep) {
      filtered[key] = flatJson[key];
    }
  }

  return filtered;
}

// 测试用例
const input = {
  "p-unit.myProduct.attributes[0].height.identifier": "height",
  "p-unit.myProduct.attributes[0].height.type": "int",
  "p-unit.myProduct.attributes[0].height.values[0].value": "",
  "p-unit.myProduct.attributes[0].height.values[0].uom.value": "cm",
  "p-unit.myProduct.attributes[1].width.identifier": "width",
  "p-unit.myProduct.attributes[1].width.type": "int",
  "p-unit.myProduct.attributes[1].width.values[0].value": "",
  "p-unit.myProduct.attributes[1].width.values[0].uom.value": "cm",
  "p-unit.myProduct.attributes[2].minimum.identifier": "minimum",
  "p-unit.myProduct.attributes[2].minimum.type": "int",
  "p-unit.myProduct.attributes[2].minimum.values[0].value": "15",
  "p-unit.myProduct.attributes[2].minimum.values[0].uom.value": "cm"
};

const output = filterFlatJson(input);
console.log(JSON.stringify(output, null, 2));

Python实现

import json

def filter_flat_json(flat_json):
    prefixes_to_remove = set()
    # 收集需要删除的属性前缀
    for key, value in flat_json.items():
        if key.endswith('.values[0].value') and value == '':
            prefix = key[:key.rfind('.values[0].value')]
            prefixes_to_remove.add(prefix)
    # 过滤键值对
    filtered = {}
    for key, value in flat_json.items():
        keep = not any(key.startswith(f"{prefix}.") for prefix in prefixes_to_remove)
        if keep:
            filtered[key] = value
    return filtered

# 测试用例
input_json = {
    "p-unit.myProduct.attributes[0].height.identifier": "height",
    "p-unit.myProduct.attributes[0].height.type": "int",
    "p-unit.myProduct.attributes[0].height.values[0].value": "",
    "p-unit.myProduct.attributes[0].height.values[0].uom.value": "cm",
    "p-unit.myProduct.attributes[1].width.identifier": "width",
    "p-unit.myProduct.attributes[1].width.type": "int",
    "p-unit.myProduct.attributes[1].width.values[0].value": "",
    "p-unit.myProduct.attributes[1].width.values[0].uom.value": "cm",
    "p-unit.myProduct.attributes[2].minimum.identifier": "minimum",
    "p-unit.myProduct.attributes[2].minimum.type": "int",
    "p-unit.myProduct.attributes[2].minimum.values[0].value": "15",
    "p-unit.myProduct.attributes[2].minimum.values[0].uom.value": "cm"
}

output_json = filter_flat_json(input_json)
print(json.dumps(output_json, indent=2))

逻辑说明

  1. 收集待删除前缀:遍历所有键,找到以.values[0].value结尾且值为空的键,提取该属性的公共父路径作为前缀,存入集合避免重复。
  2. 过滤键值对:再次遍历所有键,检查当前键是否属于待删除前缀的子路径,若不属于则保留到新对象中。

内容的提问来源于stack exchange,提问作者Katja Bürger

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.16 23:12:32