简体   繁体   中英

JQ - Cannot extract values from Ubibot json

I have a Ubibot ws-1 and I'm trying to extract Timestamp, Temperature, Humidity, Light and voltage from last_values in the following JSON

The API JSON output is

{
  "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\"}}",

  }
}

my jq skills are very poor .channel.last_values."\\(.field1) gives me jq: error (at <stdin>:0): Cannot index string with string "null" .

Can someone help me with the correct jq? Thanks in advance

You need to parse last_values as JSON value first. To do that, use fromjson :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM