NiFi中EvaluateJsonPathAttributeCustom提取数值型经纬度失败如何解决?
NiFi 提取JSON数值类型经纬度字段报错解决方案
问题场景
使用InvokeHTTPCustom调用API获取JSON格式返回数据后,可正常提取字符串类型字段,但提取metadata下数值类型的latitude、longitude字段时,EvaluateJsonPathAttributeCustom处理器报错。
现有JSON样例
[ { "input_index": 0, "candidate_index": 0, "delivery_line_1": "1 Santa Claus Ln", "last_line": "North Pole AK 99705-9901", "delivery_point_barcode": "997059901010", "components": { "primary_number": "1", "street_name": "Santa Claus", "street_suffix": "Ln", "city_name": "North Pole", "state_abbreviation": "AK", "zipcode": "99705", "plus4_code": "9901", "delivery_point": "01", "delivery_point_check_digit": "0" }, "metadata": { "record_type": "S", "zip_type": "Standard", "county_fips": "02090", "county_name": "Fairbanks North Star", "carrier_route": "C004", "congressional_district": "AL", "rdi": "Commercial", "elot_sequence": "0001", "elot_sort": "A", "latitude": 64.75233, "longitude": -147.35297, "coordinate_license": 1, "precision": "Rooftop", "time_zone": "Alaska", "utc_offset": -9, "dst": true }, "analysis": { "dpv_match_code": "Y", "dpv_footnotes": "AABB", "dpv_cmra": "N", "dpv_vacant": "N", "dpv_no_stat": "Y", "active": "Y", "footnotes": "L#" } }, { "input_index": 1, "candidate_index": 0, "addressee": "Apple Inc", "delivery_line_1": "1 Infinite Loop" } ]
现有EvaluateJsonPathAttributeCustom配置
Attribute Name Input : x**.json Attribute Name Output : latitude JsonPathExpresssion : $[0].metadata.latitude Splitif.. : False
解决方法
报错核心原因是EvaluateJsonPathAttributeCustom默认对非字符串类型的直接解析存在类型转换异常,按以下两种方案调整即可:
- 方案1:修改JsonPath表达式,强制转换为字符串
将JsonPathExpresssion参数值修改为$[0].metadata.latitude.toString(),经度对应修改为$[0].metadata.longitude.toString(),其余配置保持不变即可。提取后的经纬度会以字符串格式存入属性,后续如需数值运算,可通过UpdateAttribute处理器的表达式二次转换。 - 方案2:更换处理器适配数值类型
改用官方原生EvaluateJsonPath处理器,将Return Type参数配置为auto-detect或number,即可直接正确识别JSON中的数值类型字段,无需额外做类型转换。
内容的提问来源于stack exchange,提问作者Shubham Patwa
相关产品推荐
相关产品推荐

