How to remove some property and characters in JSON response - javascript

I am trying to remove some property of a JSON response.
I would like to remove this part of the response (first part) :
{
"en" : 10,
"left" : false,
"right" : false,
"result" :
And remove too the last character of the response (last part) :
"}"
I have tried different ways to do this but none of them work as I would like them to.
var object_delete = ['en','left','right','result'];
for(i in object_delete){
var data = JSON.parse(JSON.stringify(data).replace(object_delete[i], ""));
}
I expect the output :
[ {
"type1" : "string data",
"type2" : "string data",
"type3" : {
"name" : "string data"
},
"red" : {
"path" : "string data",
"path2" : null,
"path3" : null,
"path4" : null
}
} ]
But the actual output is :
{
"en" : 2,
"left" : false,
"right" : false,
"result" : [ {
"type1" : "string data",
"type2" : "string data",
"type3" : {
"name" : "string data"
},
"red" : {
"path" : "string data",
"path2" : null,
"path3" : null,
"path4" : null
}
} ]
}
Anyone have any idea how this can be done?

You could try something like this:
const entry = {
"entries" : 10,
"left" : false,
"right" : false,
"result" : [
{
"type1" : "string data",
"type2" : "string data",
"type3" : "string data",
"type4" : "string data",
"type5" : "string data",
"type6" : {
"name" : "string data"
},
"red" : {
"path" : "string data",
"path2" : null,
"path3" : null,
"path4" : null
}
}
]
};
const output = [...entry["result"]];
console.log(JSON.stringify(output));
// [{"type1":"string data","type2":"string data","type3":"string data","type4":"string data","type5":"string data","type6":{"name":"string data"},"red":{"path":"string data","path2":null,"path3":null,"path4":null}}]

Related

How to remove multi nested object in javascript using id

Declaration of id
var id = 3;
I want to update this object
var obj = {"comments" : {
"commentedBy" : "test",
"date" : "",
"comment" : "Hello world",
"subComments" : {
"commentedBy" : "jaril 2",
"date" : "",
"comment" : "Hello world inside dark",
"subComments" : {
"commentedBy" : "jaril 3",
"date" : "",
"comment" : "wow working great",
"subComments" : {
"commentedBy" : "jaril 4",
"date" : "",
"comment" : "wow working great",
"commentId" : 4
},
"commentId" : 3
},
"commentId" : 2
},
"commentId" : 1
},
"dueDate" : "",
"createdDate" : "",
"lastUpdated" : "",
"checkList" : [],
"position" : 2,
"status" : "active"
}
}
Function is this
function deleteCommentId(comments){
if (comments.commentId == id)){
delete comments;
return comments;
}
if (comments.subComments) {
deleteCommentId(comments.subComments);
}
return comments;
}
Function Object is this
if(id == 1){
result[0].comments = {};
} else {
deleteCommentId(obj.comments);
}
console.log("final object==>", obj);
I want output like this
{"comments" : {
"commentedBy" : "test",
"date" : "",
"comment" : "Hello world",
"subComments" : {
"commentedBy" : "jaril 2",
"date" : "",
"comment" : "Hello world inside dark",
"commentId" : 2
},
"commentId" : 1
},
"dueDate" : "",
"createdDate" : "",
"lastUpdated" : "",
"checkList" : [],
"position" : 2,
"status" : "active"
}
}
Any help would be appreciated
Note: I want to remove nested subcomments object using id if I pass
id=3 then it should remove subcomment of 2, How can I remove subcomments of 2 if id = 3
The function below should do it.
(I removed the irrelevant parts or your object to make things a bit easier to read.)
var obj = {
"comments": {
"subComments": {
"subComments": {
"subComments": {
"commentId": 4
},
"commentId": 3
},
"commentId": 2
},
"commentId": 1
}
};
function deleteCommentId(comments, id) {
if (comments.subComments) {
if (comments.subComments.commentId === id) {
delete comments.subComments;
} else {
deleteCommentId(comments.subComments, id);
}
}
}
deleteCommentId(obj.comments, 3);
console.log(obj);
var id = 3;
var obj = {"comments":{"commentedBy":"test","date":"","comment":"Hello world","subComments":{"commentedBy":"jaril 2","date":"","comment":"Hello world inside dark","subComments":{"commentedBy":"jaril 3","date":"","comment":"wow working great","subComments":{"commentedBy":"jaril 4","date":"","comment":"wow working great","commentId":4},"commentId":3},"commentId":2},"commentId":1},"dueDate":"","createdDate":"","lastUpdated":"","checkList":[],"position":2,"status":"active"}
function deleteCommentId(comments, id) {
if (comments.subComments) {
if (comments.subComments.commentId === id) {
delete comments.subComments;
} else {
deleteCommentId(comments.subComments, id);
}
}
}
deleteCommentId(obj.comments, id);
console.log("final object==>",obj);
var expectedJSON = {"comments":{"commentedBy":"test","date":"","comment":"Hello world","subComments":{"commentedBy":"jaril 2","date":"","comment":"Hello world inside dark","commentId":2},"commentId":1},"dueDate":"","createdDate":"","lastUpdated":"","checkList":[],"position":2,"status":"active"}
console.log("Output match: ",JSON.stringify(obj) == JSON.stringify(expectedJSON));

How to update document in Mongodb collection with a field value from another collection

I have two collections
1) Floorplan_backup.
The structure of the document looks like :-
{
"_id" : ObjectId("5877e18a88db272b578572bd"),
"floorplans" : [
{
"name" : "Campus Center - Atrium",
"uuid" : NumberLong(3),
"cameras" : []
},
{
"name" : "Campus Center - Bridge ",
"uuid" : NumberLong(4),
"cameras" : []
},
{
"name" : "Campus Center - Top",
"uuid" : NumberLong(5),
"cameras" : []
},
{
"name" : "Performance Dining Hall",
"uuid" : NumberLong(6),
"cameras" : []
},
{
"name" : "Main Kitchen",
"uuid" : NumberLong(7),
"cameras" : []
},
{
"name" : "OrgChart - Board of Trustees",
"uuid" : NumberLong(8),
"cameras" : []
},
{
"name" : "OrgChart - Academic Affairs",
"uuid" : NumberLong(9),
"cameras" : []
},
{
"name" : "OrgChart - Admissions",
"uuid" : NumberLong(10),
"cameras" : []
},
{
"name" : "OrgChart - Athletics",
"uuid" : NumberLong(11),
"cameras" : []
},
{
"name" : "OrgChart - Financial",
"uuid" : NumberLong(14),
"cameras" : []
},
{
"name" : "OrgChart - HR",
"uuid" : NumberLong(15),
"cameras" : []
},
{
"name" : "OrgChart - Institutional",
"uuid" : NumberLong(16),
"cameras" : []
},
{
"name" : "OrgChart - IT",
"uuid" : NumberLong(17),
"cameras" : []
},
{
"name" : "OrgChart - Student Success",
"uuid" : NumberLong(20),
"cameras" : []
},
{
"name" : "OrgChart - Student Life",
"uuid" : NumberLong(21),
"cameras" : []
}
],
"userid" : "user-56cb3c4c0c953470865336",
"firstviewhost" : undefined
}
2) Floorplan . The structure of the document
{
"_id" : ObjectId("589025b03422cafc09913363"),
"userid" : "user-56cb3c4c0c953470865336",
"firstviewhost" : "dean.vizsafe.com"
}
I would like to update the firstviewhost in Floorpla_backup with the value "dean.vizsafe.com" by matching the userid of both the collections.
The javascript code used is :-
db.floorplan_backup.find().forEach(function (doc1) {
var doc2 = db.floorplan.find({ userid: doc1.userid }, { firstviewhost: 1 });
if (doc2 != null) {
doc1.firstviewhost = doc2.firstviewhost;
db.floorplan_backup.save(doc1);
}
});
But is is not giving me the desired resultset . Could you let me know what is wrong with this?
doc2 is an array. Use findOne to get an object.
db.floorplan_backup.find().forEach(function (doc1) {
var doc2 = db.floorplan.findOne({ userid: doc1.userid }, { firstviewhost: 1 });
if (doc2 != null) {
doc1.firstviewhost = doc2.firstviewhost;
db.floorplan_backup.save(doc1);
}
});
find returns you a cursor, instead use findOne.
db.floorplan_backup.find().forEach(function (doc1) {
var doc2 = db.floorplan.findOne({ userid: doc1.userid }, { firstviewhost: 1 });
if (doc2 != null) {
doc1.firstviewhost = doc2.firstviewhost;
db.floorplan_backup.save(doc1);
}
});

Not able to Parse JSON from Jenkins properly

I'm trying to parse JSON from Jenkins's API using request
request({method: 'GET', url}, function(error, response, body) {
console.log(body.toString());
var output_json = JSON.parse(body.toString());
console.log(output_json);
}
After I parse the JSON usingJSON.parse(), few values in the tags are lost.
Console output of text output before parsing JSON
{
"_class" : "My.FreeProject",
"actions" : [
{
},
{
"_class" : "FreeProject.Property",
"parameterDefinitions" : [
{
"_class" : "org.choice.Parameter",
"defaultParameterValue" : {
"_class" : "Property",
"value" : "master19.7.0"
},
"description" : "",
"name" : "BUILD_TAG",
"type" : "ChoiceParameter"
},
{
"_class" : "Parameter",
"defaultParameterValue" : {
"_class" : "Value",
"value" : ""
},
"description" : "Random Text",
"name" : "MY_TEST",
"type" : "StringParameterDefinition"
},
{
"_class" : "org.myclass",
"defaultParameterValue" : {
"_class" : "org.newclass"
},
"description" : "",
"name" : "TESTING",
"type" : "NodeParameterDefinition"
}
]
},
{
Console output of text output after parsing JSON
{ _class: 'My.FreeProject',
actions:
[ {},
{ _class: 'FreeProject.Property',
parameterDefinitions: [Object] },
{},
{},
{},
{},
{},
{},
{},
{},
{ _class: 'com.myclass' } ],
So after parsing JSON, I'm losing some of the text values. Is there a way I could retrieve all the information of the JSON from Jenkins? Thanks
It doesn't look like anything is missing. The value of parameterDefinitions is just collapsed. Either there is a toggle you can click on to expand it, or use console.dir instead.
Example from the Chrome console. Note how it shows [Array[1]] instead of [[[[]]]] after parsing the string into an object. However, the values is still four nested arrays.

unable to fetch value from JSON array in javascript

I'm unable to fetch JSON value to check if the person is wearing glasses while taking photo, the value relies in four arrays: photos, tags, attributes and glasses, I want to check the value "value" is true or false. I use alert() to test if value fetched but nothing comes out. I don't know which part gets wrong
I use JavaScript to fetch the JSON value as follows:
var facesDet = $.getJSON( APIdetect, function() {
console.log( "success" );
console.log(facesDet);
})
.done(function (facesDet, tid) {
var VALUEIWANT = facesDet.photos[0].tags[0].attributes[0].gender[0].value;
});
The JSON value looks as follows:
{
"photos" : [
{
"url" : "http://tinyurl.com/673cksr",
"pid" : "F#0c95576847e9cd7123f1e304476b59ae_59ec9bb2ad15f",
"width" : 375,
"height" : 409,
"tags" : [
{
"tid" : "TEMP_F#0c95576847e9cd7123f1e304b1dcbe53_59ec9bb2ad15f_56.53_40.83_0_1",
"recognizable" : true,
"confirmed" : false,
"manual" : false,
"width" : 30.67,
"height" : 28.12,
"center" : { "x" : 56.53, "y" : 40.83},
"eye_left" : { "x" : 66.93, "y" : 33.99},
"eye_right" : { "x" : 51.73, "y" : 33.99},
"yaw" : -16,
"roll" : 0,
"pitch" : 0,
"attributes" : {
"face" : { "value" : "true", "confidence" : 82 },
"gender" : { "value" : "female", "confidence" : 80 },
"glasses":{"value" : "true", "confidence" : 100},
"dark_glasses":{"value" : "true", "confidence" : 72},
"smiling":{"value" : "false", "confidence" : 35}
}
}
]
}
],
"status" : "success",
"usage" : {
"used" : 1,
"remaining" : 99,
"limit" : 100,
"reset_time_text" : "Fri, 21 September 2012 12:57:19 +0000",
"reset_time" : 1348232239
}
}
You tryed to take value from gender like from array:
var VALUEIWANT = facesDet.photos[0].tags[0].attributes[0].gender[0].value;
But in your json its object, so you should use:
var VALUEIWANT = facesDet.photos[0].tags[0].attributes.gender.value;
Your access is wrong. You have an object, but you treat it like an array
var VALUEIWANT = facesDet.photos[0].tags[0].attributes[0].gender[0].value;
// ^^^ and ^^^
right access:
var VALUEIWANT = facesDet.photos[0].tags[0].attributes.gender.value;
data:
"attributes" : {
"face" : { "value" : "true", "confidence" : 82 },
"gender" : { "value" : "female", "confidence" : 80 },
"glasses":{"value" : "true", "confidence" : 100},
"dark_glasses":{"value" : "true", "confidence" : 72},
"smiling":{"value" : "false", "confidence" : 35}
}
try this one.
facesDet.photos[0].tags[0].attributes.gender.value
getJSON return a promise not the response, you need to use this code:
$.getJSON( APIdetect, function(facesDet) {
console.log( "success" );
console.log(facesDet);
})

JSON.parse: unexpected character error

I'm having a problem. I have the list of JSON objects in a separate file but want to parse them into a data table. Every time I try to parse them, I get an unexpected character error...
Here is the code
var myJSONObject = {
"orders" : [{
"orderId" : "K2_001",
"dueDate" : "04/15/2012",
"priority" : 1,
"description" : "ORDER K2_001"
}, {
"orderId" : "K2_002",
"dueDate" : "04/20/2012",
"priority" : 2,
"description" : "ORDER K2_002"
}, {
"orderId" : "K2_003",
"dueDate" : "04/23/2012",
"priority" : 3,
"description" : "ORDER K2_003"
}, {
"orderId" : "K2_004",
"dueDate" : "04/27/2012",
"priority" : 4,
"description" : "ORDER K2_004"
}, {
"orderId" : "K2_005",
"dueDate" : "04/30/2012",
"priority" : 5,
"description" : "ORDER K2_005"
}, {
"orderId" : "K2_006",
"dueDate" : "05/05/2012",
"priority" : 6,
"description" : "ORDER K2_006"
}, {
"orderId" : "K2_007",
"dueDate" : "05/12/2012",
"priority" : 7,
"description" : "ORDER K2_007"
}, {
"orderId" : "K2_008",
"dueDate" : "05/14/2012",
"priority" : 8,
"description" : "ORDER K2_008"
}]
};
var jsonObject2 = Y.JSON.parse(myJSONObject.responseText);
JSON is a string representation of a (JavaScript) object. A JSON string, is a valid JavaScript object.
Example:
var JSON = '{"Hello": "world", "test": [1,2,3]}'; // <= This is JSON, it's a string
var obj = {"Hello": "world", "test": [1,2,3]}; // <= This is a JavaScript object
In your example, myJSONObject is already an object, it doesn't need to be "parsed".
This is one problem i had faced and the solution is related to usage of double quotes.
http://mywpf-visu.blogspot.in/2012/04/json-encountered-unexpected-character.html

Categories