Cannot read json object value from via postman

210 views
Skip to first unread message

Duy Nguyen

unread,
Feb 21, 2016, 9:56:10 PM2/21/16
to nod...@googlegroups.com
Hi, 

I'm developing rest service using Express framework, I used Postman to test this web service, but I got this headache scenario need your help.
1. api method: /contents (POST)
2. request body (on Postman) (x-www-form-urlencoded):
  contents[0] : {file_name: 'abc.txt'}
  contents[1]: {file_name: 'xyz.txt'}
3. server:
 var contents = req.body.contents;
 console.log(contents[0]); //can see value here in string format with all information
 console.log(contents[0].file_name); // undefined??

I want to read attribute file_name, how I can do that?

Thanks,

--
Nguyen Hai Duy
Mobile : 0914 72 1900
Skype: nguyenhd2107

Aria Stewart

unread,
Feb 21, 2016, 10:34:45 PM2/21/16
to nod...@googlegroups.com

On Feb 21, 2016, at 21:47, Duy Nguyen <nguyen...@gmail.com> wrote:

Hi, 

I'm developing rest service using Express framework, I used Postman to test this web service, but I got this headache scenario need your help.
1. api method: /contents (POST)
2. request body (on Postman) (x-www-form-urlencoded):
  contents[0] : {file_name: 'abc.txt'}
  contents[1]: {file_name: 'xyz.txt'}
3. server:
 var contents = req.body.contents;
 console.log(contents[0]); //can see value here in string format with all information
 console.log(contents[0].file_name); // undefined??

contents[0] is a string -- if you want to parse it to see it as an object, JSON.parse it.


Duy Nguyen

unread,
Feb 21, 2016, 11:59:26 PM2/21/16
to nod...@googlegroups.com
Thanks for your hint Aria.

But even I use JSON.parse, it throws error when parsing, maybe my json string is not in right format?



--
Job board: http://jobs.nodejs.org/
New group rules: https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+un...@googlegroups.com.
To post to this group, send email to nod...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/DF006601-BDAE-4F34-AA53-B6C7FF15346A%40dinhe.net.
For more options, visit https://groups.google.com/d/optout.

Ryan Schmidt

unread,
Feb 23, 2016, 1:00:21 AM2/23/16
to nod...@googlegroups.com

On Feb 21, 2016, at 10:58 PM, Duy Nguyen <nguyen...@gmail.com> wrote:

> On Mon, Feb 22, 2016 at 10:34 AM, Aria Stewart <ared...@dinhe.net> wrote:
>
>> On Feb 21, 2016, at 21:47, Duy Nguyen <nguyen...@gmail.com> wrote:
>>
>>> I'm developing rest service using Express framework, I used Postman to test this web service, but I got this headache scenario need your help.
>>> 1. api method: /contents (POST)
>>> 2. request body (on Postman) (x-www-form-urlencoded):
>>> contents[0] : {file_name: 'abc.txt'}
>>> contents[1]: {file_name: 'xyz.txt'}
>>> 3. server:
>>> var contents = req.body.contents;
>>> console.log(contents[0]); //can see value here in string format with all information
>>> console.log(contents[0].file_name); // undefined??
>>
>> contents[0] is a string -- if you want to parse it to see it as an object, JSON.parse it.
>
> Thanks for your hint Aria.
>
> But even I use JSON.parse, it throws error when parsing, maybe my json string is not in right format?

Correct, your strings are valid JavaScript, but *NOT* valid JSON. For it to be valid JSON, the attribute values have to be "double-quoted", not 'single-quoted'. The attribute names also have to be "double-quoted" instead of not quoted at all.

Duy Nguyen

unread,
Feb 23, 2016, 9:12:26 AM2/23/16
to nod...@googlegroups.com
Thanks Ryan.

I resolved it, we need double quote for both key =&value, so final format is:
{"file_name": "test.txt"}

--
Job board: http://jobs.nodejs.org/
New group rules: https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+un...@googlegroups.com.
To post to this group, send email to nod...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages