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

技术咨询:无经验情况下如何生成UBL2.1格式JSON文件

如何将现有JSON转换为UBL 2.1格式的JSON文件

一、先明确UBL 2.1的核心规则

UBL 2.1是标准化的商业报文格式,JSON版的UBL需要遵循其定义的数据模型(对应XML版的结构)。以你提供的采购订单类数据为例,核心结构分为订单头和订单行两大部分,每个字段都有对应的标准定义。

二、现有数据与UBL 2.1字段的映射

把你现有JSON的字段对应到UBL 2.1的标准字段:

  • OrderType → DocumentTypeCode(标注订单类型)
  • HeaderData.CustomerNo → BuyerCustomerParty.Party.PartyIdentification.ID(买方客户编号)
  • HeaderData.OrderNo → ID(订单自身编号)
  • HeaderData.OrderDMY → OrderDate(需转为ISO日期格式,比如28022022改成2022-02-28)
  • link_ord_data.SKUNo → LineItem.Item.SellersItemIdentification.ID(卖方商品编号)
  • link_ord_data.SKUQty → LineItem.Quantity(商品数量,建议转为数字类型)

三、具体实现步骤

  1. 梳理完整的UBL 2.1订单结构
    参考UBL 2.1采购订单(PurchaseOrder)的核心结构,至少包含以下部分:

    • 基础文档信息(ID、IssueDate、DocumentTypeCode等)
    • 买方信息(BuyerCustomerParty)
    • 卖方信息(SellerSupplierParty,若现有数据没提供,可留空或补充必要默认值)
    • 订单行集合(LineItems)
  2. 编写字段映射逻辑
    数据量小可以手动转换;批量处理则用代码实现映射,比如Python核心逻辑示例:

    import json
    from datetime import datetime
    
    # 读取现有JSON
    with open('original_order.json', 'r') as f:
        original_data = json.load(f)
    
    # 转换日期格式
    order_date = datetime.strptime(original_data['HeaderData']['OrderDMY'], '%d%m%Y').strftime('%Y-%m-%d')
    
    # 构建UBL 2.1格式的JSON
    ubl_order = {
        "UBLVersionID": "2.1",
        "ID": original_data['HeaderData']['OrderNo'],
        "DocumentTypeCode": original_data['OrderType'],
        "OrderDate": order_date,
        "BuyerCustomerParty": {
            "Party": {
                "PartyIdentification": [
                    {
                        "ID": original_data['HeaderData']['CustomerNo']
                    }
                ]
            }
        },
        "LineItem": []
    }
    
    # 处理订单行
    for idx, item in enumerate(original_data['OrderDetail']['link_ord_data'], 1):
        line_item = {
            "ID": str(idx),
            "Quantity": int(item['SKUQty']),
            "Item": {
                "SellersItemIdentification": [
                    {
                        "ID": item['SKUNo']
                    }
                ]
            }
        }
        ubl_order['LineItem'].append(line_item)
    
    # 保存结果
    with open('ubl_2.1_order.json', 'w') as f:
        json.dump(ubl_order, f, indent=2)
    
  3. 验证格式正确性
    转换完成后,对照UBL 2.1的官方数据模型检查字段合规性,重点确保日期格式、数据类型(比如数量为数字而非字符串)、嵌套结构符合标准。

四、转换后的示例JSON

{
  "UBLVersionID": "2.1",
  "ID": "testing1234",
  "DocumentTypeCode": "N",
  "OrderDate": "2022-02-28",
  "BuyerCustomerParty": {
    "Party": {
      "PartyIdentification": [
        {
          "ID": "3ZZC15"
        }
      ]
    }
  },
  "LineItem": [
    {
      "ID": "1",
      "Quantity": 15,
      "Item": {
        "SellersItemIdentification": [
          {
            "ID": "3M-6445-SSP"
          }
        ]
      }
    },
    {
      "ID": "2",
      "Quantity": 145,
      "Item": {
        "SellersItemIdentification": [
          {
            "ID": "9789810779078"
          }
        ]
      }
    },
    {
      "ID": "3",
      "Quantity": 50,
      "Item": {
        "SellersItemIdentification": [
          {
            "ID": "ES-ENV-DL/U"
          }
        ]
      }
    }
  ]
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.27 07:40:32