0

I have been trying to assign the output received from the PROJECT_DETAILS environmental variable.

PROJECT_NAME="FRONTEND"

echo $PROJECT_DETAILS | jq ".$PROJECT_NAME.PATH"    # works

OUTPUT=$PROJECT_DETAILS | jq ".$PROJECT_NAME.PATH"  # doesnt work
echo $OUTPUT                                        # empty

Maybe this is the asynchronous operation of parsing and taking time. How to handle this?

So that with one variable declared, I am trying to access other properties from output


Solved using

OUTPUT=$(echo $PROJECT_DETAILS  |  jq ".$PROJECT_NAME")  # works

In javascript we access JSON properties like:

OUTPUT={name: 'John', address: {street: 'svl'}}

OUTPUT.address.street

Can we do something similar from the above OUTPUT?

Mithun Shreevatsa
  • 3,588
  • 9
  • 50
  • 95
  • 1
    See: [How do I set a variable to the output of a command in Bash?](https://stackoverflow.com/q/4651437/3776858) – Cyrus Dec 31 '21 at 08:47
  • 1
    Try: ```OUTPUT=$(echo $PROJECT_DETAILS | jq ".$PROJECT_NAME.PATH")```. – 过过招 Dec 31 '21 at 08:49
  • This is how to process JSON from a variable and pass arguments to JQ, and store the result in a variable (since @Cyrus closed it before I could post a detailed answer): `project_details='{"FRONTEND":{"PATH":42}}'; project_name="FRONTEND"; result=$(jq -n --argjson projectDetails "$project_details" --arg projectName "$project_name" '$projectDetails | .[$projectName].PATH'); printf %d\\n "$result"` – Léa Gris Dec 31 '21 at 09:17
  • Removed answer from the question. That's not appropriate. /// Re-added it, but made it obvious it was the solution. – ikegami Dec 31 '21 at 22:24
  • I'll not sure it's appropriate to answer this; what specific part of it is not duplicative? – Charles Duffy Jan 01 '22 at 17:06
  • By the way, you're just dealing with regular shell variables here, not environment variables. That's generally desirable: environment variables are stored in the limited OS-allocated space also used for command line arguments, whereas shell variables are stored on the heap; you should only make something an environment variable instead of a regular shell variable with a specific and compelling reason to do so, since every environment variable you export makes your maximum command line length shorter. – Charles Duffy Jan 01 '22 at 17:07
  • And no, bash does not have any kind of nested map data structure. There are maps, but they're just string->string; you can't have string->map. – Charles Duffy Jan 01 '22 at 17:18

0 Answers0