Search code examples
jolie

Substitute a word to access json data in Jolie


I have a Glossary.json file created as

{
    "glossary": {
        "program": "A set of instructions",
        "OS": "Operating System"
    }
}

Then I have a jolie file written as

include "file.iol"

type GlossaryRequest { queryWord : string }
type Glossary { glossary : string }

interface GlossaryInterface {
    RequestResponse : fetchDefinition (GlossaryRequest) (Glossary)
}

service GlossaryService {
    inputPort GlossaryInput {
        location: "socket://localhost:8081"
        protocol: http { format = "json" }
        interfaces: GlossaryInterface
    }
    
    main {
        readFile@File( {
            filename = "glossary.json"
            format = "json"
        } )( data )
        fetchDefinition (request) (response) {
            key = "data.glossary." + request.queryWord
            response.glossary = key //This is the line concerned
        }
    }
}

Since the queryWord will be dynamic, based on the user's input, I need to form the key and then evaluate it and return the response.

I want this line to be

    response.glossary = data.glossary.OS or data.glossary.program, based on the input to URL
The URL would be "http://localhost:8081/fetchDefinition?queryWord=program

Any hint or direction to achieve this? Otherwise, how can I search for the input string in data?


Solution

  • If I had searched for a few more examples on the Jolie documents page, I need not have posted this question. Anyways, I found the answer to my question under the "Dynamic Binding".

    Answer is to use it this way:

    response.glossary = data.glossary.(request.queryWord)