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.
Related
I have a Firebase realtime-database export-json that has nested informations, that I would like to extract. The structure of the JSON looks like this:
{
"users" : {
"024w97mv8NftGFY8THfQIU6PhaJ3" : {
"email" : "xxx",
"id" : "024w97mv8NftGFY8THfQIU6PhaJ3",
"name" : "xxx",
"items" : {
"-LQL9n-r9BGdo3HJZ2sk" : {
"disliked" : true,
"id" : 396,
"name" : "Aaa"
},
"-LQL9oO63nH-QW2w6zz0" : {
"liked" : true,
"id" : 3674,
"name" : "Bbb"
}
}
},
"0ERLT5DLRvbZUnjlnM7Ow0qItpz2" : {
"email" : "zzz",
"id" : "0ERLT5DLRvbZUnjlnM7Ow0qItpz2",
"name" : "zzz",
"items" : {
"-LIZnriSVQMzqTsPFNYa" : {
"id" : 396,
"liked" : true,
"name" : "Aaa"
},
"-LIZnrzOuk4WyjqEiLG8" : {
"disliked" : true,
"id" : 4805,
"name" : "Ccc"
}
}
}
}
}
What I need to achieve is getting a list of all liked item-names, and ideally counting how often an item is liked.
Is there a simple tool or script to do that? Ruby or Javascript would be preferred. Thanks a lot!
You can parse your JSON data in ruby like below
result_hash = JSON.parse(result)
result_ary = result_hash["users"].collect do |k,v|
v["items"].values.select{|v1| v1["liked"] == true }
end
result_data = result_ary.flatten
result of parsing
=> [{"liked"=>true, "id"=>3674, "name"=>"Bbb"}, {"id"=>396, "liked"=>true, "name"=>"Aaa"}]
Now its very easy for getting your required result
result_data.collect{|x| x["name"] }
=> ["Bbb", "Aaa"]
result_data.count {|x| x["name"] == "Aaa"}
=> 1
result_data.count {|x| x["name"] == "Bbb"}
=> 1
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}}]
I'm kindly requesting your help for this query that I need to do and I'm not very proficient yet in MongoDB. My data structure looks like this:
db.getCollection('EventDateValidation').find({}):
/* 1 */
{
"_id" : ObjectId("5b7b2e3ae5e2100007717d81"),
"_class" : "com.overwatch.common.model.EventDateValidation",
"caseNo" : "OW000002269122201810201135",
"loanNo" : "000002269122",
"eventType" : "BREACLETTR",
"validationStepData" : [
{
"startDate" : {
"isChecked" : "Y",
"comments" : "",
"auditedBy" : "Mahalakshmi M",
"auditedDate" : "2018-12-12"
}
},
{
"completedDate" : {
"isChecked" : "Y",
"comments" : "",
"auditedBy" : "Mahalakshmi M",
"auditedDate" : "2018-12-13"
}
},
{
"deadlineDate" : {
"isChecked" : "Y",
"comments" : "",
"auditedBy" : "Mahalakshmi M",
"auditedDate" : "2018-12-13"
}
}
]
}
/* 2 */
{
"_id" : ObjectId("5b7c11095c2b4d0007bc8c54"),
"_class" : "com.overwatch.common.model.EventDateValidation",
"caseNo" : "OW000000854076201808181158",
"loanNo" : "000000854076",
"eventType" : "FORSALAPPR",
"validationStepData" : [
{
"startDate" : {
"comments" : ""
}
},
{
"completedDate" : {
"comments" : "Received Date = 8/4/2017"
}
},
{
"deadlineDate" : {
"comments" : ""
}
}
]
}
/* 3 */
{
"_id" : ObjectId("5b7ad05d5c2b4d0007bc8631"),
"_class" : "com.overwatch.common.model.EventDateValidation",
"caseNo" : "OW000000873954201810201235",
"loanNo" : "000000873954",
"eventType" : "HUDNOTIFCA",
"validationStepData" : [
{
"startDate" : {
"isChecked" : "Y",
"comments" : "",
"auditedBy" : "Brett Scott",
"auditedDate" : "2018-09-25"
}
},
{
"completedDate" : {
"isChecked" : "Y",
"comments" : "",
"auditedBy" : "Brett Scott",
"auditedDate" : "2018-09-25"
}
},
{
"deadlineDate" : {
"isChecked" : "Y",
"comments" : "",
"auditedBy" : "Brett Scott",
"auditedDate" : "2018-09-25"
}
}
]
}
From this collection, I need to find the documents that have an "auditedDate" in the "deadlineDate". In this example, I would find the documents 1 and 3. Please help me as I'm stuck on this one.
I have tried
db.getCollection('EventDateValidation').find({"validationStepData.deadlineDate.auditedDate":{$exists:true}})
But doesn't seem to work. Help please!
Just for clearing things up: the query in the question works well. I chatted with #Gabriel, and the problem was that Robomongo added hidden non-printable unicode characters to the query.
All in all, for any interested nomads, here are few ways to query an array of objects:
1) Implicit $elemMatch / simple dot notation on an array:
db.getCollection('EventDateValidation').find({"validationStepData.deadlineDate.auditedDate": {$exists:true}})
2) Explicit $elemMatch (we can have multiple query criteria):
db.getCollection('EventDateValidation').find({"validationStepData": { $elemMatch: {"deadlineDate.auditedDate" : {$exists:true} }}})
3) Array dot notation with an index position (when we know the exact position of an element inside an array):
db.getCollection('EventDateValidation').find({"validationStepData.2.deadlineDate.auditedDate": {$exists:true}})
Dot notation wouldn't work since you have an array of objects within validationStepData. You could use $elemMatch to apply your query conditions to the array elements that match your expression.
db.getCollection('EventDateValidation').find({"validationStepData" : { $elemMatch: {"deadlineDate.auditedDate" : {$exists:true} }}})
I am getting a weird problem when I run in shell for mongoDB.
My collection structure is like below in mongoDB.The type of ServiceList items must be integer.
{
"_id" : ObjectId("56565"),
"Contact" : "",
"Email" : "",
"AppList" : [
{
"Code" : "",
"Usage" : NumberInt(542),
"LicenceType" : "Monthly",
"CreatedDate" : ISODate("2015-07-29T06:15:20.520+0300"),
"ServiceList" : [
1,5,8
]
}
]
}
My aim to run this code is converting Usage type from int32 to int64.It works fine.When I run the code below. something weird occours, and the type of the items inside AppList.ServiceList is converted to double from int32 itself.
db.CustomerInfo.find({}).forEach( function (item) {
if(item.AppList != undefined){
item.AppList.forEach(function (x){
x.Usage = NumberLong(x.Usage);
});
db.CustomerInfo.save(item);
}
});
and it seems like this,
{
"_id" : ObjectId("56565"),
"Contact" : "",
"Email" : "",
"AppList" : [
{
"Code" : "",
"Usage" : NumberLong(542),
"LicenceType" : "Monthly",
"CreatedDate" : ISODate("2015-07-29T06:15:20.520+0300"),
"ServiceList" : [
1.0,5.0,8.0
]
}
]
}
How can I solve this problem ?
MongoDB version:3.4.9
MongoShell version:v3.4.9
My mongoDB collection looks like this :
{
"_id" : ObjectId("5070310e0f3350482b00011d"),
"emails" : [
{
"_id" : ObjectId("5070310e0f3350482b000120"),
"_type" : "Email",
"name" : "work",
"email" : "peter.loescher#siemens.com",
"current" : true
}
]
}
and this is the .js code i use to print the contents :
c = db.contacts.findOne( { "emails.email" : { $ne : null } }, { "emails" : 1 } )
print(c._id.toString() + " " + c.emails[0]);
when I try to run this javascript file, it is just displaying the id but not the email array.
output:
5070310e0f3350482b00011d [object bson_object]
but when I try c.emails[0].email is is giving proper result. i.e. peter.loescher#siemens.com
All I need is I want to display the whole emails embedded object.
i.e.
"emails" : [
{
"_id" : ObjectId("5070310e0f3350482b000120"),
"_type" : "Email",
"name" : "work",
"email" : "peter.loescher#siemens.com",
"current" : true
}
]
Where I am going wrong?. Any help would be appreciated.
You need printjson to output a nicely formatted JSON:
printjson(c.emails[0]);
Here it is the documentation.