您好,我正在尝试从以下 JSON 数据中提取令牌last_name
:first_name
phone
{"message":"{\"_\":\"user\",\"pFlags\":{\"contact\":true},\"flags\":2167,\"id\":95384129,\"access_hash\":\"780828213343231334\",\"first_name\":\"xaa\",\"last_name\":\"xz\",\"phone\":\"989123930793\",\"photo\":{\"_\":\"userProfilePhoto\",\"photo_id\":\"409671715068685579\",\"photo_small\":{\"_\":\"fileLocation\",\"dc_id\":4,\"volume_id\":\"455930331\",\"local_id\":281464,\"secret\":\"3283911659027961987\"},\"photo_big\":{\"_\":\"fileLocation\",\"dc_id\":4,\"volume_id\":\"455930331\",\"local_id\":281466,\"secret\":\"3533047346646019161\"}},\"status\":{\"_\":\"userStatusLastMonth\"}}","phone":"989123930793","@version":"1","typ":"tg_contacts","access_hash":"780828213343231334","id":95384129,"@timestamp":"2020-01-26T13:53:31.091Z","path":"/home/user/mirror2/users_5d3de570e549953b6163eb0f.log","type":"redis","flags":2167,"host":"ubuntu","imported_from":"tg"}
这是我的命令
jq -r '[.first_name, .last_name, .phone]|@csv'
我如何只能提取字段phone
,我不知道为什么我不能提取first_name
和last_name
。
答案1
如果你试试
jq -r '.' file.json
您会看到没有名字和姓氏,只有电话号码。
{
"message": "{\"_\":\"user\",\"pFlags\":{\"contact\":true},\"flags\":2167,\"id\":95384129,\"access_hash\":\"780828213343231334\",\"first_name\":\"xaa\",\"last_name\":\"xz\",\"phone\":\"989123930793\",\"photo\":{\"_\":\"userProfilePhoto\",\"photo_id\":\"409671715068685579\",\"photo_small\":{\"_\":\"fileLocation\",\"dc_id\":4,\"volume_id\":\"455930331\",\"local_id\":281464,\"secret\":\"3283911659027961987\"},\"photo_big\":{\"_\":\"fileLocation\",\"dc_id\":4,\"volume_id\":\"455930331\",\"local_id\":281466,\"secret\":\"3533047346646019161\"}},\"status\":{\"_\":\"userStatusLastMonth\"}}",
"phone": "989123930793",
"@version": "1",
"typ": "tg_contacts",
"access_hash": "780828213343231334",
"id": 95384129,
"@timestamp": "2020-01-26T13:53:31.091Z",
"path": "/home/user/mirror2/users_5d3de570e549953b6163eb0f.log",
"type": "redis",
"flags": 2167,
"host": "ubuntu",
"imported_from": "tg"
}
您要查找的字段位于 中.message
,它是一个字符串,一个编码的 JSON 对象。
jq
有fromjson
您可以使用内置函数并将其作为 JSON 获取:
jq -r '.message | fromjson | [.first_name, .last_name, .phone]|@csv' file.json
"xaa","xz","989123930793"