0

I need to transform an array by adding additional objects -

I have:

"user_id":"testuser"
"auth_token":"abcd"

I need:

"key":"user_id"
"value":"testuser"
"key":"auth_token"
"value":"abcd"

I have been using jq but cant figure out how to do it. Do i need to transform this into a multi-dimensional array first?

I have tried multiple jq queries but cant find the most suitable

When i try using jq i get

jq: error: syntax error, unexpected $end, expecting QQSTRING_TEXT or QQSTRING_INTERP_START or QQSTRING_END (Unix shell quoting issues?) at , line 1

4
  • 1
    Where is your JSON ? Commented Jun 21, 2019 at 12:27
  • The error is a parsing error (jq complains it reaches the end of the input while it was reading a string, which wasn't closed with the appropriate quote). I'm not sure whether it's your command or its input that it fails to parse, hard to tell when you didn't provide either. Probably the input though, I think command parsing errors don't follow that format. Commented Jun 21, 2019 at 13:08
  • jasonYardley - If your input is valid JSON, please show it; if the input is not intended to be valid JSON, please don't characterize it as such. Similarly please clarify your requirements regarding the output. Commented Jun 21, 2019 at 14:22
  • sorry this was my first time posting so my request came through unintentionally. basically i run a POST against a url that returns a credential. the credential is output as exaclty (minus changing the vlues): {"auth_token":"aaaa","id":"bbb"}. the problem im having is not inputting back into a different system as json, but rather transforming the json to include "key" and "value" as per my description. i have tried almost every jq argument i could find and spent many hours researching. i posted this to see if someone has done this before; transform the json to inclose extra objects inbetween Commented Jun 21, 2019 at 15:36

3 Answers 3

1

Your input is not json, it's just a bunch of what could be thought of as key/value pairs. Assuming your json input actually looked like this:

{
    "user_id": "testuser",
    "auth_token": "abcd"
}

You could get an array of key/value pair objects using to_entries.

$ jq 'to_entries' input.json
[
    {
        "key": "user_id",
        "value": "testuser"
    },
    {
        "key": "auth_token",
        "value": "abcd"
    }
]

If on the other hand your input was actually that, you would need to convert it to a format that can be processed. Fortunately you could read it in as a raw string and probably parse using regular expressions or basic string manipulation.

$ jq -Rn '[inputs|capture("\"(?<key>[^\"]+)\":\"(?<value>[^\"]*)\"")]' input.txt
$ jq -Rn '[inputs|split(":")|map(fromjson)|{key:.[0],value:.[1]}]' input.txt
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, that worked perfectly. my original output was in JSON i just pasted incorrectly. I tried so hard to find an expression that could do that and stumbled across a few. im still new to using jq but going to spend more time learning it now. thanks so much for your help!
1

You can use to_entries filter for that.

Here is jqplay example

Comments

1

Robust conversion of key:value lines to JSON.

If the key:value specifications would be valid JSON except for the missing punctuation (opening and closing braces etc), then a simple and quite robust approach to converting these key:value pairs to a single valid JSON object is illustrated by the following:

cat <<EOF | jq -nc -R '["{" + inputs + "}" | fromjson] | add' 
"user_id": "testuser"
"auth_token" : "abcd"
EOF

Output

{
  "user_id": "testuser",
  "auth_token": "abcd"
}

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.