スポンサーリンク
linuxのCLI上でjsonを扱うためには欠かせないjqコマンド。しかし出力がjson形式に縛られるので困るという方がいらっしゃるのではないでしょうか。
実を言うとjqではjson以外も出力できます。
今回はそのやり方をメモしていきたいと思います。
jqコマンドがインストールされているlinux環境ならばだいたいOKだと思われます
CentOS系にインストールする場合はこちらの記事: CentOS7に15秒でjqコマンドをインストールするをどうぞ。
以下のようなjsonの入力値を例に説明します。
# cat json.txt | jq "."
{
"users": [
{
"name": "Mura",
"age": 30
},
{
"name": "Ken",
"age": 29
}
]
}
上記のjsonを元に以下の文を出力します。
Mura is 30 years old
Ken is 29 years old
早速ですがやり方です。
コマンド
cat json.txt | jq '.users[] | .name = .name + " is " + (.age|tostring) + " years old" | .name'
結果
"Mura is 30 years old"
"Ken is 29 years old"
できました。
ポイントは2点あります。
+
を使うことで文字列を変更するtostring
を使い数値型を文字列型に変更するそれぞれ詳細を解説します。
説明しているのは
.name = .name + " is " + (.age|tostring) + " years old"
の部分です。
ここの意味は name というキーの値(=Mura と Ken)に対して "is " と (.age|tostring) と "years old" という文字を結合する という意味です。
ここで結合してできた新しい .name
を最後に表示しているわけです。
次は以下の部分の説明です。
(.age|tostring)
これは キー age
のvalue、つまり 30
と 29
を文字列に変換するということです。
jqには型が存在しており、数値型のまま結合しようとすると
jq: error (at <stdin>:1): string ("Mura is ") and number (30) cannot be added
というように怒られてしまいます。
以上、jqコマンドのちょっと便利な使い方でした。
スポンサーリンク
2022/04/07 19:29:17
cheapest cialis cialis sale
2022/04/07 21:40:46
order generic viagra viagra online uk buy cheap viagra viagra tablets
2022/04/08 05:06:08
free viagra without prescription buy viagra online at
2022/04/09 11:33:57
tadalafil online sublingual tadalafil pill
2022/04/09 15:08:08
tadalafil vs viagra tadalafil farmacias del ahorro
2022/04/10 02:59:56
is sildenafil covered by insurance is sildenafil citrate the same as viagra
2022/04/11 16:18:56
costco pharmacy prices what does rx mean in pharmacy
2022/04/12 02:00:55
sildenafil citrate tablets 100mg price sildenafil 100 mg uso
2022/04/11 18:32:03
description tadalafil tadalafil canada online pharmacy
2022/04/13 01:39:17
buy sildenafil mexico sildenafil otc australia
2022/04/13 09:57:13
viagra cialis levitra trial pack generic name for levitra
2022/04/13 07:45:39
cialis onine buy cialis india
2022/04/13 17:33:51
viagra without rx does sildenafil expire
2022/04/14 23:36:48
wedgewood pharmacy pet rx canadian pharmacy 1 internet online drugstore
2022/04/15 01:09:29
target store pharmacy nearest drug store
2022/04/16 18:10:14
cialis tadalafil 10 mg tadalafil cost walmart
2022/04/15 14:48:20
online pharmacy ed canadian pharmacy rx
2022/04/16 08:50:23
how much is ivermectin stromectol online bestellen
2022/04/16 11:24:02
cvs pharmacy near me voltaren canadian pharmacies
2022/04/18 01:13:04
viagra 50mg online in india sildenafil citrate women
2022/04/18 02:46:44
how to buy generic cialis online generic cialis cheapest
2022/04/18 19:42:22
stromectol dosage ivermectin plus for goats
2022/04/19 17:46:13
levitra cost at walgreens levitra online coupons
2022/04/19 19:48:59
levitra generic release date levitra generico 20 mg
2022/04/20 02:51:29
viagra vs cialis vs levitra alcohol and cialis
2022/04/21 09:14:17
sildenafil buy from canada generic viagra tablets
2022/04/21 07:44:20
online canadian pharmacies regulated by health canada costco pharmacy online refill
2022/04/22 02:51:40
cialis coupon free trial cialis super active
2022/04/22 22:05:00
safe online canadian pharmacies health canada controlled drugs and substances
2022/04/23 00:12:59
Clozaril can i be denied employment due to prescription drugs
2022/04/23 07:21:59
generic pharmacy online quality rx pharmacy
2022/04/24 12:16:31
prescription drugs to stop drinking alcohol ordering prescription drugs from canada
2022/04/24 13:42:54
is it safe to buy viagra online canadian pharmacy longs drug store
2022/04/25 06:59:53
polish pharmacy online uk perscription drugs from canada
2022/04/29 09:57:32
is canadian pharmacy online legit on line pharmacy https://jzpharmacyukj.com/
2022/04/29 12:04:31
does levitra work better than cialis viagra cialis levitra vergleich https://glevitrargu.com/
2022/04/29 19:07:39
sildenafil uses sildenafil citrat https://sildenafilkiol.com/
2022/04/30 23:26:45
ivermectin for swine oral ivermectin buy https://stromectolujlo.com/
2022/05/01 00:42:10
super rx pharmacy best canadian mail order pharmacy https://dtupharmyjn.com/
2022/05/01 17:46:09
viagra pharmacy over the counter viagra 50 mg tablet online purchase https://sgiviagraikn.com/
2022/05/03 12:38:19
levitra for bph levitra vardenafil tablets https://xclevitradb.com/
2022/05/05 01:33:13
purchase viagra australia order viagra 50 mg https://aviagrasbt.com/
2022/05/05 02:54:29
cialis coupon voucher cialis australia online stores https://dccialisxvf.com/
2022/05/05 18:21:36
cost modafinil 100mg purchase provigil
2022/05/05 18:42:38
canadian overnight pharmacy Dramamine
2022/05/09 02:46:09
hcq medical abbreviation https://keys-chloroquinehydro.com/
2022/05/09 06:11:21
hydroxochlorquine hydroxychloroquine covid
2022/05/09 06:47:12
levitra viagra levitra generic usa https://xclevitradb.com/
2022/05/09 08:46:46
levitra (vardenafil) levitra over the counter https://glevitrargu.com/
2022/05/09 15:28:23
natural sildenafil substitute how to pronounce sildenafil https://sildenafilkiol.com/
2022/05/10 19:09:41
viagra generic drug where can you buy viagra in australia https://aviagrasbt.com/
2022/05/10 20:20:17
order cialis online generuc cialis https://dccialisxvf.com/
2022/05/11 11:40:21
reputable canadian pharmacies canadian neighborhood pharmacy reviews
2022/05/14 09:08:53
levitra free trial order cheap online levitra https://xclevitradb.com/
2022/05/14 11:12:31
levitra user reviews levitra comparisons to cialis and viagra https://glevitrargu.com/
2022/05/14 18:00:33
sildenafil dosage for ed sam's club sildenafil https://sildenafilkiol.com/
2022/05/15 05:51:19
how long is a doctoral dissertation
site:ameba.jp dissertation help
what is dissertation
2022/05/15 22:44:19
provigil online order modafinil 100mg oral modafinil 100mg pills
2022/05/16 04:50:55
example of a dissertation
writing your dissertation in a week
dissertation help service villeurbanne
2022/05/16 15:40:18
Glucotrol rexall pharmacy & drug store
2022/05/18 02:44:45
order modafinil 100mg pills provigil 200mg cheap
2022/05/18 12:19:05
levitra pill id levitra para que es https://xclevitradb.com/
2022/05/18 14:21:45
levitra patent expiry date levitra professional reviews https://glevitrargu.com/
2022/05/18 21:08:37
sildenafil side effect sildenafil heart https://sildenafilkiol.com/
2022/05/18 22:08:14
modafinil 100mg oral purchase modafinil online cheap
2022/05/19 14:49:12
buy modafinil sale buy modafinil 200mg online cheap modafinil 100mg without prescription
2022/05/19 17:30:24
dissertation writing companies
guide writing dissertation
dissertation thesis writing
2022/05/19 19:13:12
provigil 200mg over the counter provigil drug
2022/05/20 01:01:47
viagra online canada mastercard how to get female viagra https://aviagrasbt.com/
2022/05/20 01:14:10
buy provigil 200mg for sale buy modafinil 100mg online
2022/05/20 02:16:00
cialis use cialis lion https://dccialisxvf.com/
2022/05/20 18:46:34
mexican pharmacy what to buy Combivir