Thursday, May 23, 2024
 Popular · Latest · Hot · Upcoming
139
rated 0 times [  140] [ 1]  / answers: 1 / hits: 32089  / 11 Years ago, tue, july 16, 2013, 12:00:00

I'm using Node.js and express (3.x). I have to provide an API for a mac client and from a post request I extract the correct fields. (The use of request.param is mandatory) But the fields should be composed back together to JSON, instead of strings.



I got:



var obj = {
title: request.param('title'),
thumb: request.param('thumb'),
items: request.param('items')
};


and request.param('items') contains an array of object but still as a string:



'[{name:this},{name:that}]'


I want to append it so it becomes:



var obj = {
title: request.param('title'),
thumb: request.param('thumb'),
items: [{name:this},{name:that}]
};


Instead of



var obj = {
title: request.param('title'),
thumb: request.param('thumb'),
items: [{name:this},{name:that}]
};


Anyone who can help me with this? JSON.parse doesn't parse an array of object, only valid JSON.


More From » json

 Answers
5

How about this:



var obj = JSON.parse({items: + request.param('items') + });
obj.title = request.param('title');
obj.thumb = request.param('thumb');

JSON.stringify(obj);

[#76968] Monday, July 15, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
victorw

Total Points: 484
Total Questions: 120
Total Answers: 107

Location: Faroe Islands
Member since Thu, Apr 8, 2021
3 Years ago
victorw questions
Fri, Mar 18, 22, 00:00, 2 Years ago
Sun, Feb 20, 22, 00:00, 2 Years ago
Fri, May 7, 21, 00:00, 3 Years ago
Sat, Mar 13, 21, 00:00, 3 Years ago
;