html tool

显示标签为“jq”的博文。显示所有博文
显示标签为“jq”的博文。显示所有博文

2024年11月18日星期一

jq处理key中包含点的方式

 参考:https://blog.csdn.net/weixin_44745147/article/details/124035982

ll文件中如下

{

  "tcp.alproto.change.detect.alproto.fail": 0

}

解决:
# cat ll|./jq '.counter.stream."tcp.alproto.change.detect.alproto.fail"'
0

2023年11月30日星期四

jq获得全部的json的keys

 参考:https://alingse.github.io/jq-manual-cn/manual/v1.5/#TypesandValues

处理方式:

cat *.json|jq "path(..)" 

输出即全部keys,类似如下:



原文:

Note that the path expressions are not different from normal expressions. The expression path(..|select(type=="boolean")) outputs all the paths to boolean values in ., and only those paths.

examples:

  • program: ‘path(.a[0].b)’ input: ‘null’ output: [’[“a”,0,”b”]’]
  • program: ‘[path(..)]’ input: ‘{“a”:[{“b”:1}]}’ output: [’[[],[“a”],[“a”,0],[“a”,0,”b”]]’]

2023年2月27日星期一

key中包含空格,在jq中的引用

 参考:https://hellogitlab.com/OS/Centos/json_tool_jq.html#_4-2-%E5%AF%B9%E8%B1%A1%E6%A0%87%E8%AF%86%E7%AC%A6-%E7%B4%A2%E5%BC%95-foo%E3%80%81-foo-bar

# cat a

{"response_code":0,"verbose_msg":"ok","timestamp":1677555522,"data":{"host":"192.168.120.103","total":534,"schedule":0,"static":251,"engine":0,"dynamic":283,"details":{"dynamic":{"ready ":262,"unacked ":21,"total ":283,"incoming":0.2,"deliver ":0.4,"ack ":0.2},"engine":{"ready ":0,"unacked ":0,"total ":0,"incoming":0,"deliver ":0,"ack ":0},"schedule":{"ready ":0,"unacked ":0,"total ":0,"incoming":0.2,"deliver ":0.2,"ack ":0.2},"static":{"ready ":234,"unacked ":17,"total ":251,"incoming":0,"deliver ":0,"ack ":0}}}}


# cat a|jq -r '.data.details.dynamic|[."total ",."ready ",."unacked ",.incoming,."deliver ",."ack "]| @csv'

283,262,21,0.2,0.4,0.2


转:jq打印多个key使用,分割

 参考:https://cloud.tencent.com/developer/ask/sof/37939

打印为CSV/TSV

$ cat file.json | jq -r '.users[] | [.first, .last] | @tsv'
Stevie  Wonder
Michael Jackson
cat file.json | jq -r '.users[] | [.first, .last] | @csv'
"Stevie","Wonder"
"Michael","Jackson"

第一个表达式.users[]将对象从最外层的数组中取消嵌套,如问题中给出的代码所示。下一个表达式[.first, .last]为每个输入对象创建一个新的值数组,最后一个表达式使用内置函数@tsv和@csv分别将所有输入数组打印为制表符分隔值和逗号分隔值。

2021年11月2日星期二

转:shell 中 urlencode

 https://stackoverflow.com/questions/296536/how-to-urlencode-data-for-curl-command


方法一:

sed -f  url_escape.sed

And if you really really feel that you need an external tool (well, it will go a lot faster, and might do binary files and such...) I found this on my OpenWRT router...

replace_value=$(echo $replace_value | sed -f /usr/lib/ddns/url_escape.sed)

Where url_escape.sed was a file that contained these rules:

# sed url escaping
s:%:%25:g
s: :%20:g
s:<:%3C:g
s:>:%3E:g
s:#:%23:g
s:{:%7B:g
s:}:%7D:g
s:|:%7C:g
s:\\:%5C:g
s:\^:%5E:g
s:~:%7E:g
s:\[:%5B:g
s:\]:%5D:g
s:`:%60:g
s:;:%3B:g
s:/:%2F:g
s:?:%3F:g
s^:^%3A^g
s:@:%40:g
s:=:%3D:g
s:&:%26:g
s:\$:%24:g
s:\!:%21:g
s:\*:%2A:g


方法二:

 use jq:

$ printf %s 'encode this'|jq -sRr @uri
encode%20this
$ jq -rn --arg x 'encode this' '$x|@uri'
encode%20this

2021年4月22日星期四

问题: JQ doesn't support dash - character in dictionary key names?

 https://github.com/stedolan/jq/issues/1464


.content.book1."field-2"

returns

"value-2"

$ cat tmp.json | jq '.content.book1."field-2"'
"value-2"

$ cat tmp.json | jq '.content.book1["field-three"]'
{
  "name": "THIRD",
  "url": "book1/field-three/",
  "short-url": "book1/field-three/chota-chetan"
}

2021年4月16日星期五

jq:select 判断是否为空

 https://stedolan.github.io/jq/manual/

def recurse(f): def r: ., (f | select(. != null) | r); r;

2021年3月21日星期日

jq-过滤指定子节点的某个key值

 https://stackoverflow.com/questions/42097410/how-to-check-for-presence-of-key-in-jq-before-iterating-over-the-values

$ jq '.result 
  | .property_history?  
  | .[]
  | select(.event_name == "Sold")  #在指定的key中搜索
  | .date'
"08/30/2004"


JSON:

{  
   "result":{  
      "property_history":[  
         {  
            "date":"01/27/2016",
            "price_changed":0,
            "price":899750,
            "event_name":"Listed",
            "sqft":0
         },
         {  
            "date":"12/15/2015",
            "price_changed":0,
            "price":899750,
            "event_name":"Listed",
            "sqft":2357
         },
         {  
            "date":"08/30/2004",
            "price_changed":0,
            "price":739000,
            "event_name":"Sold",
            "sqft":2357
         }
      ]
   }
}

2020年5月21日星期四

python 中使用jq

参考:https://pypi.org/project/jq/

setup
pip install jq

 
code

1 #-*-coding:utf8-*-
  2 import jq, re
  3 import json
  4  
  5 a = {"t":{"t1":"a","a":"b"}, "e":"e1"}
  6 print jq.compile(".t.a").input(a).first() 
  7 assert jq.compile(".").input("hello").first() == "hello"



2018年8月6日星期一

jq get json keys



https://stackoverflow.com/questions/23118341/how-to-get-key-names-from-json-using-jq

You need to use jq 'keys[]'. For example:
echo '{"example1" : 1, "example2" : 2, "example3" : 3}' | jq 'keys[]'
Will output a line separated list:
"example1"
"example2"
"example3"

2018年7月19日星期四

sort by json value



http://bigdatums.net/2016/11/29/sorting-json-by-value-with-jq/
[popexizhi:
自己的问题本来是同一个json体中的[]的每个key的value 排序,下面这个是同多个json同结构sort。不过这个还好,可以处理一下[]中的内容成多个json同结构再sort
]

SORTING JSON BY VALUE WITH JQ (COMMAND LINE JSON PROCESSOR)

jq is a lightweight command line JSON processor that is very easy to use. Sometimes it is helpful to see your data sorted by a particular field value. Luckily jq makes this easy to do. Here are some sample JSON records we will be working with in this post:
Sorting JSON by value with jq can easily be done by using the sort_by() function. The main trick with using the sort_by() function is that your JSON input must be in an array. There are different ways to do this in jq (if your data is not already like this), including using the -s or --slurp option. The --slurp option will read all JSON input into a JSON array. From this point the data can be sorted by value. You can use the .[] syntax to return all elements of the array.

Sorting by a Single Value Example

Sorting by Multiple Values Example