-1

I have json file which extract the color value from the file. For some reason, it only fetch only one block of code & for the rest it throws error.

snippet

#!/bin/bash

clear

echo "Add the figma json file path"
read path

figma_json="$(echo -e "${path}" | tr -d '[:space:]')"

echo "*****************************************"


color_values=$(cat $figma_json | jq -r '.color')
color_keys=$(cat $figma_json | jq -r '.color | keys' |sed 's,^ *,,; s, *$,,'| tr -s ' ' | tr ' ' '_')

echo $color_keys


for c_key in $color_keys
do

    echo "key string: $c_key"

    echo $color_values | jq ".$c_key.value"
    echo "*********************************************"

done

Output

trimmed string: "gray1",
{
  "description": "",
  "type": "color",
  "value": "#333333ff",
  "extensions": {
    "org.lukasoppermann.figmaDesignTokens": {
      "styleId": "S:0b49d19e868ec919fac01ec377bb989174094d7e,",
      "exportKey": "color"
    }
  }
}
null
*********************************************
trimmed string: "gray2" //Expected output
"#333333ff"
*********************************************

If we look at the second output it prints the hex value of gray2 which is the expected output Please use the follow link to get the json file link

cigien
  • 57,834
  • 11
  • 73
  • 112
Anand
  • 2,086
  • 2
  • 26
  • 44
  • 1
    Please [edit] to provide a [mre] with enough JSON data in the question itself to reproduce the problem. But the error message is surely telling you that `$c_key.value` contains something which isn't syntactically valid for `jq`. Probably also edit your output to show the value printed just before the error. – tripleee Apr 05 '22 at 11:12
  • As an aside, http://shellcheck.net/ will point out some common beginner errors, though it can't cringe over how you post-process the output from `jq` when you assign `color_keys`. It is also oddly lenient about the [useless uses of `cat`](https://stackoverflow.com/questions/11710552/useless-use-of-cat). – tripleee Apr 05 '22 at 11:13
  • `$color_keys` is a string containing a json array. `for c_key in $color_keys` can't be used to loop over such a string. – 0stone0 Apr 05 '22 at 11:15
  • That said, please add your desired output. This can probably be done in a single JQ call. – 0stone0 Apr 05 '22 at 11:16
  • @0stone0 I have added the expected output. Can you please help me here – Anand Apr 05 '22 at 11:28
  • Your expected output is unclear, why start with an object and then the hex? Is this the final outcome? – 0stone0 Apr 05 '22 at 11:38

1 Answers1

2

It's quite unclear what you are aiming at, but here's one way how you would read from a JSON file using just one call to jq, and most probably without the need to employ sed or tr. The selection as well as the formatting can easily be adjusted to your liking.

jq -r '.color | to_entries[] | "\(.key): \(.value.value)"' "$figma_json"
gray1: #333333ff
gray2: #4f4f4fff
gray3: #828282ff
gray4: #bdbdbdff
gray5: #e0e0e0ff
gray6: #f2f2f2ff
red: #eb5757ff
orange: #f2994aff
yellow: #f2c94cff
green1: #219653ff
green2: #27ae60ff
green3: #6fcf97ff
blue1: #2f80edff
blue2: #2d9cdbff
blue3: #56ccf2ff
purple1: #9b51e0ff
purple2: #bb6bd9ff

Demo

pmf
  • 24,478
  • 2
  • 22
  • 31