简体   繁体   English

JOLT 中的条件 JSON 转换

[英]conditional JSON transformation in JOLT

I need to process response from SOLR service that looks like following:我需要处理来自 SOLR 服务的响应,如下所示:

{
  "data": {
    "distanceUnits": "mi", //it can be "mi", "MI", "miles", "km", "KM", etc.
    "solrResponse": {
      "header": {
        "found": 32,
        "retrieved": 10,
        ... //there can be other things like timestamp
      },
      "results": [
        {
          "matchScore": "08768",
          "indicators" [{...}],
          "location": {
             ... //there are about hundred attributes
            "distance": "2.7649" //distance always in km
            "similarity": "0.342"
        },
        ...
      ]
}

The transformation need to return everything from solrResponse practically intact except two things:转换需要从 solrResponse 返回几乎完整的所有内容,除了两件事:

  1. similarity need to be renamed to similarityScore and moved up next to matchScore. similarity 需要重命名为 similarityScore 并向上移动到 matchScore 旁边。
  2. If distanceUnit is specified as miles, distance need to be converted to miles (ie divided by 1.609)如果 distanceUnit 指定为英里,距离需要转换为英里(即除以 1.609)
  3. distance value is to berounded to 2 digits after decimal point and concatenated with distanceUnit.距离值四舍五入到小数点后2位,并与distanceUnit连接。 I have created the following spec:我创建了以下规范:
[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "data": {
        "unit1": "=toLower(@(1,distanceUnit))",
        "unit2": "=substring(@(1,unit1),0,1)",
        "solrResponse": {
          "results": {
            "": {
              "dist1": "=divideAndRound(2,@(1,distance),0.6215)",
              "distanceMiles": "=concat(@(1,dist1), ' ', @(2,distanceUnit))",
              "dist2": "=divideAndRound(2,@(1,distance),1.0)",
              "distanceKm": "=concat(@(1,dist2), ' ', @(2,distanceUnit))"
            }
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "data": {
        "solrResponse": {
          "header": "&1",
          "response": {
            "": {
              "&1.similarity": "similarityScore",
              "unit2": {
                "m": {
                  "distanceMiles": "locations.distance"
                },
                "": {
                  "distanceKm": "locations.distance"
                }
              },
              "": "&1"
            }
          }
        }
      }
    }
  }
]

Unfortunately it does not work.不幸的是它不起作用。 Please help me.请帮我。

The Jolt doesn't have a function such as multiply or product , but divide and divideAndRound to be used in a modify transformation spec. Jolt 没有 function,例如multiplyproduct ,但是dividedivideAndRound用于修改转换规范。 Then, we'll have a conditional to filer out whether the provided value for distanceUnit attribute exists within the list( mi,m,Mi,MI ) or not such as然后,我们将有一个条件来过滤distanceUnit属性的提供值是否存在于列表( mi,m,Mi,MI )中,例如

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": { // this outer one represents "locations" list(if there's no other outer level list or object, otherwise replace "*" with the key name "locations") 
        "*": { // this one stands for "locations" list's indexes
          "distanceinMiles_": "=divideAndRound(2,@(1,distance),0.6215040397762585)",
          "distanceinMiles": "=concat(@(1,distanceinMiles_),' ',@(1,distanceUnit))",
          "distance_": "=divideAndRound(2,@(1,distance),1)",
          "distance": "=concat(@(1,distance_),' ',@(1,distanceUnit))"
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "address": "&2[&1].&",
          "city": "&2[&1].&",
          "state": "&2[&1].&",
          "postalCode": "&2[&1].&",
          "distanceUnit": {
            "mi|m|Mi|MI": { "@(2,distanceinMiles)": "&4[&3].distance" },
            "*": { "@(2,distance)": "&4[&3].distance" }
          }
        }
      }
    }
  }
]

where在哪里

  • @(1,distanceXx) in the first spec represents traversing one colon( : ) (as a Right-Hand-Side element) in order to reach the original level of the elements @(1,distanceXx) , while @(2,distanceXx) stands for traversing { (object opening "curly" brace) twice to reach the same.第一个规范中的@(1,distanceXx)表示遍历一个冒号 ( : )(作为右侧元素)以达到元素@(1,distanceXx)的原始级别,而@(2,distanceXx)代表遍历{ (object opening "curly" brace) 两次以达到相同。

  • the attributes at the indexes level of "locations" are combined by using [&1] and [&3] respectively “locations”索引级别的属性分别使用[&1][&3]组合

  • &2 and &4 are substituted respectively to denote the key name "locations" &2&4分别代入表示键名"locations"

the demo on the site http://jolt-demo.appspot.com/ :网站http://jolt-demo.appspot.com/上的演示:

在此处输入图像描述

Edit ( response for your last edit ): Considering the input编辑对您上次编辑的回应):考虑输入

{
  "data": {
    "distanceUnit": "Mi",
    "solrResponse": {
      "header": {
        "found": 32,
        "retrieved": 10
      },
      "results": [
        {
          "matchScore": "08768",
          "location": {
            "distance": "2.7649",
            "similarity": "0.342"
          }
        }
      ]
    }
  }
}

you can use the following specs您可以使用以下规格

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "data": {
        "distanceUnit": "=toLower(@(1,&))",
        "solrResponse": {
          "results": {
            "*": {
              "location": {
                "dist1": "=divideAndRound(2,@(1,distance),0.6215)",
                "distanceKm": "=concat(@(1,dist1), ' km')",
                "dist2": "=divideAndRound(2,@(1,distance),1.0)",
                "distanceMiles": "=concat(@(1,dist2), ' km')"
              }
            }
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "data": {
        "*": "&1.&",
        "solrResponse": {
          "*": "&2.&1.&",
          "results": {
            "*": {
              "*": "&4.&3.&2[&1].&",
              "location": {
                "@(4,distanceUnit)": {
                  "mi": { "@(2,distanceKm)": "&7.&6.&5[&4].&3.distance" },
                  "km": { "@(2,distanceMiles)": "&7.&6.&5[&4].distance" }
                },
                "similarity": "&5.&4.&3[&2].&Score"
              }
            }
          }
        }
      }
    }
  }, 
  {
    "operation": "sort"
  }
]

the demo is:演示是:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM