1
0
-1

Example i have set of values in JSON file like  zip code, City and State code. If user enter the value of zip code, i need to search the value in the json file and get the appropriate city and state value.

So need to know how to import the external json file to avoka and need to read the data in the json file.

    CommentAdd your comment...

    2 answers

    1.  
      2
      1
      0

      If you have a large set of JSON data you need to reference in your applications please use the PropertyQuery class to lookup these values.

      import com.avoka.tm.query.*
       
      String refData = new PropertyQuery()
          .setName("Product Catalog")
          .setClientCode("maguire")
          .setCacheTimeout(60)
          .getValue()
      
      

      To load reference data into Transact from an external system please use the service template below:

      Next specify the Organization property name you want for your reference data, and the REST service file parameter name for the upload operation.

      You can then upload reference data using this service via the REST Groovy Service Invoke API.  Please see the documentation below:

      Groovy Service Invoke

      regards Malcolm Edgar

        CommentAdd your comment...
      1.  
        1
        0
        -1

        In your Fluent Groovy Service you can add a service parameter to store the JSON data. If you are using an IDE just drop the JSON file into your service directory and add the service parameter to the service-def.json file with the filePath attribute pointing to your JSON file e.g.:

        {
            "name": "jsonData",
            "type": "JSON",
            "filePath": "data.json",
            "bind": false,
            "required": false,
            "clearOnExport": false,
            "readOnly": false
        }

        See the following article for more detail on the service-def.json file:

        Then in your Fluent Groovy script you can access this service parameter and interrogate it using the Path class.

        Assuming your JSON data looks something like:

        {
          "locations": [
            {
              "zipCode": "11111",
              "city": "Alpha",
              "state": "Peach"
            },
            {
              "zipCode": "22222",
              "city": "Beta",
              "state": "Pear"
            }
          ]
        }

        you could interrogate the data as follows:

        String invoke(SvcDef svcDef, Txn txn, HttpServletRequest request, User user) {
        
          String zipCode = request.getParameter('zipCode')
          logger.debug 'Zip Code: ' + zipCode
        
          String jsonData = svcDef.paramsMap.jsonData
          Path jsonPath = new Path(jsonData)
        
          Path matchingLocation = jsonPath.paths('locations.*').findAll{ it.val('zipCode') == zipCode }.first()
        
          Map responseData = [
            'zipCode': zipCode,
            'city': matchingLocation.val('city'),
            'state': matchingLocation.val('state')
          ]
          logger.debug 'Response: ' + responseData
        
          return new JsonBuilder(responseData).toString()
        }

        For more information on interrogating structured data paths see the following article:

         

        You can verify this code by including a unit test script as follows:

        void invoke(SvcDef svcDef, Map testParams) throws Exception {
        
          Txn txn = new MockVoBuilder().createTxnOpened()
          MockRequest request = new MockRequest()
          request.setParameter('zipCode','22222')
        
          Map params = [
            "svcDef": svcDef,
            "txn": txn,
            "request": request,
            "user": null
          ]
        
          String result = (String) new ServiceInvoker(svcDef).invoke(params)
          logger.info result
        
          Path responsePath = new Path(result)
          assert responsePath.val('city') == 'Beta'
        }

        Running this test script in Transaction Manager you should see output similar to the following:

        09:12:59,150 DEBUG Zip Code: 22222
        09:12:59,151 DEBUG Response: [zipCode:22222, city:Beta, state:Pear]
        09:12:59,151 INFO {"zipCode":"22222","city":"Beta","state":"Pear"}
          CommentAdd your comment...