0

In the given script, the nested key is not getting appended with the value. I could not figure out where the script is going wrong.

#!/bin/bash

echo "Add the figma json file path"
read path

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

echo $(cat $figma_json | jq -r '.color | to_entries[] | "\(.key):\(.value| if .value == null then .[] | .value  else .value end)"')

Sample input:

{
  "color": {
    "white": {
      "description": "this is just plain white color",
      "type": "color",
      "value": "#ffffffff",
      "extensions": {
        "org.lukasoppermann.figmaDesignTokens": {
          "styleId": "S:40940df38088633aa746892469dd674de8b147eb,",
          "exportKey": "color"
        }
      }
    },
    "gray": {
      "50": {
        "description": "",
        "type": "color",
        "value": "#fafafaff",
        "extensions": {
          "org.lukasoppermann.figmaDesignTokens": {
            "styleId": "S:748a0078c39ca645fbcb4b2a5585e5b0d84e5fd7,",
            "exportKey": "color"
          }
        }
      }
    }
  }
}

Actual output:

white:#ffffffff gray:#fafafaff 

Excepted output:

white:#ffffffff gray:50:#fafafaff

Full input file

7
  • 2
    Don't use echo -e unless you really know you need to. (And really, don't use it even then; if you really need its effects, printf '%b\n' is the better alternative, even suggested in the echo standards documentation). In particular, echo -e with a JSON string can make it into something that's no longer valid JSON, by replacing literal \n sequences with newlines. Commented Apr 6, 2022 at 19:36
  • BTW, why is your actual output code-formatted but your expected output not? It makes it hard to compare them. Commented Apr 6, 2022 at 19:38
  • ...beyond that, what part of your jq code is supposed to add the :50 to the output? I don't see any code that would be reasonably expected to do that? Commented Apr 6, 2022 at 19:39
  • @CharlesDuffy I have highlighted the output code to make reader's easier. Commented Apr 6, 2022 at 19:43
  • It's the opposite of easier, unless you highlight them both the same way. Commented Apr 6, 2022 at 19:44

2 Answers 2

2

Here's a solution using tostream instead of to_entries to facilitate simultaneous access to the full path and its value:

jq -r '
  .color | tostream | select(.[0][-1] == "value" and has(1)) | .[0][:-1]+.[1:] | join(":")
' "$figma_json"
white:#ffffffff
gray:50:#fafafaff

Demo

Sign up to request clarification or add additional context in comments.

Comments

1

An approach attempting to demonstrate bash best-practices:

#!/bin/bash

figma_json=$1                              # prefer command-line arguments to prompts
[[ $figma_json ]] || {
  read -r -p 'Figma JSON file path: ' path # read -r means backslashes not munged
  figma_json=${path//[[:space:]]/}         # parameter expansion is more efficient than tr
}

jq -r '
  def recurse_for_value($prefix):
    to_entries[]
    | .key as $current_key
    | .value?.value? as $immediate_value
    | if $immediate_value == null then
        .value | recurse_for_value(
                   if $prefix != "" then
                     $prefix + ":" + $current_key
                   else
                     $current_key
                   end
                 )
      else
        if $prefix == "" then
          "\($current_key):\($immediate_value)"
        else
          "\($prefix):\($current_key):\($immediate_value)"
        end
      end
    ;
  .color |
  recurse_for_value("")
' "$figma_json"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.