Search code examples
arraysjsonbashjq

How to convert a json string into an individual character array in bash?


I have a json string that I need to convert into an array for passing a password in. Current issue is I need to do this for many users

Current json looks like this which was generated from

[
   {
        "password": "Password1",
        "loginId": "USER1"
   }
]

I need to change it to this

[ 
   { 
      "password":[ 
         "P",
         "a",
         "s",
         "s",
         "w",
         "o",
         "r",
         "d",
         "1"
      ],
      "loginId":"USER1"
   }
]

I'd prefer to do this in bash but if anyone has other options I'm open to anything.


Solution

  • In jq, splitting a string by empty string results in a character array just as you need.

    jq '.[].password |= split("")' file
    

    Online demo at jqplay.org