解析JSON值时出错

时间:2017-11-01 06:23:46

标签: json jq

不知何故,我无法解析以下JSON的值。

  

无法获取此qlog.QBOL-QUERY-SCHEMA值(使字符串无效   误差)

我正在寻找像这样的输出:

id:104909913,QBOL-QUERY-SCHEMA:/tmp/2017-10-31/5031/104909913.dir/000

JSON文件:

{
  "id":104909913,
  "path":"/tmp/2017-10-31/5031/104909913",
  "status":"done",
  "created_at":"2017-10-31T05:03:19Z",
  "updated_at":"2017-10-31T05:10:24Z",
  "command_type":"HiveCommand",
  "progress":100,
  "qbol_session_id":6527670,
  "qlog":"{\"QBOL-QUERY-SCHEMA\":{\"/tmp/2017-10-31/5031/104909913.dir/000\":[{\"ColumnType\":\"string\",\"ColumnName\":\"ts_date\"},{\"ColumnType\":\"bigint\",\"ColumnName\":\"testing_requests\"},{\"ColumnType\":\"bigint\",\"ColumnName\":\"testings_placed\"}]},\"EXECUTION_ENGINE\":\"mr\",\"QBOL-S3N-BYTES-READ\":1258754351,\"QBOL-S3N-BYTES-WRITTEN\":224,\"HIVE_VERSION\":\"0.13\"}",
  "resolved_macros":null,
  "pid":27212,
  "template":"generic",
  "submit_time":1509426199,
  "start_time":1509426202,
  "end_time":1509426624,
  "can_notify":false,
  "num_result_dir":1,
  "pool":null,
  "timeout":null,
  "name":null,
  "command_source":"API",
  "account_id":5031,
  "saved_query_mutable_id":null,
  "user_id":12146,
  "label":"default",
  "meta_data":{
    "results_resource":"commands/104909913/results",
    "logs_resource":"commands/104909913/logs"
  },
  "uid":5164,
  "perms":null,
  "command":{
    "query":"select * from abc ",
    "sample":false,
    "approx_mode":false,
    "approx_aggregations":false,
    "loader_table_name":null,
    "loader_stable":null,
    "md_cmd":false,
    "script_location":null
  },
  "instance":null
}

感谢您的帮助!

2 个答案:

答案 0 :(得分:0)

输入对象中的

glog属性包含JSON文本表示形式,采用“ unparsed ”形式。

要将JSON文本表示解析为值,请使用fromjson函数:

jq -r '"id:\(.id),QBOL-QUERY-SCHEMA:\(.qlog | fromjson | .["QBOL-QUERY-SCHEMA"] | keys[0])"' input.json

输出:

id:104909913,QBOL-QUERY-SCHEMA:/tmp/2017-10-31/5031/104909913.dir/000

答案 1 :(得分:0)

这是一种更具编程性的方法(也就是说,jq程序没有明确提到“QBOL-QUERY-SCHEMA”键)。然而,这是否符合您的实际需求还不清楚。

"id:\(.id)," + 
  (.qlog | fromjson | to_entries[0] | .key + ":" +
            (.value | to_entries[0] | .key) )

当使用-r选项调用jq时,会产生:

 id:104909913,QBOL-QUERY-SCHEMA:/tmp/2017-10-31/5031/104909913.dir/000