Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making multiple changes to one JSON template

I'm currently using jq with the 1pass CLI to try and create randomly generated passwords into a secure note. I'm having an issue with setting the fields.

These are two of my variables. I have 8 total I need to set.

section0_section_uuid="Section_0"

section1_section_uuid="Section_1"

And here are my commands to manipulate the template. I first read it in, change the first title, then save it to $template. I then pass $template into jq

template=$(cat template.json | jq --arg uuid "$section0_section_uuid" '.sections[0].title=$uuid')

template=$($template | jq --arg uuid "$section1_section_uuid" '.sections[1].title=$uuid')

echo $template

I get "file name too long." I don't think I'm passing the modified template variable in correctly. I need to do 7 more modifications to the template.json file.

Edit:

Here's the full template I'm trying to manipulate. It's 12 total changes to the template I have to make. 10 of the 12 are random numbers that I will generate. The remaining 2 of the 12 will be a generated usernames.

{
  "fields": [],
  "sections": [
    {
      "fields": [
        {
          "k": "concealed",
          "n": "[CHANGE_ME]",
          "t": "ROOT_USER_PASS",
          "v": "[CHANGE_ME]"
        },
        {
          "k": "concealed",
          "n": "[CHANGE_ME]",
          "t": "DEV_USER_PASS",
          "v": "[CHANGE_ME]"
        }
      ],
      "name": "Section_[CHANGE_ME]",
      "title": "Container SSH"
    },
    {
      "fields": [
        {
          "k": "string",
          "n": "[CHANGE_ME]",
          "t": "placeholdertext",
          "v": "[CHANGE_ME_LETTERS]"
        },
        {
          "k": "string",
          "n": "[CHANGE_ME]",
          "t": "placeholdertext",
          "v": "[CHANGE_ME_LETTERS]"
        },
        {
          "k": "concealed",
          "n": "[CHANGE_ME]",
          "t": "placeholdertext",
          "v": "[CHANGE_ME]"
        }
      ],
      "name": "Section_[CHANGE_ME]",
      "title": "MySQL"
    }
  ]
}
like image 338
Jordan Little Avatar asked Jul 12 '26 12:07

Jordan Little


1 Answers

Why not make your template an actual jq filter, rather than a JSON blob to modify?

The contents of template.jq would be

{
  sections: [
    { title: $t1 },
    { title: $t2 },
    { title: $t3 },
    { title: $t4 },
    { title: $t5 },
    { title: $t6 },
    { title: $t7 },
    { title: $t8 }
  ]
}

Then your command would simply be

$ jq -n --arg t1 foo --arg t2 bar ... -f template.jq
{
  "sections": [
    {
      "title": "foo"
    },
    {
      "title": "bar"
    },
    ...
  ]
}

One benefit of doing it this way is that you can't accidentally forget a value; jq can only process the filter if you provide definitions for all 8 variables.

like image 125
chepner Avatar answered Jul 15 '26 09:07

chepner



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!