1

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.

3 Answers 3

3

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

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

1 Comment

Wow that was super simple. Thank you so much
1

With you can use either of the three queries to update the JSON:

$ xidel -s input.json \
  -e '$json(1).password   :=array{analyze-string($json()/password,".")//match}'   # Dot notation
  -e '$json(1)("password"):=array{extract($json()/password,".",0,"*")}'           # JSONiq notation
  -e '$json?1?password    :=array{x:cps(x:cps($json()/password))}'                # XPath 3.1 syntax
[
  {
    "password": [
      "P",
      "a",
      "s",
      "s",
      "w",
      "o",
      "r",
      "d",
      "1"
    ],
    "loginId": "USER1"
  }
]
  • The XPath-like notation ($json()/password) can't be used on the left side of the :=. You have to use the dot notation, the JSONiq notation, or the XPath 3.1 syntax.
  • The analyze-string() function and Xidel's own extract() and x:cps() can all be used to turn a string into a sequence of letters.

1 Comment

Seems like an excellent tool, wonder why I have never heard about it
0

Handling JSON through Bash is a bad idea. Awk is not better at all but it can be done :

awk -F':' '
function ltrim(s) { sub(/^[ \t\r\n]+/, "", s); return s }
function rtrim(s) { sub(/[ \t\r\n]+$/, "", s); return s }
function trim(s) { return rtrim(ltrim(s)); }
!/"password"/ {
    print $0
}
/"password"/ {
    found=1;
    print $1 ":["
    password = $2
    gsub(/"|,/, " ", password)
    split(trim(password),letters,"")
    for(c=1; c <= length(letters); c++ ) {
        printf("\t\t\"%s\"",letters[c])
        if (c != length(letters)) {
            printf(",\n")
        }else{
            printf("\n\t\t]\n")
        }
    }
}
' input.txt

On the other hand. I think the right tool for this is Python 3 actually :

python3 -c '
import json

result = []

list = json.loads("""
[
   {
        "password": "Password1",
        "loginId": "USER1"
   }
]
""")
for item in list:
  result.append({ "loginId" : item["loginId"], "password" : [char for char in item["password"]] })

print(json.dumps(result, indent=4))
'

Hope this helps!

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.