You are not logged in.

#1 2018-08-24 04:51:13

tony5429
Member
Registered: 2006-03-28
Posts: 1,017

[SOLVED] Using Bash String in jq Command

I have the following contacts.json file:

[
	{
		"name": "John",
		"email": "john@email.com"
	},
	{
		"name": "George",
		"email": "george@email.com"
	},
	{
		"name": "Sally",
		"email": "sally@email.com"
	}
]

If I run this jq command, I get the expected output:

[tony@linux ~]$ jq '.[] | select(.name == "John")' contacts.json
{
  "name": "John",
  "email": "john@email.com"
}

But if I use a bash string for the needle in the haystack, jq doesn't find it (there's no output). Does anyone know why?

[tony@linux ~]$ name="John" ; jq -r '.[] | select(.name == "$name")' contacts.json
[tony@linux ~]$

Last edited by tony5429 (2018-08-24 12:56:40)

Offline

#2 2018-08-24 05:41:44

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,424
Website

Re: [SOLVED] Using Bash String in jq Command

jq --arg name "$name" '.[] | select(.name == $name)' contacts.json

Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#3 2018-08-24 12:56:21

tony5429
Member
Registered: 2006-03-28
Posts: 1,017

Re: [SOLVED] Using Bash String in jq Command

Ah; that works! Thanks!

Offline

Board footer

Powered by FluxBB