Mohammed Ali
Mohammed Ali

Reputation: 155

Getting rid of Double quotes within a value using JQ

I am trying to create a --data-raw with the help of jq , it works fine for the most part, except where i have a list .

#!/bin/bash
TEST="'bfc', 'punch', 'mld', 'extended_mld', 'chargingstation', 'ch'"
BFC_TAG=$BUILD_ID
UPLOAD_TO=s3
COMPILE=$test
REGION=world
PROJECT=bfc
JSON_STRING=$( jq -n \
--arg a "$BFC_TAG" \
--arg b "$UPLOAD_TO" \
--arg c "$TEST" \
--arg d "$REGION" \
--arg f "$PROJECT" '
{brfc_tag: $a,
upload_to: $b,
compile: [ $c ],
region: $d,
project: $f
}')
echo ${JSON_STRING}

Output:

{ "bfc_tag": "", "upload_to": "s3", "compile": [ "'bfc', 'punch', 'mld', 'extended_mld', 'chargingstation', 'ch'" ], "region": "world", "project": "bfc" }

If u look at the output particularly in compile key it has " in beginning and in the end, which break breaks python code, which consumes this, i need to get rid of the double quotes within compile, any help aprreaciated.

Upvotes: 1

Views: 1197

Answers (1)

0stone0
0stone0

Reputation: 43944

Those single quotes are already present in your input string:

TEST="'bfc', 'punch', 'mld', 'extended_mld', 'chargingstation', 'ch'"

I would recommend removing those, and creating a simple csv witch JQ can split for us

TEST="bfc,punch,mld,extended_mld,chargingstation,ch"

To create an array from that string, use;

compile: $c | split(",")

So the complete code:

#!/bin/bash
TEST="bfc,punch,mld,extended_mld,chargingstation,ch"
BFC_TAG=$BUILD_ID
UPLOAD_TO=s3
COMPILE=$test
REGION=world
PROJECT=bfc
JSON_STRING=$( jq -n \
--arg a "$BFC_TAG" \
--arg b "$UPLOAD_TO" \
--arg c "$TEST" \
--arg d "$REGION" \
--arg f "$PROJECT" '
{brfc_tag: $a,
upload_to: $b,
compile: $c | split(","),
region: $d,
project: $f
}')
echo ${JSON_STRING}

Will produce:

{ "brfc_tag": "", "upload_to": "s3", "compile": [ "bfc", "punch", "mld", "extended_mld", "chargingstation", "ch" ], "region": "world", "project": "bfc" }

Upvotes: 1

Related Questions