0

This works:

#!/bin/sh -e

cat << EOF > error.json
[
  {
    "path": "path1",
    "path_with_namespace": "path_with_namespace1"
  },
  {
    "path": "path2",
    "path_with_namespace": "path_with_namespace2"
  }
]
EOF

jq -c '.[]' error.json | while read PROJECT; do
  # This works
  PATH_WITH_NAMESPACE=$(echo "$PROJECT" | jq .path_with_namespace)
  echo "Shallow cloning $PATH_WITH_NAMESPACE ..."
done

Output:

$ ./error.sh 
Shallow cloning "path_with_namespace1" ...
Shallow cloning "path_with_namespace2" ...

Extracting a second value does not work:

#!/bin/sh -e

cat << EOF > error.json
[
  {
    "path": "path1",
    "path_with_namespace": "path_with_namespace1"
  },
  {
    "path": "path2",
    "path_with_namespace": "path_with_namespace2"
  }
]
EOF

jq -c '.[]' error.json | while read PROJECT; do
  # This works
  PATH_WITH_NAMESPACE=$(echo "$PROJECT" | jq .path_with_namespace)
  echo "Shallow cloning $PATH_WITH_NAMESPACE ..."

  # This does not
  PATH=$(echo "$PROJECT" | jq .path)
  echo "$PATH"
done

Output:

$ ./error.sh 
Shallow cloning "path_with_namespace1" ...
"path1"
./error.sh: line 18: jq: command not found

I've tried numerous variants of looping over arrays with bash and jq. Basically I can't work with more than one key out of the json file. I need to work with more than one. How would a solution look like?

@0stone0 quoting "$PROJECT" has no effect.

3
  • 5
    You overwrite the environment variable PATH, so the jq executable cannot be located anymore (resulting in a command not found error). By convention, never use all-caps variable names. Commented Oct 28, 2024 at 14:48
  • @pmf Thanks! Make it an answer and I will accept it as solution. Commented Oct 28, 2024 at 14:54
  • 3
    This is why you never use all-uppercase names for your own variables. Commented Oct 28, 2024 at 15:07

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.