Access JSON Response

52 views
Skip to first unread message

Ximui

unread,
Aug 11, 2020, 6:47:47 PM8/11/20
to Flutter Development (flutter-dev)
[{messages: [{id: Auth.form.error.email.taken, message: Email is already taken.}]}]

The above message is part of my JSON response, I would like to access the part that show 'Email is already taken' but I could not even though I have tried many different methods.
I am not familiar with JSON so I would like to ask everyone how can I finish this task.
Thanks.

Dorian Holmes

unread,
Aug 11, 2020, 6:49:25 PM8/11/20
to Ximui, Flutter Development (flutter-dev)
You need to declare it in a map and you will be able to fetch that message by using message as a key. 

--

You received this message because you are subscribed to the Google Groups "Flutter Development (flutter-dev)" group.

To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev...@googlegroups.com.

To view this discussion on the web visit https://groups.google.com/d/msgid/flutter-dev/d93240dc-88c0-4c61-8db7-180981758983o%40googlegroups.com.

Suzuki Tomohiro

unread,
Aug 11, 2020, 7:12:57 PM8/11/20
to Ximui, Flutter Development (flutter-dev)
Would you share the attempts you tried in code as well as the outcome (error or unexpected behavior)?

Ximui

unread,
Aug 11, 2020, 7:18:16 PM8/11/20
to Flutter Development (flutter-dev)
Here is the code

final responseData = json.decode(response.body);
final String errorMsg = responseData['message'];
print(errorMsg);

And this is the error that I got:

 Unhandled Exception: type 'List<dynamic>' is not a subtype of type 'String'

Ximui

unread,
Aug 11, 2020, 7:20:38 PM8/11/20
to Flutter Development (flutter-dev)
I tried but I am not sure if I do it correctly.

Map<String, dynamic> data = responseData['message'];

is that how I should set it up

On Tuesday, August 11, 2020 at 3:49:25 PM UTC-7, Dorian Holmes wrote:
You need to declare it in a map and you will be able to fetch that message by using message as a key. 
On Tue, Aug 11, 2020 at 5:47 PM Ximui <luumai...@gmail.com> wrote:
[{messages: [{id: Auth.form.error.email.taken, message: Email is already taken.}]}]



The above message is part of my JSON response, I would like to access the part that show 'Email is already taken' but I could not even though I have tried many different methods.

I am not familiar with JSON so I would like to ask everyone how can I finish this task.

Thanks.



--

You received this message because you are subscribed to the Google Groups "Flutter Development (flutter-dev)" group.

To unsubscribe from this group and stop receiving emails from it, send an email to flutt...@googlegroups.com.

Suzuki Tomohiro

unread,
Aug 11, 2020, 7:39:05 PM8/11/20
to Ximui, Flutter Development (flutter-dev)
Add a breakpoint to the second line. Use the debugger to understand the structure of responseData when your code hits the breakpoint. As per the error message, you’ll see its “message” field is not a String but a List.

--
You received this message because you are subscribed to the Google Groups "Flutter Development (flutter-dev)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/flutter-dev/53656a06-f71c-47f3-9274-e106e82d937do%40googlegroups.com.

Ximui

unread,
Aug 11, 2020, 7:50:56 PM8/11/20
to Flutter Development (flutter-dev)
Yeah, I understand it's not a string but a list, my attempt is to access "Email is already taken" message in that list but I couldn't


On Tuesday, August 11, 2020 at 4:39:05 PM UTC-7, Suzuki Tomohiro wrote:
Add a breakpoint to the second line. Use the debugger to understand the structure of responseData when your code hits the breakpoint. As per the error message, you’ll see its “message” field is not a String but a List.
On Tue, Aug 11, 2020 at 19:18 Ximui <luumai...@gmail.com> wrote:
Here is the code

final responseData = json.decode(response.body);
final String errorMsg = responseData['message'];
print(errorMsg);

And this is the error that I got:

 Unhandled Exception: type 'List<dynamic>' is not a subtype of type 'String'

On Tuesday, August 11, 2020 at 3:47:47 PM UTC-7, Ximui wrote:
[{messages: [{id: Auth.form.error.email.taken, message: Email is already taken.}]}]

The above message is part of my JSON response, I would like to access the part that show 'Email is already taken' but I could not even though I have tried many different methods.
I am not familiar with JSON so I would like to ask everyone how can I finish this task.
Thanks.

--
You received this message because you are subscribed to the Google Groups "Flutter Development (flutter-dev)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flutt...@googlegroups.com.

Suzuki Tomohiro

unread,
Aug 11, 2020, 7:58:35 PM8/11/20
to Ximui, Flutter Development (flutter-dev)
You can use “.first” to get the first element in a list. Use debugger to understand the type of the element.

To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/flutter-dev/ab601b17-360a-402b-b265-a3d293f19ccao%40googlegroups.com.

Souvik Dutta

unread,
Aug 11, 2020, 8:42:11 PM8/11/20
to Ximui, Flutter Development (flutter-dev)
Let's say your response is stored in a variable called res. Then to get the message part you will have to do 
res[0]["messages"][0]["message"].
Let's breakdown what is happening here. We know an array surrounded by [ ] is list and {} is a map. Now the first element in the list is the map that contains the required information. So we do res[0] which gives us the map which has the first element called messages. To extract anything from a set by using the key name we do map.[key] so here we will do res[0]["messages"] which gives us the value of messages. This value is again a list so we do res[0]["messages"][0]. Now we have a set again with the message key which is the required property. So we do res[0]["messages"][0]["message"]. I hope this helps and works. 

--
You received this message because you are subscribed to the Google Groups "Flutter Development (flutter-dev)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev...@googlegroups.com.

Ximui

unread,
Aug 12, 2020, 2:25:17 PM8/12/20
to Flutter Development (flutter-dev)
Thanks everyone for your help, my problem still occur. Below is my code and message:

Map<String, dynamic> data = responseData['message'].first;
print(data);
List<dynamic> data1 = data[0];
print(data1);

I/flutter (27822): {messages: [{id: Auth.form.error.email.taken, message: Email is already taken.}]}
I/flutter (27822): null

I do not understand why data1 = data[0] return null. should it return id: ..., message: ... ?

On Tuesday, August 11, 2020 at 3:47:47 PM UTC-7, Ximui wrote:

Suzuki Tomohiro

unread,
Aug 12, 2020, 2:29:44 PM8/12/20
to Ximui, Flutter Development (flutter-dev)
Use debugger. It tells why the map (“data” variable) does not have any value for the key “0”.

--
You received this message because you are subscribed to the Google Groups "Flutter Development (flutter-dev)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev...@googlegroups.com.

wa...@clinicianfirst.com

unread,
Aug 12, 2020, 7:16:48 PM8/12/20
to Flutter Development (flutter-dev)
Could you show the entire json response?  You aren't following the parsed structure down to the data.  Feel free to remove any sensitive information.  I think you have a basic misunderstanding of the result of parsing the returned data.. 

Souvik Dutta

unread,
Aug 12, 2020, 8:33:43 PM8/12/20
to Ximui, Flutter Development (flutter-dev)
Because data1 is trying to get an element from a set and not a list. Look for the curly braces. You are trying to get the value using the key 0. In this case you will have to do data1= data["messages"]

--
Message has been deleted
Message has been deleted
Message has been deleted

Ximui

unread,
Aug 14, 2020, 3:39:06 AM8/14/20
to Flutter Development (flutter-dev)
Thanks everyone for your helps.

Thanks to Souvik! I did not see the 's' in 'messages' until now.
My solution is 
List<dynamic> data = responseData['message'];
final String errorMsg = data[0]['messages'][0]['message'];

Suzuki Tomohiro

unread,
Aug 14, 2020, 8:57:56 AM8/14/20
to Ximui, Flutter Development (flutter-dev)
(3rd same advice)
Learn how to use debugger.

On Fri, Aug 14, 2020 at 03:06 Ximui <luumai...@gmail.com> wrote:
I/flutter (31409): {messages: [{id: Auth.form.error.email.taken, message: Email is already taken.}]}

Sorry for the late responses, the above is what I get from 

http.Response response = await http.post('http://10.0.2.2:1337/auth/local/register', body: {
"username": _username,
"email": _email,
"password": _password
});

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

Map<String, dynamic> data = responseData['message'].first;
print(data);

And I tried to get the message 'Email is already taken' part and display it in my page.
I understand that what I got back is a map with a list then a map inside of it.

So I tried 

print(data[0]['message'][0]['message']);
print(data['message'][0]['message']);

I both got:
[ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: NoSuchMethodError: The method '[]' was called on null.

I have been playing with it for a while still cannot figure it out

On Wednesday, August 12, 2020 at 5:33:43 PM UTC-7 souvik...@gmail.com wrote:
Reply all
Reply to author
Forward
0 new messages