如何将 JSON 子元素字符串转换为 bash 中的真实 JSON 元素
How to convert JSON subelement strings to real JSON elements in bash
我有 JSON 从 Cassandra 导出的消息,我想将其作为结构化文档加载到 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 } ] } ]"
}
但我不知道如何通过简单的方式将 "data" 内容转换为允许将其加载到 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":"failed
to
parse","caused_by":{"type":"json_parse_exception","reason":"Unexpected
character ('p' (code 112)): was expecting comma to separate OBJECT
entries\n at [Source:
org.elasticsearch.common.io.stream.InputStreamStreamInput@3508edee;
line: 1, column: 170
第一个子元素名称 "policy"
中的字母 "p" 似乎有问题
知道如何将它放入 Elastic 中吗?
虽然这是有效的 JSON,但您可能需要解析对象的 data
元素。
如果你愿意接受python:
- 写入文件,比如
record.json
(为方便起见,您也可以使用标准输入)
python(替换文件名或使用sys.stdin和sys.stdout)
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"))
要将 .data 元素从 JSON 字符串转换为 JSON 对象,您可以使用过滤器:
.data |= fromjson
如果您只想提取 .data 元素并进行转换,您可以使用过滤器:
.data | fromjson
例如:jq -c '.data|fromjson' data.json
补充问题
如果不确定fromjson
是否成功,可以使用成语:fromjson? // .
,例如:
.data |= (fromjson? // .)
我有 JSON 从 Cassandra 导出的消息,我想将其作为结构化文档加载到 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 } ] } ]"
}
但我不知道如何通过简单的方式将 "data" 内容转换为允许将其加载到 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":"failed to parse","caused_by":{"type":"json_parse_exception","reason":"Unexpected character ('p' (code 112)): was expecting comma to separate OBJECT entries\n at [Source: org.elasticsearch.common.io.stream.InputStreamStreamInput@3508edee; line: 1, column: 170
第一个子元素名称 "policy"
中的字母 "p" 似乎有问题知道如何将它放入 Elastic 中吗?
虽然这是有效的 JSON,但您可能需要解析对象的 data
元素。
如果你愿意接受python:
- 写入文件,比如
record.json
(为方便起见,您也可以使用标准输入) python(替换文件名或使用sys.stdin和sys.stdout)
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"))
要将 .data 元素从 JSON 字符串转换为 JSON 对象,您可以使用过滤器:
.data |= fromjson
如果您只想提取 .data 元素并进行转换,您可以使用过滤器:
.data | fromjson
例如:jq -c '.data|fromjson' data.json
补充问题
如果不确定fromjson
是否成功,可以使用成语:fromjson? // .
,例如:
.data |= (fromjson? // .)