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

使用Bazaarvoice Java JOLT库执行JSON转换无结果的问题排查求助

Bazaarvoice Java JOLT库执行JSON转换无结果的问题排查求助

大家好,我现在在用Bazaarvoice的Java JOLT库做JSON转换操作,但始终得不到预期的输出结果,想请各位帮忙排查下问题出在哪里。

我已经准备了输入JSON、业务规则文件、JOLT转换规格文件,还有对应的Java实现代码,但运行后没能输出想要的结果。以下是我的相关文件和预期输出:

1. Java代码(AttributeTransformer.java)

package com.ebay.app.compliance;

import com.bazaarvoice.jolt.Chainr;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

public class AttributeTransformer {
    private final Chainr chainr;
    private final Map<String, Object> rules;
    private final ObjectMapper objectMapper = new ObjectMapper();

    /**
     * Constructor loads the JOLT specification and compliance rules from classpath resources.
     * In a real microservice (e.g., using Spring Boot), this would be done once at startup.
     */
    public AttributeTransformer() throws IOException {
        // Load the JOLT transformation spec from the classpath
        InputStream specStream = this.getClass().getResourceAsStream("/spec.json");
        if (specStream == null) {
            throw new IOException("Could not find spec.json in classpath.");
        }
        // Use Jackson ObjectMapper to parse the spec directly into a List.
        List<Object> specJson = objectMapper.readValue(specStream, new TypeReference<List<Object>>() {});
        this.chainr = Chainr.fromSpec(specJson);

        // Load the business rules from the classpath
        InputStream rulesStream = this.getClass().getResourceAsStream("/rules.json");
        if (rulesStream == null) {
            throw new IOException("Could not find rules.json in classpath.");
        }
        // Use Jackson to parse the rules JSON into a Java Map
        this.rules = objectMapper.readValue(rulesStream, new TypeReference<Map<String, Object>>() {});
    }

    /**
     * Transforms an input payload into compliance attributes.
     *
     * @param inputJson The input from the classification service (e.g., {"product_class": "Knife", "product_subclass": "Throwing Knife"})
     * @return A JSON string of the transformed attributes.
     * @throws IOException if JSON processing fails.
     */
    public String transform(String inputJson) throws IOException {
        // Parse the input JSON string into a Java Map
        Map<String, Object> inputMap = objectMapper.readValue(inputJson, new TypeReference<Map<String, Object>>() {});
        // The JOLT transform method can take a "context" map, which allows the spec
        // to reference external data. We'll provide our rules here.
        Map<String, Object> context = Map.of("rules", this.rules);
        // Perform the transformation
        Object transformedOutput = chainr.transform(inputMap, context);
        // Convert the final Java Object back to a pretty-printed JSON string
        return objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(transformedOutput);
    }

    /**
     * Main method to act as a test harness for our transformer.
     */
    public static void main(String[] args) {
        try {
            // 1. Initialize the transformer (loads specs and rules)
            AttributeTransformer transformer = new AttributeTransformer();
            // 2. Define the input payload from the upstream service
            String inputPayload = "{\"product_class\": \"Knife\", \"product_subclass\": \"Throwing Knife\"}";
            System.out.println("--- 🚀 Starting JOLT Transformation ---");
            System.out.println("Input Payload: " + inputPayload);
            System.out.println("----------------------------------------");
            // 3. Run the transformation
            String finalOutput = transformer.transform(inputPayload);
            // 4. Print the result
            System.out.println("Final Output:");
            System.out.println(finalOutput);
            System.out.println("--- ✅ Transformation Complete ---");
        } catch (IOException e) {
            System.err.println("Failed to initialize or run transformer.");
            e.printStackTrace();
        }
    }
}

2. 业务规则文件(rules.json)

{
  "Knife": {
    "Cutlery Knife": {
      "is_bladed": true,
      "is_prohibited_ca": false,
      "requires_age_verification": false,
      "compliance_policy_id": "CP-102"
    },
    "Throwing Knife": {
      "is_bladed": true,
      "is_prohibited_ca": true,
      "requires_age_verification": true,
      "compliance_policy_id": "CP-451"
    }
  },
  "Lighter": {
    "Default": {
      "is_bladed": false,
      "is_hazmat_ground": true,
      "is_hazmat_air": true,
      "compliance_policy_id": "CP-210"
    }
  }
}

3. JOLT转换规格文件(spec.json)

[
  {
    "operation": "shift",
    "spec": {
      "rules": {
        "@1,product_class": {
          "*": {
            "@2,&": {
              "@4,product_subclass": {
                "*": {
                  "@2,&": "attributes"
                }
              }
            }
          }
        }
      },
      "*": "&"
    }
  }
]

预期输出

我期望运行后能得到如下格式的输出(核心是从rules.json中匹配对应product_class和product_subclass的属性,封装到attributes字段下):

--- 🚀 Starting JOLT Transformation ---
Input Payload: {"product_class": "Knife", "product_subclass": "Throwing Knife"}
----------------------------------------
Final Output:
{
  "product_class" : "Knife",
  "product_subclass" : "Throwing Knife",
  "attributes" : {
    "is_bladed" : true,
    "is_prohibited_ca" : true,
    "requires_age_verification" : true,
    "compliance_policy_id" : "CP-451"
  }
}
--- ✅ Transformation Complete ---

但实际运行后并没有得到这样的结果,想请各位帮忙看看我的JOLT规格是不是写得有问题,或者Java代码里的调用方式哪里出错了?

内容来源于stack exchange

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.07 12:47:58