I need some basic understanding

I am having a great deal of difficulty understanding how to assemble some things. I know it is likely a basic lack of understanding. The following hardcoded WebSocket message works.

interfaces.webSocket.sendMessage('{"type":"auth","access_token":"JKV1Qi.LCJhbGciOiJIUzI1NiJ9"}')

I want to put the access token into a variable like the following;

def auth = ('"type"'+':'+'"auth"'+','+'"access_token"'+':'+'"'+"$access_token"+'"')

and then use it like:

interfaces.webSocket.sendMessage('{"$auth"}')

I just can't seem to get it to work

The following expands the $access_token in the logs.

log.debug('"type"'+':'+'"auth"'+','+'"access_token"'+':'+'"'+"$access_token"+'"')

however, using the following fails

interfaces.webSocket.sendMessage('"type"'+':'+'"auth"'+','+'"access_token"'+':'+'"'+"$access_token"+'"')

Could some kind soul point me in a better direction?

Oops sorry, missed a key part of your goal. Try this instead:

def token = 'JKV1Qi.LCJhbGciOiJIUzI1NiJ9'
def auth = '{"type":"auth","access_token":"' + $token + '"}'
interfaces.webSocket.sendMessage($auth)

Why do people always want to hand roll json? :slight_smile: Groovy has nice libraries for this.

    def message_data = [
         'type': "auth",
         'access_token': "JKV1Qi.LCJhbGciOiJIUzI1NiJ9",
    ]

    def json = new groovy.json.JsonOutput().toJson(message_data)
    interfaces.webSocket.sendMessage(json)

Does this make sense or do I need to explain it more?

3 Likes

@asj This is my first foray into groovy. Did I mention I wasn't a programmer? I can usually stumble through things until I finally see some light. I did manage to make it work my way. I just had a misplaced bracket. I will give it a try your way.
Since I am learning, would you mind explaining a bit more?

Many thanks for responding. I did come up with what worked.

interfaces.webSocket.sendMessage('{"type"'+':'+'"auth"'+','+'"access_token"'+':'+'"'+"$access_token"+'"}')

@asj many thanks. Works a treat. Now to redo my other bits.

Sure, so groovy has a data type called maps, they're key, value pairs. Other languages call them dictionaries, hashes, etc. So you can do things like thing["key"] = 5 then you've a stored the value 5 into index key . You can then do thing.key for nicer shorthand to access it as well.

Many of the simple data types of groovy map well to json. That's the {"key": 5} that you wrote by hand. The { says start a map and have the index key with the value 5. Writing json by hand is a pain since you're mixing " in with groovy single quote or double quote for strings, and you get no validation since the language just sees it as text strings.

By writing groovy types, no only is it more readable, but you get the compiler doing syntax checking for you and guaranteeing you get valid json. (within reason)

Walking through the example:

 def message_data = [
    'type': "auth",
    'access_token': "JKV1Qi.LCJhbGciOiJIUzI1NiJ9",
]

This says make a new variable message_data of type map and initialize it with 2 elements, type and access_token which are both strings. I was lazy and cutting and pasting, but could/should have changed to the "auth" to 'auth' to not only be consistent, but because were aren't doing any variable expansion.

def json = new groovy.json.JsonOutput().toJson(message_data)

This (via a round about route) calls the library function toJson to convert a groovy map/type/etc into json. The return value is a string of json. I just cut & paste the code above from somewhere, but as I read the docs (link lower down) I realized it's a bit dumb and you could write:

def json = groovy.json.JsonOutput.toJson(obj)

Groovy isn't a language I'm all the familiar with tbh, and like all things there's a but of junk out there.

This this line sends the string returned from above call.

interfaces.webSocket.sendMessage(json)

Docs for the json parser and producer are at: The Apache Groovy programming language - Parsing and producing JSON

3 Likes

After you posted the bit earlier and I started working on my code, it all started making more sense. I actually got variable expansion figured out where access_token is taken from an input field. I was able rip out most of the ugly messy crappy bits of my code so far.
Still much to learn but thanks to your snippet and the explanations the initial wall has been scaled. I am not a programmer but I can usually figure out how to make things work. I often find, when I tackle an unfamiliar programming language that there is some initial hurdle that I spend an inordinate amount of time trying to get over. Once I "get it", the rest is a lot smoother and I understand what I am looking for in the manual.
Thanks again. I so appreciate your kindness and time.