Here is the original JSON object I am retrieving from my CouchDB database:
{
"total_rows": 3,
"offset": 0,
"rows": [
{
"id": "1e8fe199d4e761b71bb8920eb3003db2",
"key": null,
"value": {
"_id": "1e8fe199d4e761b71bb8920eb3003db2",
"_rev": "3-6979e0cd646f2bc0ddf84c0664d25808",
"Room_number": 99,
"Location": "City campus",
"Timetable": {
"Monday": [
"08:30",
"10:00",
"11:30",
"13:30",
"14:14"
],
"Tuesday": [
"08:30",
"10:00",
"11:30",
"13:30",
"14:14"
],
"Sunday": [
"08:30",
"10:00",
"11:30",
"13:30",
"14:14"
]
}
}
},
{
"id": "1e8fe199d4e761b71bb8920eb3004cc6",
"key": null,
"value": {
"_id": "1e8fe199d4e761b71bb8920eb3004cc6",
"_rev": "2-f7250cca62a804137174a20f48312c40",
"Room_number": 12,
"Location": "City hall",
"Timetable": {
"Monday": [
"08:30",
"10:00",
"11:30",
"13:30",
"14:14"
],
"Tuesday": [
"08:30",
"10:00",
"11:30",
"13:30",
"14:14"
],
"Sunday": [
"08:30",
"10:00",
"11:30",
"13:30",
"14:14"
]
}
}
]
}
I want to make it look like the JSON object below:
[
{
"Room_number": 99,
"Location": "City campus",
"Timetable": {
"Monday": [
"08:30",
"10:00",
"11:30",
"13:30",
"14:14"
],
"Tuesday": [
"08:30",
"10:00",
"11:30",
"13:30",
"14:14"
],
"Sunday": [
"08:30",
"10:00",
"11:30",
"13:30",
"14:14"
]
}
},
{
"Room_number": 12,
"Location": "City hall",
"Timetable": {
"Monday": [
"08:30",
"10:00",
"11:30",
"13:30",
"14:14"
],
"Tuesday": [
"08:30",
"10:00",
"11:30",
"13:30",
"14:14"
],
"Sunday": [
"08:30",
"10:00",
"11:30",
"13:30",
"14:14"
]
}
}
]
Here is what I have got so far:
[
{
"value": {
"Room_number": 99,
"Location": "City campus",
"Timetable": {
"Monday": [
"08:30",
"10:00",
"11:30",
"13:30",
"14:14"
],
"Tuesday": [
"08:30",
"10:00",
"11:30",
"13:30",
"14:14"
],
"Sunday": [
"08:30",
"10:00",
"11:30",
"13:30",
"14:14"
]
}
}
},
{
"value": {
"Room_number": 12,
"Location": "City hall",
"Timetable": {
"Monday": [
"08:30",
"10:00",
"11:30",
"13:30",
"14:14"
],
"Tuesday": [
"08:30",
"10:00",
"11:30",
"13:30",
"14:14"
],
"Sunday": [
"08:30",
"10:00",
"11:30",
"13:30",
"14:14"
]
}
}
}
]
Here is a NodeJS code I am using to achieve this:
var express = require('express')
, db = require('nano')('http://localhost:5984/rooms_database')
, app = express();
// query a view
db.view('show_rooms', 'view_show_rooms', function (err, doc)
{
app.get("/", function(request,response)
{
var json_data = doc;
json_data = json_data.rows;
for (var i = 0; i < json_data.length; i++)
{
delete json_data[i].id;
delete json_data[i].key;
delete json_data[i].value._id;
delete json_data[i].value._rev;
}
response.send(json_data);
});
});
app.listen(8080);
console.log('************ Server is running! ************');
I am using standard view in the CouchDB:
function(doc)
{
emit(null, doc);
}
Can anyone tell me how to get rid of the 'value'
key in my JSON object?
You could map each item like this using Array.map()
, keeping what you want instead of removing what you don't want:
var output = json_data.rows.map(function(item) {
return {
Room_number: item.value.Room_number,
Location: item.value.Location,
Timetable: item.value.Timetable
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With