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

如何使用JOLT基于输入数组及层级字段生成目标扁平化数组

JOLT转换JSON为数据库存储用扁平化对象数组求助

需要将输入JSON转换为可存入数据库的扁平化对象数组,每个对象需包含以下字段:

  • rateplan:对应subscriptions.subscription[].products[specification=rateplan].code
  • code:对应subscriptions.subscription[].options[].products[].code
  • mandatory:对应subscriptions.subscription[].options[].mandatory
  • multiple:对应subscriptions.subscription[].options[].multiple

对象总数需与subscriptions.subscription[].options[].products[]的元素数量一致。


输入JSON

{    
    "subscriptions": {        
        "subscription": [{                
            "name": "TA Generated subsr1",                
            "specification": "Mobile Postpaid",                
            "products": [{                        
                "specification": "rateplan",                        
                "code": "CODE1"                    
            }, {                        
                "specification": "addon",                        
                "code": "P2PCF"                    
            }, {                        
                "specification": "addon",                        
                "code": "AANSK"                    
            }],                
            "options": [{                        
                "category": "Installment Plan",                        
                "products": [{                                
                    "specification": "addon",                                
                    "code": "TA_CODE2"                            
                }, {                                
                    "specification": "addon",                                
                    "code": "TA_CODE3"                            
                }],                        
                "mandatory": "false",                        
                "multiple": "false"                    
            }, {                        
                "category": "Internet",                        
                "products": [{                                
                    "specification": "addon",                                
                    "code": "TA_CODE4"                            
                }],                        
                "mandatory": "false",                        
                "multiple": "false"                    
            }]            
        }, {                
            "name": "TA Generated subsr2",                
            "specification": "Mobile Postpaid",                
            "products": [{                        
                "specification": "rateplan",                        
                "code": "CODE11"                    
            }, {                        
                "specification": "addon",                        
                "code": "P2PCF"                    
            }, {                        
                "specification": "addon",                        
                "code": "AANSK"                    
            }],                
            "options": [{                        
                "category": "Internet",                        
                "products": [{                                
                    "specification": "addon",                                
                    "code": "TA_CODE41"                            
                }],                        
                "mandatory": "false",                        
                "multiple": "false"                    
            }]            
        }]    
    }
}

期望输出JSON

[    
    {        
        "rateplan": "CODE1",        
        "code": "TA_CODE2",        
        "mandatory": "false",        
        "multiple": "false"    
    },        
    {        
        "rateplan": "CODE1",        
        "code": "TA_CODE3",        
        "mandatory": "false",        
        "multiple": "false"    
    },    
    {        
        "rateplan": "CODE11",        
        "code": "TA_CODE41",        
        "mandatory": "false",        
        "multiple": "false"    
    }    
]

我编写的JOLT Spec

[  
    {    
        "operation": "shift",    
        "spec": {      
            "subscriptions": {        
                "subscription": {          
                    "*": {            
                        "products": {              
                            "*": {                
                                "specification": {                  
                                    "rateplan": {                    
                                        "@(2,code)": "o[].rateplan"                  
                                    }                
                                }              
                            }            
                        },            
                        "options": {              
                            "*": {                
                                "products": {                  
                                    "*": {                    
                                        "@(code)": "o[&1].code",                    
                                        "@(2,mandatory)": "o[&1].mandatory",                    
                                        "@(2,multiple)": "o[&1].multiple"                  
                                    }                
                                }              
                            }            
                        }          
                    }        
                }      
            }    
        }  
    }
]

调整后的JOLT Spec

[
  {
    "operation": "shift",
    "spec": {
      "subscriptions": {
        "subscription": {
          "*": {
            "products": {
              "*": {
                "specification": {
                  "rateplan": {
                    "@(2,code)": "[&3].rateplan"
                  }
                }
              }
            },
            "options": {
              "*": {
                "products": {
                  "*": {
                    "@(4,rateplan)": "[&5].rateplan",
                    "@(code)": "[&5].code",
                    "@(2,mandatory)": "[&5].mandatory",
                    "@(2,multiple)": "[&5].multiple"
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  {
    "operation": "remove",
    "spec": {
      "*": {
        "rateplan": {
          "": ""
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": "[]"
    }
  }
]

调整说明

  1. 层级关联修正:原Spec中o[].rateplan和o[&1].code的层级索引不匹配,导致rateplan无法和对应options的products正确绑定。调整后通过[&3]和[&5]的层级定位,确保每个subscription下的rateplan能与该订阅下所有options的products一一关联。
  2. 空元素清理:添加remove操作过滤未匹配到products的空对象,最后用shift操作将结果整理为纯数组格式,与期望输出完全一致。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.12 15:47:09