Quantcast
Channel: Active questions tagged jq - Stack Overflow
Viewing all articles
Browse latest Browse all 578

jq not printing full string with spaces in Bash script [duplicate]

$
0
0

Here is my structure:

enter image description here

I am trying to print some values in a JSON file. Here is the service-data.json:

{"service_name": "nphies-support.service","description": "nphies-support-service Spring Boot Application","user": "aceusr","working_directory": "/home/aceusr/deployment/nphies/nphies-support","java_home": "/usr/lib/jvm/jdk-21.0.1","jar_file": "nphies-support-service.jar"}

I am reading this JSON in a bash script using jq:

#!/bin/bashecho "$0 script starting"# test if the jq is installedtest_json=$(echo "{ }" | jq)if [ "$test_json" != "{}" ]; then    echo "jq not installed"    exit 1fi#read json into variable jsonjson=$(cat service-data.json)#converting json objects to bash variabledeclare $(echo $json | jq -r 'to_entries | .[] | "export \(.key)=\(.value)"')echo "service_name = $service_name"echo "description = $description"echo "user = $user"echo "working_directory = $working_directory"echo "java_home = $java_home"echo "jar_file = $jar_file"#use the -r option to output the result as raw text, without quotes. description1=$(echo $json | jq -r '.description')echo "description1 = $description1"exit 0 

This JSON is an object so I am using declare function to convert JSON into bash script variables.

For description it is only printing nphies-support-service instead of nphies-support-service Spring Boot Application. Looks like it is breaking on first space.

But when I read the same field using the syntax name=$(echo $json | jq -r '.description') it prints the whole text.

enter image description here

Why is it not printing the whole text in iteration? What do I do so it prints whole text for description variable?


Viewing all articles
Browse latest Browse all 578

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>