我应该使用哪个JSONPath表达式来分割我的JSON字符串?

时间:2017-09-25 10:46:11

标签: json apache-nifi jsonpath

我想应用ButtonClick以便将以下JSON文件拆分为2个FlowFiles(根据SplitJson):

hits

我应该使用哪个 {"took":0,"timed_out":false,"_shards": {"total":5,"successful":5,"failed":0}, "hits":{"total":2,"max_score":0.0, "hits": [ {"_index":"my_index","_type":"my_entry","_id":"111","_score":0.0,"_source":{"ZoneId":"1","OriginId":"1"}, "fields":{"ttime":[11000]}}, {"_index":"my_index","_type":"my_entry","_id":"222","_score":0.0,"_source":{"ZoneId":"1","OriginId":"2"}, "fields":{"ttime":[5000]}} ] } } ?我尝试了JsonPath Expression,但它根据第一级$.hits[*]拆分了内容。在我的情况下,我有hits,但我应该如何在表达式中指定它?

更新

我想获得两个FlowFiles:

FlowFile#1:hits[hits[...]]

FlowFile#2: {"_index":"my_index","_type":"my_entry","_id":"111","_score":0.0,"_source":{"ZoneId":"1","OriginId":"1"},"fields":{"ttime":[11000]}}

2 个答案:

答案 0 :(得分:3)

var arr = $.hits.hits;

将为您提供所需的2个对象数组。

var o1 = arr[0];
var o2 = arr[1];

会给你2个你想要的东西。

var json1 = JSON.stringify(arr[0]);
var json2 = JSON.stringify(arr[1]);

将按要求为您提供2个JSON文件。 这是你需要的吗?

答案 1 :(得分:1)

您可以使用此website为您的案例测试JSONPath索引。 正确答案是$.hits.hits[*]

如DanteTheSmith所述,您可以在案例中使用$.hits.hits。这取决于后处理。两种方法都运行良好。