Given:

{"code":200,"lang":"ru-en","text":["test"]} 

Task:

Put test into variable.

How to try:

 text = '{"code":200,"lang":"ru-en","text":["test"]}' translate = $($text | jq '.text[0]') echo $translate 

In the end, writes that it was не найдено .

Question:

Why does it take more than an hour to pull a value out of JSON ?

  • 2
    translate = $ (echo $ text | jq '.text [0]') - Vlad Vetrov
  • @VladVetrov, it works, thanks) But how should I understand that echo needed? = (I killed more than an hour on Google and mate ( - Yuriy SPb
  • @Yuriy SPb It was necessary to read the error message carefully - -bash: {"code":200,"lang":"ru-en","text":["test"]}: command not found - you need to execute the command before the pipe which text variable is not. - 0xdb
  • @ 0xdb, it seems I understood ... It just gave me this: 16: /tmp/tomcat8-tomcat8-tmp/jenkins8610740465972801402.sh: {"code":200,"lang":"ru-en","text":["test"]}: not found and I thought that I could not find it in JSON ( - YuriiSPb

1 answer 1

 text='{"code":200,"lang":"ru-en","text":["test"]}' translate="$(echo "$text" | jq '.text[0]')" echo "$translate" 

Of the important:

  • Single quotes in initialization text
  • echo and double quotes in translate

And on bash you can do this: translate="$(jq '.text[0]' <<<"$text")"

  • Thank you) I’ve been picking Google for more than an hour, but I haven’t found anywhere that I need to add echo (What did I need to know / google to understand that I should add it? - YuriiSPb
  • @Yuriy SPb, these are the basic principles of working with bash ... the original line made sense to "execute the command saved in $ text and transfer its output through the pipe to jq", which in this case is meaningless ... So it probably costs for starters what -read a bash primer to read ... - Fat-Zer
  • Understood, thank you) I just have to release the notes automatically to transfer for stuffing them in the automatic assembly of the android application and I didn’t expect to read the textbooks for a seemingly simple operation) - Yuriy SPb
  • @Yuriy SPb, I'm afraid to ask, are you writing programs without using textbooks? Can one then find out which ones, so that God forbid not to use? - 0andriy
  • @ 0andriy, when I need to write a program that makes a request to the network and writes the result to a file, I see no reason to read the tutorial. Why, if without reading it the task will be solved many times faster? But in general - this is an old argument - how best to learn - starting with theory or practice. I now have a second option. You, apparently, the first. Programs can show. For example: play.google.com/store/apps/details?id=en.dante.scpfoundation - JuriySPb