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!










share|improve this question























  • 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















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!










share|improve this question























  • 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













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!










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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

















  • 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













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.






share|improve this answer


















  • 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

















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"






share|improve this answer




















  • 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 googling jq “def INDEX"
    – peak
    Nov 12 at 12:18










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















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

























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.






share|improve this answer


















  • 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














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.






share|improve this answer


















  • 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












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.






share|improve this answer














Here



jq -r --arg array "$array" 
'from_entries | .[($array | split(" "))]'
dictionary.json


Output



value01
value02
value03


See man jq for further information.







share|improve this answer














share|improve this answer



share|improve this answer








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












  • 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












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"






share|improve this answer




















  • 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 googling jq “def INDEX"
    – peak
    Nov 12 at 12:18














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"






share|improve this answer




















  • 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 googling jq “def INDEX"
    – peak
    Nov 12 at 12:18












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"






share|improve this answer












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"







share|improve this answer












share|improve this answer



share|improve this answer










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 googling jq “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






  • 1




    So copy the def, available by googling jq “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

















 

draft saved


draft discarded















































 


draft saved


draft discarded














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





















































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







這個網誌中的熱門文章

What does pagestruct do in Eviews?

Dutch intervention in Lombok and Karangasem

Channel Islands