Array size in a JSON in Shell Script

Asked

Viewed 26 times

0

I would like to take the size of a vector in a JSON in the scriipt shell but I’m only able to get the size of each vector string, that is, for the example :

#!/bin/bash

j='{"Nomes": { "nome": ["maria", "jose", "vitor","caio"] }}'

echo $j
echo  $j | jq -r ".Nomes | .nome[] | length"

Comes out beyond JSON:

5
4
5
4

But I wanted to know 4 that is the amount of items in the vector.

1 answer

0


Try to remove the []:

echo  $j | jq -r ".Nomes.nome | length"

Browser other questions tagged

You are not signed in. Login or sign up in order to post.