Home > Software design >  jq bash variable is showing incomplete value
jq bash variable is showing incomplete value

Time:06-06

I have a JSON file and want to copy modules array into a bash variable. The problem I am trying to solve is I have two dependencies (similar) as below. It has modules array in each. I want to merge these two modules arrays.

base_dependencies.txt

{
    "version": "1.0.1",
    "name": "test",
    "number": "1990",
    "buildAgent": {
        "name": "GENERIC",
        "version": "2.16.4"
    },
    "agent": {
        "name": "jfrog-cli-go",
        "version": "2.16.4"
    },
    "started": "2022-05-20T15:01:48.827-0500",
    "durationMillis": 0,
    "artifactoryPrincipal": "test",
    "modules": [{
        "properties": {},
        "type": "generic",
        "id": "test",
        "artifacts": [{
            "type": "jar",
            "sha1": "bef8dd3",
            "sha256": "1c2ba8cb5369d",
            "md5": "f60031d2a0ef13dd8505b61af90c1c44",
            "name": "test-0-SNAPSHOT16.jar",
            "path": "libs-release-local/libs7/test-0-SNAPSHOT16.jar"
        }]
    }]
}

When i run

jq '.modules[]' base_dependency.txt

i am getting following output

    {
  "properties": {},
  "type": "generic",
  "id": "test",
  "artifacts": [
    {
      "type": "jar",
      "sha1": "bef8dd3",
      "sha256": "1c2ba8cb5369d",
      "md5": "f60031d2a0ef13dd8505b61af90c1c44",
      "name": "test-0-SNAPSHOT16.jar",
      "path": "libs-release-local/libs7/test-0-SNAPSHOT16.jar"
    }
  ]
}

Trying to assign the this to a variable so that i can merge both the variables.

base_modules=$(echo | jq '.modules[]' base_dependency.txt )

But when i echo base_modules in git bash

$ echo $base_modules
 }path": "libs-release-local/libs7/test-0-SNAPSHOT16.jar"

What am doing wrong.

CodePudding user response:

I can't reproduce the problem you've described. If I start with your base_dependencies.txt file, and run:

$ base_modules=$(echo | jq '.modules[]' base_dependencies.txt )

I get:

$ echo $base_modules
{ "properties": {}, "type": "generic", "id": "test", "artifacts": [ { "type": "jar", "sha1": "bef8dd3", "sha256": "1c2ba8cb5369d", "md5": "f60031d2a0ef13dd8505b61af90c1c44", "name": "test-0-SNAPSHOT16.jar", "path": "libs-release-local/libs7/test-0-SNAPSHOT16.jar" } ] }

Or with quotes:

$ echo "$base_modules"
{
  "properties": {},
  "type": "generic",
  "id": "test",
  "artifacts": [
    {
      "type": "jar",
      "sha1": "bef8dd3",
      "sha256": "1c2ba8cb5369d",
      "md5": "f60031d2a0ef13dd8505b61af90c1c44",
      "name": "test-0-SNAPSHOT16.jar",
      "path": "libs-release-local/libs7/test-0-SNAPSHOT16.jar"
    }
  ]
}

Note that the command you're using to set base_modules doesn't need that echo; it should be:

$ base_modules=$(jq '.modules[]' base_dependencies.txt )

CodePudding user response:

The output }path": makes me think, that this is -- once again -- a DOS/Unix EOL mismatch where a Windows jq executable is called from a Git4Windows/Cygwin/WSL bash.

Try if this works:

base_modules=$(jq '.modules[]' base_dependency.txt | dos2unix)

See https://stackoverflow.com/a/72296242/947357 for a similar problem.

  • Related