I’m trying to display arrays with accent in the result but only arrays that don’t have accent are showing.
Complete themoviedb API: https://api.themoviedb.org/3/movie/566525?api_key=b2f8880475c888056b6207067fbaa197&language=pt-BR
JavaScript
x
19
19
1
"genres": [
2
{
3
"id": 28,
4
"name": "Ação"
5
},
6
{
7
"id": 12,
8
"name": "Aventura"
9
},
10
{
11
"id": 14,
12
"name": "Fantasia"
13
}
14
],
15
16
"overview": "Shang-Chi precisa confrontar o passado que pensou ter deixado para trás. Ao mesmo tempo, ele é envolvido em uma teia de mistérios da organização conhecida como Dez Anéis.",
17
18
19
Shell code:
JavaScript
1
12
12
1
getMovieInfo()
2
{
3
4
movieInfo=$(httpGet "https://api.themoviedb.org/3/movie/566525?api_key=b2f8880475c888056b6207067fbaa197&append_to_response=videos&language=pt-BR")
5
genreOne=$(echo "$movieInfo" | python -c "import sys, json; print json.load(sys.stdin)['genres'][0]['name']" 2> /dev/null )
6
genreTwo=$(echo "$movieInfo" | python -c "import sys, json; print json.load(sys.stdin)['genres'][1]['name']" 2> /dev/null )
7
genreThree=$(echo "$movieInfo" | python -c "import sys, json; print json.load(sys.stdin)['genres'][2]['name']" 2> /dev/null )
8
genres=$(echo "$genreOne $genreTwo $genreThree" | tr " " ",")
9
overview=$(echo "$movieInfo" | python -c "import sys, json; print json.load(sys.stdin)['overview']" 2> /dev/null )
10
}
11
12
result:
JavaScript
1
8
1
==========================================
2
| Title: Shang-Chi and the Legend of the Ten Rings
3
| Language: en
4
| Genre: ,Aventura,Fantasia
5
| Runtime: 132 mins
6
| Overview:
7
==========================================
8
Advertisement
Answer
Here is a cleaner way to do this in jq. This solution also scales better (you don’t need to know the number of elements in your array)
JavaScript
1
8
1
my_function() {
2
movieInfo="$(httpGet "https://api.themoviedb.org/3/movie/566525?api_key=b2f8880475c888056b6207067fbaa197&append_to_response=videos&language=pt-BR")"
3
language="$(jq -r '.original_language' <<< $movieInfo)"
4
genres="$(jq -r '[.genres[].name] | join(",")' <<< $movieInfo)"
5
runtime="$(jq -r '.runtime' <<< $movieInfo)"
6
overview="$(jq -r '.overview' <<< $movieInfo)"
7
}
8