1

I want to extract the child value of queryResult ie. "Hello there. I am chat bot So shall we get started?" from the JSON using flutter

final responseJson = json.decode(response.body);

print("DATA \n ${responseJson['queryResult']}");

JSON DATA ===>

{  
   "responseId":"123456789",
   "queryResult":{  
      "queryText":"Hello",
      "action":"input.welcome",
      "parameters":{  

      },
      "allRequiredParamsPresent":true,
      "fulfillmentText":"Greetings! How can I assist?",
      "fulfillmentMessages":[  
         {  
            "text":{  
               "text":[  
                  "Hello there. I am chat bot So shall we get started?"
               ]
            }
         },
         {  
            "quickReplies":{  
               "quickReplies":[  
                  "Yes",
                  "No"
               ]
            }
         }
      ],
      "outputContexts":[  
         {  
            "name":"xyz",
            "lifespanCount":5
         }
      ],
      "intent":{  
         "name":"xyz",
         "displayName":"Default Welcome Intent"
      },
      "intentDetectionConfidence":1,
      "diagnosticInfo":{  
         "webhook_latency_ms":5
      },
      "languageCode":"en"
   },
   "webhookStatus":{  
      "message":"Webhook execution successful"
   }
}

I am unable to read the child data of queryResult using

final responseJson = json.decode(response.body);
print("DATA \n ${responseJson['queryResult']}");
1

1 Answer 1

3

If you want to get that specific line, than you can get it this way, but for more usage it would be wise to serialize the json like here: https://flutter.dev/docs/development/data-and-backend/json

print("DATA \n ${responseJson['queryResult']['fulfillmentMessages'][0]['text']['text'][0]}");

For an easier understanding on the path:

print("DATA \n ${
      responseJson['queryResult']
        ['fulfillmentMessages'][0]
          ['text']
            ['text'][0]
      }"
 );

Whole code which is like yours:

import 'dart:convert';

final responseBody = '{"responseId":"123456789","queryResult":{"queryText":"Hello","action":"input.welcome","parameters":{},"allRequiredParamsPresent":true,"fulfillmentText":"Greetings! How can I assist?","fulfillmentMessages":[{"text":{"text":["Hello there. I am chat bot So shall we get started?"]}},{"quickReplies":{"quickReplies":["Yes","No"]}}],"outputContexts":[{"name":"xyz","lifespanCount":5}],"intent":{"name":"xyz","displayName":"Default Welcome Intent"},"intentDetectionConfidence":1,"diagnosticInfo":{"webhook_latency_ms":5},"languageCode":"en"},"webhookStatus":{"message":"Webhook execution successful"}}';

void main() {
  final Map<String, dynamic> responseJson = json.decode(responseBody);

  print("DATA \n ${
      responseJson['queryResult']
        ['fulfillmentMessages'][0]
          ['text']
            ['text'][0]
      }"
 );
}
Sign up to request clarification or add additional context in comments.

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.