Search code examples
jsonibm-watsonwatson-conversation

IBM Watson conversation: How to access JSON object from context variable?


I made a chatbot which takes 'place' as input and connects to an external API service passing the 'place' as a parameter. The API service returns a list of all available airport details for the given place as a JSON object.

For example, if 'Berlin' is given as input in conversation, The API returns following JSON object, which is then stored in the context variable $TheResult, The context variable $TheResult holds the following JSON object as the value when the input is 'Berlin',

{
  "message": {
    "Places": [
      {
        "CityId": "BERL-sky",
        "CountryId": "DE-sky",
        "CountryName": "Germany",
        "PlaceId": "BERL-sky",
        "PlaceName": "Berlin",
        "RegionId": ""
      },
      {
        "CityId": "BERL-sky",
        "CountryId": "DE-sky",
        "CountryName": "Germany",
        "PlaceId": "TXL-sky",
        "PlaceName": "Berlin Tegel",
        "RegionId": ""
      },
      {
        "CityId": "BERL-sky",
        "CountryId": "DE-sky",
        "CountryName": "Germany",
        "PlaceId": "SXF-sky",
        "PlaceName": "Berlin Schoenefeld",
        "RegionId": ""
      },
      {
        "CityId": "BTVA-sky",
        "CountryId": "US-sky",
        "CountryName": "United States",
        "PlaceId": "BTV-sky",
        "PlaceName": "Burlington",
        "RegionId": "VT"
      },
      {
        "CityId": "BLIA-sky",
        "CountryId": "US-sky",
        "CountryName": "United States",
        "PlaceId": "BLI-sky",
        "PlaceName": "Bellingham",
        "RegionId": "WA"
      },
      {
        "CityId": "BRLA-sky",
        "CountryId": "US-sky",
        "CountryName": "United States",
        "PlaceId": "BRL-sky",
        "PlaceName": "Burlington",
        "RegionId": "IA"
      }
    ]
  },
  "parameters": {
    "context": "",
    "message": "",
    "service": "https://skyscanner-skyscanner-flight-search-v1.p.rapidapi.com/apiservices/autosuggest/v1.0/FI/EUR/en-US/?query=Berlin"
  }
}

When I use the respond text as Response from external server: $TheResult.message.Places, it generates the following output,

Response from external server: [{"CityId":"BERL-sky","CountryId":"DE-sky","CountryName":"Germany","PlaceId":"BERL-sky","PlaceName":"Berlin","RegionId":""},{"CityId":"BERL-sky","CountryId":"DE-sky","CountryName":"Germany","PlaceId":"TXL-sky","PlaceName":"Berlin Tegel","RegionId":""},{"CityId":"BERL-sky","CountryId":"DE-sky","CountryName":"Germany","PlaceId":"SXF-sky","PlaceName":"Berlin Schoenefeld","RegionId":""},{"CityId":"BTVA-sky","CountryId":"US-sky","CountryName":"United States","PlaceId":"BTV-sky","PlaceName":"Burlington","RegionId":"VT"},{"CityId":"BLIA-sky","CountryId":"US-sky","CountryName":"United States","PlaceId":"BLI-sky","PlaceName":"Bellingham","RegionId":"WA"},{"CityId":"BRLA-sky","CountryId":"US-sky","CountryName":"United States","PlaceId":"BRL-sky","PlaceName":"Burlington","RegionId":"IA"}]

I have to access the first 'CityId' from the JSON object and display it as the output, in the case of Berlin as input, the expected output is,

The first airport city id is: BERL-sky

I tried the respond text, The first airport city id is: $TheResult.message.Places.CityId it throws following dialog node error

Error when updating output with output of dialog node id [node_1_1551877430730]. Node output is [{"generic":[{"values":[{"text":"Response from external server: $TheResult.message.Places.CityId"}],"response_type":"text","selection_policy":"sequential"}]}] SpEL evaluation error: Expression [ $TheResult.message.Places.CityId ] converted to [ context['TheResult'].message.Places.CityId ] at position 37: EL1008E: Property or field 'CityId' cannot be found on object of type 'JsonArray' - maybe not public?

and the respond text,The first airport city id is: $TheResult.message.Places[0].CityId also didn't work. It didn't generate any error, but displays the same output as above only appending extra [0].CityId at the end.

The first airport city id is: [{"CityId":"BERL-sky","CountryId":"DE-sky","CountryName":"Germany","PlaceId":"BERL-sky","PlaceName":"Berlin","RegionId":""},{"CityId":"BERL-sky","CountryId":"DE-sky","CountryName":"Germany","PlaceId":"TXL-sky","PlaceName":"Berlin Tegel","RegionId":""},{"CityId":"BERL-sky","CountryId":"DE-sky","CountryName":"Germany","PlaceId":"SXF-sky","PlaceName":"Berlin Schoenefeld","RegionId":""},{"CityId":"BTVA-sky","CountryId":"US-sky","CountryName":"United States","PlaceId":"BTV-sky","PlaceName":"Burlington","RegionId":"VT"},{"CityId":"BLIA-sky","CountryId":"US-sky","CountryName":"United States","PlaceId":"BLI-sky","PlaceName":"Bellingham","RegionId":"WA"},{"CityId":"BRLA-sky","CountryId":"US-sky","CountryName":"United States","PlaceId":"BRL-sky","PlaceName":"Burlington","RegionId":"IA"}][0].CityId

How should I parse this JSON object to access the individual key-value pairs?

Thanks in advance, I can elaborate the question further if necessary!!


Solution

  • Try to put your JSON path expression into the expression syntax. There you can use the full syntax. Something like this:

    The first airport code is <? $TheResult.message.Places[0].CityId ?>