我有从Cassandra导出的JSON消息,我想将其加载到Elastic作为结构化文档:
{
"correlationId": "fb8f855a0eac8985d430896d",
"leg": 65535,
"tag": "circuitpath",
"offset": 479306,
"len": 508,
"prev": {
"page": {
"file": 10342,
"page": 8
},
"record": 216
},
"data": "[ { \"policy\": \"Health Check\", \"execTime\": 0, \"filters\": [ { \"espk\": \"DEFAULT_PRIMARY_VordelGateway_7.4.0:223\", \"name\": \"Set Message\", \"type\": \"ChangeMessageFilter\", \"class\": \"com.vordel.circuit.conversion.ChangeMessageFilter\", \"status\": \"Pass\", \"filterTime\": 1518702587006, \"execTime\": 0 } , { \"espk\": \"DEFAULT_PRIMARY_VordelGateway_7.4.0:222\", \"name\": \"Reflect\", \"type\": \"ReflectFilter\", \"class\": \"com.vordel.circuit.net.ReflectFilter\", \"status\": \"Pass\", \"filterTime\": 1518702587006, \"execTime\": 0 } ] } ]"
}
但我不知道如何简单地将“数据”内容转换为格式,以便将其加载到Elastic中。
我试过以下方法,其中$ json是上面的消息:
json2="${json//\\\"/\"}"
json2="${json2//\\\\/\\}"
echo "$json2"
那么“data”元素似乎是:
"data":"[ { "policy": "Health Check", "execTime": 0, "filters": [ { "espk": "DEFAULT_PRIMARY_VordelGateway_7.4.0:223", "name": "Set Message", "type": "ChangeMessageFilter", "class": "com.vordel.circuit.conversion.ChangeMessageFilter", "status": "Pass", "filterTime": 1518709297006, "execTime": 0 } , { "espk": "DEFAULT_PRIMARY_VordelGateway_7.4.0:222", "name": "Reflect", "type": "ReflectFilter", "class": "com.vordel.circuit.net.ReflectFilter", "status": "Pass", "filterTime": 1518709297006, "execTime": 0 } ] } ]"
但装载机告诉我
“status”:400,“error”:{“type”:“mapper_parsing_exception”,“reason”:“无法解析”,“caused_by”:{“type”:“json_parse_exception”,“reason”:“意外字符( 'p'(代码112)):期待逗号分隔OBJECT条目\ n在[来源:org.elasticsearch.common.io.stream.InputStreamStreamInput@3508edee; line:1,column:170
好像第一个子子元素名称“policy”中的字母“p”有问题
知道怎么把它变成Elastic吗?
要将.data元素从JSON字符串转换为JSON对象,可以使用过滤器:
.data |= fromjson
如果您只想提取.data元素并将其转换,您可以使用过滤器:
.data | fromjson
例如:jq -c '.data|fromjson' data.json
如果不确定fromjson
是否会成功,你可以使用成语:fromjson? // .
,例如:
.data |= (fromjson? // .)
虽然这是有效的JSON,但您可能需要解析对象的data
元素。
如果你愿意接受python:
record.json
(为方便起见,你也可以使用stdin)import json
record = json.load(open('/path/to/record.json'))
data = json.loads(record['data'])
record['data'] = data
json.dump(record, open("/path/to/result.json", "w"))