jq

Check if string is a valid JSON with jq

六月ゝ 毕业季﹏ 提交于 2020-06-24 07:07:16
问题 I need to catch an error when lifting a service. The response can be null , a string error message like error services-migration/foobar: Not found: services-migration/foobar or a valid JSON when everything is fine. I was wondering if there is a way with jq to simply check if the provided string is a valid JSON. I could ofc check the string for some keywords like error f.e., but I'm looking for a more robust option, where eg. I get a true/false or 1/0 from jq. I was looking through the docs of

jq dates and unix timestamps

本小妞迷上赌 提交于 2020-06-24 05:43:07
问题 So I have a data with bunch of unix timestamp values (in milliseconds). Something like this: { "id": "f6922fd5-4f97-4113-820e-b45eba0ae236", "published_at": 1461624333859, "tracking_id": "a85d5ed5-5efa-461b-aae0-beb2098c0ff7", }, { "id": "835d412f-5162-440c-937b-7276f22c4eb9", "published_at": 1461625249934, "tracking_id": "86472ba2-ce5f-400f-b42a-5a0ac155c42c", }, { "id": "bc2efcac-67a0-4855-856a-f31ce5e4618e", "published_at": 1461625253393, "tracking_id": "c005398f-07f8-4a37-b96d

jq dates and unix timestamps

99封情书 提交于 2020-06-24 05:43:06
问题 So I have a data with bunch of unix timestamp values (in milliseconds). Something like this: { "id": "f6922fd5-4f97-4113-820e-b45eba0ae236", "published_at": 1461624333859, "tracking_id": "a85d5ed5-5efa-461b-aae0-beb2098c0ff7", }, { "id": "835d412f-5162-440c-937b-7276f22c4eb9", "published_at": 1461625249934, "tracking_id": "86472ba2-ce5f-400f-b42a-5a0ac155c42c", }, { "id": "bc2efcac-67a0-4855-856a-f31ce5e4618e", "published_at": 1461625253393, "tracking_id": "c005398f-07f8-4a37-b96d

Get JSON array object at index satisfying a filter condition using jq

偶尔善良 提交于 2020-06-17 09:59:27
问题 I need to get the entire object(s) in json array returned by list-action-executions command from aws cli, where the attributes satisfy the condition below "stageName": "DeployStage", "actionName": "PromoteToProdApprovalGate", The output from the awscli command is of the format. The actual output contains about 40 array elements and more than 1 satisfying the above conditions. I would like to get all of them as full objects. How can I do it with JQ? I tried the select and index options, but

How do I extract a key with jq based on its child values

匆匆过客 提交于 2020-06-09 05:40:45
问题 I'm trying to process some JSON with jq. Specifically, I want a particular key, based on its child value. Example, given: { "foo": {"primary": true, "blah": "beep"}, "bar": {"primary": false, "blah": "narf"}, "baz": {"primary": false, "blah": "poink"}, } I want the string "foo", because that is the key whose child value "primary' is true. (I can guarantee that one and only one entry will have primary = true, due to what's generating the JSON.) So far the best I've been able to manage is: jq

How do I extract a key with jq based on its child values

微笑、不失礼 提交于 2020-06-09 05:40:06
问题 I'm trying to process some JSON with jq. Specifically, I want a particular key, based on its child value. Example, given: { "foo": {"primary": true, "blah": "beep"}, "bar": {"primary": false, "blah": "narf"}, "baz": {"primary": false, "blah": "poink"}, } I want the string "foo", because that is the key whose child value "primary' is true. (I can guarantee that one and only one entry will have primary = true, due to what's generating the JSON.) So far the best I've been able to manage is: jq

jq: error: round/0 is not defined at <top-level>

杀马特。学长 韩版系。学妹 提交于 2020-05-30 07:37:45
问题 round function in jq doesn't work. $ jq '10.01 | round' jq: error: round/0 is not defined at <top-level>, line 1: 10.01 | round jq: 1 compile error $ jq --help jq - commandline JSON processor [version 1.5-1-a5b5cbe] What I need to do? 回答1: Seems like round is unavailable in your build. Either upgrade jq or implement round using floor : def round: . + 0.5 | floor; Usage example: $ jq -n 'def round: . + 0.5 | floor; 10.01 | round' 10 来源: https://stackoverflow.com/questions/56593531/jq-error

JQ issues with comments on Json file

吃可爱长大的小学妹 提交于 2020-05-29 05:14:55
问题 I'm using JQ https://stedolan.github.io/jq/ to work in bash with my json and when I read the json is throwing me an error parse error: Invalid numeric literal at line 2, column 5= Since my json has some comments // comment "spawn": {} I've been seen looking the options and I cannot find any option to fix the problem. Any idea how to solve it? 回答1: JSON and thus jq do not support comments (in the usual sense) in JSON input. The jq FAQ lists a number of tools that can be used to remove comments

get the first (or n'th) element in a jq json parsing

白昼怎懂夜的黑 提交于 2020-05-25 08:57:10
问题 I can get the 1st element in a json inside [] $ echo '[{"a":"x", "b":true}, {"a":"XML", "b":false}]' | jq '.[1]' { "a": "XML", "b": false } But if the json is already disassembled (for instance, after filtering entries using 'select'), how can I choose a single entry and avoid the error seen here? $ echo '[{"a":"x", "b":true}, {"a":"x", "b":false},{"a":"XML", "b":false}]' | jq '.[] | select( .a == "x")' { "a": "x", "b": true } { "a": "x", "b": false } $ echo '[{"a":"x", "b":true}, {"a":"x",

get the first (or n'th) element in a jq json parsing

隐身守侯 提交于 2020-05-25 08:57:06
问题 I can get the 1st element in a json inside [] $ echo '[{"a":"x", "b":true}, {"a":"XML", "b":false}]' | jq '.[1]' { "a": "XML", "b": false } But if the json is already disassembled (for instance, after filtering entries using 'select'), how can I choose a single entry and avoid the error seen here? $ echo '[{"a":"x", "b":true}, {"a":"x", "b":false},{"a":"XML", "b":false}]' | jq '.[] | select( .a == "x")' { "a": "x", "b": true } { "a": "x", "b": false } $ echo '[{"a":"x", "b":true}, {"a":"x",