I have this code where I want to check if a name attribute in JSON objects contains a particular word.
In the fetchLog function, I am using jq to query objects that contain a particular word. The function checkName in the jq code to check if the name attribute contains the keyword $1 which is an argument to the function fetchLog.
function fetchLog(){
jq -r '.roots.bookmark_bar.children[]' | \
jq '{name,url} | select((checkName .name $1) == true)' bookmark.json
}
function checkName()
{
if [[ $1 == *"$2"* ]]; then
echo true
else
echo false
fi
}
bookmark.json contain:
{
"name": "Networking",
"url": "https://www.youtube.com/watch?v=AhOU2eOpmX0&list=PLIFyRwBY_4bRLmKfP1KnZA6rZbRHtxmXi&index=7"
}
{
"name": "algorithm - How are ssl certificates verified? - Stack Overflow",
"url": "https://stackoverflow.com/questions/188266/how-are-ssl-certificates-verified"
}
{
"name": "(170) What is SSL & TLS ? What is HTTPS ? What is an SSL VPN? - Practical TLS - YouTube",
"url": "https://www.youtube.com/watch?v=HMoFvRK4HUo&list=PLIFyRwBY_4bTwRX__Zn4-letrtpSj1mzY"
}
When I run fetchLog networking
.
I keep getting an error:
+ jq '{name,url} | select((checkName .name $1) == true)'
jq: error: syntax error, unexpected '$' (Unix shell quoting issues?) at <top-level>, line 1:
{name,url} | select((checkName .name $1) == true)
jq: 1 compile error
I expected the output of
fetchLog networking
to be
{
"name": "Networking",
"url": "https://www.youtube.com/watch?v=AhOU2eOpmX0&list=PLIFyRwBY_4bRLmKfP1KnZA6rZbRHtxmXi&index=7"
}
instead of the error:
+ jq '{name,url} | select((checkName .name $1) == true)'
jq: error: syntax error, unexpected '$' (Unix shell quoting issues?) at <top-level>, line 1:
{name,url} | select((checkName .name $1) == true)
jq: 1 compile error