Use jq to replace array values from dictionary
up vote
1
down vote
favorite
I have a dictionary which looks like:
cat dictionary.json
[
"key": "key01",
"value": "value01"
,
"key": "key02",
"value": "value02"
,
"key": "key03",
"value": "value03",
"extraProperty":
"foo": "bar"
,
"key": "key04",
"value": "value04"
]
Then, I have an array which is:
echo $array
key01 key02 key03
Expected output:
value01 value02 value03
I have some trouble to make jq using an array which is not json format.
I tried various solutions that I found, but none of them worked.
This post jq - How to select objects based on a 'whitelist' of property values seems to solve a similar problem but it doesn't work with my input:
echo $array | jq --argfile whitelist dictionary.json 'select(any(.key== $whitelist; .value))'
parse error: Invalid numeric literal at line 1, column 6
I also tried to use
jq -n --arg array $array --argfile whitelist dico.json 'select(any(.key== $whitelist; .valuee))'
jq: error: key02/0 is not defined at <top-level>, line 1:
key02
jq: 1 compile error
Thanks!
arrays json bash dictionary jq
add a comment |
up vote
1
down vote
favorite
I have a dictionary which looks like:
cat dictionary.json
[
"key": "key01",
"value": "value01"
,
"key": "key02",
"value": "value02"
,
"key": "key03",
"value": "value03",
"extraProperty":
"foo": "bar"
,
"key": "key04",
"value": "value04"
]
Then, I have an array which is:
echo $array
key01 key02 key03
Expected output:
value01 value02 value03
I have some trouble to make jq using an array which is not json format.
I tried various solutions that I found, but none of them worked.
This post jq - How to select objects based on a 'whitelist' of property values seems to solve a similar problem but it doesn't work with my input:
echo $array | jq --argfile whitelist dictionary.json 'select(any(.key== $whitelist; .value))'
parse error: Invalid numeric literal at line 1, column 6
I also tried to use
jq -n --arg array $array --argfile whitelist dico.json 'select(any(.key== $whitelist; .valuee))'
jq: error: key02/0 is not defined at <top-level>, line 1:
key02
jq: 1 compile error
Thanks!
arrays json bash dictionary jq
Welcome to SO. Stack Overflow is a question and answer site for professional and enthusiast programmers. The goal is that you add some code of your own to your question to show at least the research effort you made to solve this yourself.
– Cyrus
Nov 10 at 18:32
Thanks for welcoming. I edited my answer
– wazairi
Nov 10 at 18:41
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have a dictionary which looks like:
cat dictionary.json
[
"key": "key01",
"value": "value01"
,
"key": "key02",
"value": "value02"
,
"key": "key03",
"value": "value03",
"extraProperty":
"foo": "bar"
,
"key": "key04",
"value": "value04"
]
Then, I have an array which is:
echo $array
key01 key02 key03
Expected output:
value01 value02 value03
I have some trouble to make jq using an array which is not json format.
I tried various solutions that I found, but none of them worked.
This post jq - How to select objects based on a 'whitelist' of property values seems to solve a similar problem but it doesn't work with my input:
echo $array | jq --argfile whitelist dictionary.json 'select(any(.key== $whitelist; .value))'
parse error: Invalid numeric literal at line 1, column 6
I also tried to use
jq -n --arg array $array --argfile whitelist dico.json 'select(any(.key== $whitelist; .valuee))'
jq: error: key02/0 is not defined at <top-level>, line 1:
key02
jq: 1 compile error
Thanks!
arrays json bash dictionary jq
I have a dictionary which looks like:
cat dictionary.json
[
"key": "key01",
"value": "value01"
,
"key": "key02",
"value": "value02"
,
"key": "key03",
"value": "value03",
"extraProperty":
"foo": "bar"
,
"key": "key04",
"value": "value04"
]
Then, I have an array which is:
echo $array
key01 key02 key03
Expected output:
value01 value02 value03
I have some trouble to make jq using an array which is not json format.
I tried various solutions that I found, but none of them worked.
This post jq - How to select objects based on a 'whitelist' of property values seems to solve a similar problem but it doesn't work with my input:
echo $array | jq --argfile whitelist dictionary.json 'select(any(.key== $whitelist; .value))'
parse error: Invalid numeric literal at line 1, column 6
I also tried to use
jq -n --arg array $array --argfile whitelist dico.json 'select(any(.key== $whitelist; .valuee))'
jq: error: key02/0 is not defined at <top-level>, line 1:
key02
jq: 1 compile error
Thanks!
arrays json bash dictionary jq
arrays json bash dictionary jq
edited Nov 10 at 18:46
asked Nov 10 at 18:25
wazairi
83
83
Welcome to SO. Stack Overflow is a question and answer site for professional and enthusiast programmers. The goal is that you add some code of your own to your question to show at least the research effort you made to solve this yourself.
– Cyrus
Nov 10 at 18:32
Thanks for welcoming. I edited my answer
– wazairi
Nov 10 at 18:41
add a comment |
Welcome to SO. Stack Overflow is a question and answer site for professional and enthusiast programmers. The goal is that you add some code of your own to your question to show at least the research effort you made to solve this yourself.
– Cyrus
Nov 10 at 18:32
Thanks for welcoming. I edited my answer
– wazairi
Nov 10 at 18:41
Welcome to SO. Stack Overflow is a question and answer site for professional and enthusiast programmers. The goal is that you add some code of your own to your question to show at least the research effort you made to solve this yourself.
– Cyrus
Nov 10 at 18:32
Welcome to SO. Stack Overflow is a question and answer site for professional and enthusiast programmers. The goal is that you add some code of your own to your question to show at least the research effort you made to solve this yourself.
– Cyrus
Nov 10 at 18:32
Thanks for welcoming. I edited my answer
– wazairi
Nov 10 at 18:41
Thanks for welcoming. I edited my answer
– wazairi
Nov 10 at 18:41
add a comment |
2 Answers
2
active
oldest
votes
up vote
2
down vote
accepted
Here
jq -r --arg array "$array"
'from_entries | .[($array | split(" "))]'
dictionary.json
Output
value01
value02
value03
See man jq for further information.
1
That works perfectly! Thanks you!
– wazairi
Nov 12 at 12:02
Last question: is that possible to display the original key when not found in the dictionary? Like: Input: key01 key02 key03 notFound Output: value01 value02 value03 notFound
– wazairi
Nov 12 at 13:21
Ask another question about it.
– oguzismail
Nov 12 at 13:56
add a comment |
up vote
1
down vote
Using INDEX/2, which constructs a dictionary:
echo 'key01 key02 key03' |
jq -Rr --argfile dict dictionary.json '
INDEX($dict; .key) as $d
| split(" ") | map( $d[.]|.value )
| join(" ")'
yields:
value01 value02 value03
If your jq does not have INDEX, then now would be an excellent time to upgrade to jq 1.6; alternatively, you can simply snarf its def by googling: jq "def INDEX"
I should have added that I have to use jq 1.5
– wazairi
Nov 12 at 12:03
1
So copy the def, available by googlingjq “def INDEX"
– peak
Nov 12 at 12:18
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
Here
jq -r --arg array "$array"
'from_entries | .[($array | split(" "))]'
dictionary.json
Output
value01
value02
value03
See man jq for further information.
1
That works perfectly! Thanks you!
– wazairi
Nov 12 at 12:02
Last question: is that possible to display the original key when not found in the dictionary? Like: Input: key01 key02 key03 notFound Output: value01 value02 value03 notFound
– wazairi
Nov 12 at 13:21
Ask another question about it.
– oguzismail
Nov 12 at 13:56
add a comment |
up vote
2
down vote
accepted
Here
jq -r --arg array "$array"
'from_entries | .[($array | split(" "))]'
dictionary.json
Output
value01
value02
value03
See man jq for further information.
1
That works perfectly! Thanks you!
– wazairi
Nov 12 at 12:02
Last question: is that possible to display the original key when not found in the dictionary? Like: Input: key01 key02 key03 notFound Output: value01 value02 value03 notFound
– wazairi
Nov 12 at 13:21
Ask another question about it.
– oguzismail
Nov 12 at 13:56
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
Here
jq -r --arg array "$array"
'from_entries | .[($array | split(" "))]'
dictionary.json
Output
value01
value02
value03
See man jq for further information.
Here
jq -r --arg array "$array"
'from_entries | .[($array | split(" "))]'
dictionary.json
Output
value01
value02
value03
See man jq for further information.
edited Nov 12 at 12:05
answered Nov 10 at 19:55
oguzismail
2,120517
2,120517
1
That works perfectly! Thanks you!
– wazairi
Nov 12 at 12:02
Last question: is that possible to display the original key when not found in the dictionary? Like: Input: key01 key02 key03 notFound Output: value01 value02 value03 notFound
– wazairi
Nov 12 at 13:21
Ask another question about it.
– oguzismail
Nov 12 at 13:56
add a comment |
1
That works perfectly! Thanks you!
– wazairi
Nov 12 at 12:02
Last question: is that possible to display the original key when not found in the dictionary? Like: Input: key01 key02 key03 notFound Output: value01 value02 value03 notFound
– wazairi
Nov 12 at 13:21
Ask another question about it.
– oguzismail
Nov 12 at 13:56
1
1
That works perfectly! Thanks you!
– wazairi
Nov 12 at 12:02
That works perfectly! Thanks you!
– wazairi
Nov 12 at 12:02
Last question: is that possible to display the original key when not found in the dictionary? Like: Input: key01 key02 key03 notFound Output: value01 value02 value03 notFound
– wazairi
Nov 12 at 13:21
Last question: is that possible to display the original key when not found in the dictionary? Like: Input: key01 key02 key03 notFound Output: value01 value02 value03 notFound
– wazairi
Nov 12 at 13:21
Ask another question about it.
– oguzismail
Nov 12 at 13:56
Ask another question about it.
– oguzismail
Nov 12 at 13:56
add a comment |
up vote
1
down vote
Using INDEX/2, which constructs a dictionary:
echo 'key01 key02 key03' |
jq -Rr --argfile dict dictionary.json '
INDEX($dict; .key) as $d
| split(" ") | map( $d[.]|.value )
| join(" ")'
yields:
value01 value02 value03
If your jq does not have INDEX, then now would be an excellent time to upgrade to jq 1.6; alternatively, you can simply snarf its def by googling: jq "def INDEX"
I should have added that I have to use jq 1.5
– wazairi
Nov 12 at 12:03
1
So copy the def, available by googlingjq “def INDEX"
– peak
Nov 12 at 12:18
add a comment |
up vote
1
down vote
Using INDEX/2, which constructs a dictionary:
echo 'key01 key02 key03' |
jq -Rr --argfile dict dictionary.json '
INDEX($dict; .key) as $d
| split(" ") | map( $d[.]|.value )
| join(" ")'
yields:
value01 value02 value03
If your jq does not have INDEX, then now would be an excellent time to upgrade to jq 1.6; alternatively, you can simply snarf its def by googling: jq "def INDEX"
I should have added that I have to use jq 1.5
– wazairi
Nov 12 at 12:03
1
So copy the def, available by googlingjq “def INDEX"
– peak
Nov 12 at 12:18
add a comment |
up vote
1
down vote
up vote
1
down vote
Using INDEX/2, which constructs a dictionary:
echo 'key01 key02 key03' |
jq -Rr --argfile dict dictionary.json '
INDEX($dict; .key) as $d
| split(" ") | map( $d[.]|.value )
| join(" ")'
yields:
value01 value02 value03
If your jq does not have INDEX, then now would be an excellent time to upgrade to jq 1.6; alternatively, you can simply snarf its def by googling: jq "def INDEX"
Using INDEX/2, which constructs a dictionary:
echo 'key01 key02 key03' |
jq -Rr --argfile dict dictionary.json '
INDEX($dict; .key) as $d
| split(" ") | map( $d[.]|.value )
| join(" ")'
yields:
value01 value02 value03
If your jq does not have INDEX, then now would be an excellent time to upgrade to jq 1.6; alternatively, you can simply snarf its def by googling: jq "def INDEX"
answered Nov 10 at 20:36
peak
28.5k73752
28.5k73752
I should have added that I have to use jq 1.5
– wazairi
Nov 12 at 12:03
1
So copy the def, available by googlingjq “def INDEX"
– peak
Nov 12 at 12:18
add a comment |
I should have added that I have to use jq 1.5
– wazairi
Nov 12 at 12:03
1
So copy the def, available by googlingjq “def INDEX"
– peak
Nov 12 at 12:18
I should have added that I have to use jq 1.5
– wazairi
Nov 12 at 12:03
I should have added that I have to use jq 1.5
– wazairi
Nov 12 at 12:03
1
1
So copy the def, available by googling
jq “def INDEX"– peak
Nov 12 at 12:18
So copy the def, available by googling
jq “def INDEX"– peak
Nov 12 at 12:18
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53242090%2fuse-jq-to-replace-array-values-from-dictionary%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Welcome to SO. Stack Overflow is a question and answer site for professional and enthusiast programmers. The goal is that you add some code of your own to your question to show at least the research effort you made to solve this yourself.
– Cyrus
Nov 10 at 18:32
Thanks for welcoming. I edited my answer
– wazairi
Nov 10 at 18:41