JQ-无法从Ubibot json提取值

时间:2019-02-06 04:45:57

标签: json jq

我有一个Ubibot ws-1,我正在尝试从以下JSON中的last_values中提取时间戳,温度,湿度,光和电压

API JSON输出为

{
  "channel": {
    "channel_id": "1111",
    "name": "C-1111",
    "field1": "Temperature",
    "field2": "Humidity",
    "field3": "Light",
    "field4": "Voltage",
    "field5": "WIFI RSSI",
    "field6": "Vibration Index",
    "field7": "Knocks",
    "field8": "External Temperature Probe",
    "field9": "Reed Sensor",
    "field10": null,
    "latitude": "1",
    "longitude": "1",
    "elevation": null,
    "created_at": "2019-02-05T05:16:43Z",
    "updated_at": "2019-02-05T06:11:38Z",
    "metadata": "{\"fn_dp\":900,\"fn_th\":300,\"fn_light\":300,\"fn_acc_act\":0,\"thres_acc_min\":0,\"fn_acc_tap1\":0,\"fn_acc_tap2\":0,\"fn_ext_t\":300,\"fn_battery\":7200,\"fn_485_th\":0,\"fn_485_sth\":0,\"net_mode\":0,\"no_net_fn\":1,\"cg_data_led\":1,\"wifi_mode\":1}",
    "tags": null,
    "public_flag": "false",
    "url": null,
    "description": null,
    "write_key": "1b0f2b31a6d522a376782a90f4be0270",
    "user_id": "9186F150-9203-492B-B31A-77077E15E461",
    "last_entry_id": "583",
    "last_entry_date": "2019-02-05T19:12:47Z",
    "usage": "96328",
    "device_id": "17a867e84624895f6dab0346a4cba8cfd8279298",
    "status": "{\"ssid\":\"hydra1\",\"status\":\"ssid=wifi,usb=1\",\"usb\":\"1\"}",
    "status_date": "1549393967",
    "last_ip": "nn.nn.nn.nnn",
    "channel_icon": null,
    "product_id": "ubibot-ws1",
    "plan_code": "ubibot_free",
    "plan_start": "2019-02-05T05:16:43Z",
    "plan_end": null,
    "bill_start": "2019-02-05T05:16:43Z",
    "bill_end": "2019-03-07T05:16:43Z",
    "traffic_out": "188763",
    "traffic_in": "33767",
    "full_dump": "0",
    "renew": null,
    "last_values": "{\"field1\":{\"value\":27.186996,\"created_at\":\"2019-02-05T19:12:39Z\"},\"field3\":{\"value\":10.37,\"created_at\":\"2019-02-05T19:12:39Z\"},\"field2\":{\"value\":29,\"created_at\":\"2019-02-05T19:12:39Z\"},\"field5\":{\"value\":-35,\"created_at\":\"2019-02-05T19:12:41Z\"},\"field4\":{\"value\":5,\"created_at\":\"2019-02-05T17:27:38Z\"}}",

  }
}

我的jq技能很差.channel.last_values."\(.field1)给了我jq: error (at <stdin>:0): Cannot index string with string "null"

有人可以为我提供正确的jq吗?预先感谢

1 个答案:

答案 0 :(得分:1)

您需要首先将last_values解析为JSON值。为此,请使用fromjson

jq '.channel.last_values|fromjson.field1' file