unable to fetch value from JSON array in javascript - 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);
})

Related

How to extract data from Firebase realtime database JSON-export

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

$multiply only supports numeric types, not array

I am trying to use the $multiply operator in MongoDB.
STEP 1
db.message1064sd_00011_3744.aggregate([
{$project : {
"prices.00026" : 1,
priceReal : {
price : "$prices.00026",
}
}},
{ $match : { "priceReal.price" : {$gt : 30 } } },
{ $limit : 1 }
])
I am getting the results
{
"result" : [
{
"_id" : "54884_00011_001",
"prices" : {
"00026" : 34.43
},
"priceReal" : {
"price" : 34.43
}
}
],
"ok" : 1
}
STEP 2
But, when I use $multiply, I get
$multiply only supports numeric types, not array
db.message1064sd_00011_3744.aggregate([
{$project : {
"prices.00026" : 1,
priceReal : {
price : { $multiply : ["$prices.00026", 1 ] },
}
}},
{ $match : { "priceReal.price" : {$gt : 30 } } },
{ $limit : 1 }
])
Help me anybody
Example document which I can get from db.message1064sd_00011_3744.findOne()
{
"_id" : "25906_00011_001",
"Message_ID" : 25906,
"Subdivision_ID" : 3747,
"Sub_Class_ID" : 6300,
"Checked" : 1,
"Nomencl_ID" : "10000014597",
"manufacturer_ID" : "П1170",
"disableIfZero" : 0,
"Discontinued" : 0,
"New" : 0,
"Nomencl_Group_ID" : 28,
"Nalichie" : "Мало",
"sort" : 99,
"Warehouse_ID" : "00011",
"ParentWarehouse_ID" : "00011",
"Kachestvo" : "001",
"Svobod_Nalichie" : "10",
"Svobod_sort" : 10,
"character" : [],
"prices" : {
"00014" : 1.51,
"00015" : 1.45,
"00016" : 1.41,
"00017" : 1.38,
"00018" : 1.35,
"00019" : 1.33,
"00021" : 1.31,
"00022" : 1.29,
"00023" : 1.28,
"00024" : 1.27,
"00025" : 1.25,
"00026" : 1.24
},
"price" : {
"Curr_ID" : 840,
"ChangePriceTime" : "2017-01-22 19:18:21",
"PriceUpDown" : "up",
"callPrice" : 0,
"Price_Value_RODP" : 1.24,
"Price_Value_RUR" : 72.04000000000001
},
"sName" : "чип epson m2300{m2400{mx20 8k (elp, китай)",
"sNomencl_ID" : "10000014597",
"sNomencl_Articul_Proizvod" : "elp-ch-e2300-8k",
"sItemID" : "elp-ch-e2300-8k",
"EnglishName" : "cZ277",
"begin_vl" : 121,
"Hidden_URL" : "/netshop/cZ079/cZ270/cZ277/",
"Checked_Subdivision" : 1
}
In case you want to ensure that only data from a certain data type enters your result set, for example in an aggregate query, you can add the following filter to the match:
{$match: {
// ... your query
, "priceReal.price": { $type : "double" }
}

How to query single document which contains specific value of some element in array of arrays MongoDB

I have an document that contains array of arrays i am using embedded document in MongoDb.Say i have collection name Orders looks like:-
"_id" : "HjPGrdkffg7dQPtiX",
"ListOrdersResult" : [
{
"Orders" : {
"Order" : [
{
"LatestShipDate" : "2016-01-13T18:29:59Z",
"OrderType" : "StandardOrder",
"PurchaseDate" : "2016-01-11T10:24:49Z",
"PaymentExecutionDetail" : {
"PaymentExecutionDetailItem" : {
"PaymentMethod" : "COD",
"Payment" : {
"CurrencyCode" : "INR",
"Amount" : "839.30"
}
}
},
"BuyerEmail" : "vccdbptpx2ssd74882#marketplace.amazon.in",
"AmazonOrderId" : "402-4031538-7451469",
"LastUpdateDate" : "2016-01-14T06:47:17Z",
"ShipServiceLevel" : "IN Exp Dom 2",
"NumberOfItemsShipped" : "1",
"OrderStatus" : "Shipped",
"SalesChannel" : "Amazon.in",
"ShippedByAmazonTFM" : "false",
"LatestDeliveryDate" : "2016-01-19T18:29:59Z",
"NumberOfItemsUnshipped" : "0",
"BuyerName" : "xyz",
"EarliestDeliveryDate" : "2016-01-13T18:30:00Z",
"OrderTotal" : {
"CurrencyCode" : "INR",
"Amount" : "839.30"
},
"IsPremiumOrder" : "false",
"EarliestShipDate" : "2016-01-11T18:30:00Z",
"MarketplaceId" : "A21TJRRWUN4KGVC",
"FulfillmentChannel" : "MFN",
"TFMShipmentStatus" : "Delivered",
"PaymentMethod" : "COD",
"ShippingAddress" : {
"StateOrRegion" : "HARYANA",
"City" : "GURGAON",
"Phone" : "9999999999",
"CountryCode" : "IN",
"PostalCode" : "122001",
"Name" : "Murthy",
"AddressLine1" : "House No. , J Block, Badshahpur"
},
"IsPrime" : "false",
"ShipmentServiceLevelCategory" : "Expedited"
},
{
"LatestShipDate" : "2016-01-13T18:29:59Z",
"OrderType" : "StandardOrder",
"PurchaseDate" : "2016-01-11T13:16:49Z",
"PaymentExecutionDetail" : {
"PaymentExecutionDetailItem" : {
"PaymentMethod" : "COD",
"Payment" : {
"CurrencyCode" : "INR",
"Amount" : "899.40"
}
}
},
"BuyerEmail" : "xyz#marketplace.amazon.in",
"AmazonOrderId" : "402-2142159-5087541",
"LastUpdateDate" : "2016-01-14T06:47:15Z",
"ShipServiceLevel" : "IN Exp Dom 2",
"NumberOfItemsShipped" : "1",
"OrderStatus" : "Cancel",
"SalesChannel" : "Amazon.in",
"ShippedByAmazonTFM" : "false",
"LatestDeliveryDate" : "2016-01-19T18:29:59Z",
"NumberOfItemsUnshipped" : "0",
"BuyerName" : "demo prakash",
"EarliestDeliveryDate" : "2016-01-13T18:30:00Z",
"OrderTotal" : {
"CurrencyCode" : "INR",
"Amount" : "899.40"
},
"IsPremiumOrder" : "false",
"EarliestShipDate" : "2016-01-11T18:30:00Z",
"MarketplaceId" : "A21TJEUUN4WGV",
"FulfillmentChannel" : "MFN",
"TFMShipmentStatus" : "Delivered",
"PaymentMethod" : "COD",
"ShippingAddress" : {
"StateOrRegion" : "DELHI",
"City" : "DELHI",
"Phone" : "99999999",
"CountryCode" : "IN",
"PostalCode" : "110038",
"Name" : "Demo prakash",
"AddressLine1" : "Hn 638 gali n 04 Wazirabad new delhi"
},
"IsPrime" : "false",
"ShipmentServiceLevelCategory" : "Expedited"
},
}
]
},
"CreatedBefore" : "2015-03-19T06:17:59Z"
}
],
"ResponseMetadata" : {
"RequestId" : "cf94645e-ada7-4ec6-b161-a97d07a77817"
},
"seller_user_id" : "yg4e34ccodzf3GPR2",
}
So as you can see this is the single document that contains the whole data of array i want to fetch the orders whose status is cancel from this order array.
So for that i have use :-
var orderDetails =
orders.find({"ListOrdersResult.Orders.Order":{$elemMatch:
{ OrderStatus:"Canceled"}}}).fetch();
Also i tried with:-
orders.find({"ListOrdersResult.Orders.Order.OrderStatus":'Canceled'}).fetch();
So this will return the whole document that contains status as canceled and other as well but i want only selected result from the document that contains status as pending.
So is there any way in mongoDb to query a selected value from a single document that contains nested array of arrays as object.
Or I need to staore the values into diff diff documents thats only the solution.
Any help would be appriciated please contribute
Thanks!
You can go with any meteor aggregate package. You can use match and then group the data then send it to client-side.Like :
var ordersLines = orders.aggregate([
{$unwind : "$ListOrdersResult.Orders.Order"},
{$match : { OrderStatus:"Canceled"} },
{$project : {
OrderType : '$ListOrdersResult.Orders.Order.OrderType'
....
}
}
]);
return ordersLines;
But I suggest you go with a different document.

Meteor find is returning null with variable [duplicate]

This question already has an answer here:
Updating template with session variable and subscription/publication only updates with previous query and appends information
(1 answer)
Closed 7 years ago.
Description of the problem:
I have two collections videos and specs. videos collection has a key called spec which corresponds to a specs id. Both collections are not empty.
My Template Helper:
Template.List.helpers({
videos: function(){
var vids = Videos.find({ online: false}).fetch();
return vids.map(function(value){
console.log("id: " + value.spec);
console.log(Specs.find({ id: value.spec }).fetch());
//Issue here
value.specName = Specs.find({ id: value.spec }).fetch()[0].name;
return value;
});
}
});
As you can see in my template helper I loop through the video array and add specName to the array.
The main problem comes from this specific line:
value.specName = Specs.find({ id: value.spec }).fetch()[0].name;
and more specifically this line: Specs.find({ id: value.spec }).fetch()
which will return null every time.
What I've tried:
Naturally, my first thought would be to check what value.spec returns. And it returns an int between 1 and 15 (included) which is right. If value.spec is right, then how come the find() doesn't return anything?
I then decided to hard set it and tried this:
Specs.find({ id: 2}).fetch()
And this worked. Which is weird because on multiple occasions value.spec returns 2...
I've also tried intParse(value.spec) just in case, however that didn't work either.
Question
Why does Specs.find({ id: value.spec }).fetch() return null knowing that value.spec is set correctly and that a hard coded number works?
Requested json data: (from meteor mongo)
specs:
{ "_id" : "XKXHtQuiFsAew3dDy", "id" : 1, "name" : "Endocrine surgery" }
{ "_id" : "68jFidAMXTXpQtQye", "id" : 2, "name" : "General and digestive" }
{ "_id" : "GZSXToRXMfJgnH3CY", "id" : 3, "name" : "Pediatric surgery" }
{ "_id" : "T2mBz2gsXEqQaybmq", "id" : 4, "name" : "Thoracic surgery" }
{ "_id" : "hnuQzZiPKvYYDZhc8", "id" : 5, "name" : "Equipment" }
{ "_id" : "byE3A6HchvfhKdmR8", "id" : 6, "name" : "Gynecology" }
{ "_id" : "u5rrPB7asGW3NC6B2", "id" : 7, "name" : "Urology" }
{ "_id" : "umxKvR66oEx5dRppf", "id" : 8, "name" : "Cardiovascular surgery" }
{ "_id" : "bPcBTZn3t5ubRRcrQ", "id" : 9, "name" : "Endoscopic surgery" }
{ "_id" : "yNyAqQPoreNtdRZ34", "id" : 10, "name" : "NOTES" }
{ "_id" : "KG794eakRaztEqehG", "id" : 11, "name" : "Robotic surgery" }
{ "_id" : "QBrtvTg4GT7Tf7cAJ", "id" : 12, "name" : "Skull base surgery" }
{ "_id" : "HEhq6oBjuuMnrxE5a", "id" : 13, "name" : "Arthroscopy and upper limb surgery" }
{ "_id" : "xwpgHqZpBQP7WAnd5", "id" : 14, "name" : "Single port surgery" }
{ "_id" : "K4BgFupwNdDGD3449", "id" : 15, "name" : "Telemicrosurgery" }
videos:
{ "_id" : "L5Qi7YRRhn6Sfcjk8", "id" : "vd01en1065e", "title" : "Right inguinal hernia: open plug technique", "authors" : [ "E Pelissier" ], "date_published" : "2004-09-27", "abstract" : "", "tags" : [ "" ], "spec" : 2, "private" : true, "online" : false }
{ "_id" : "M8cuLW6KNCqKeP9vF", "id" : "vd01en1074e", "title" : "Laparoscopic splenectomy, posterior approach", "authors" : [ "D Mutter", " F Rubino" ], "date_published" : "2004-09-27", "abstract" : "", "tags" : [ "" ], "spec" : 2, "private" : true, "online" : false }
{ "_id" : "Ptzrxw8GifeMvQk9k", "id" : "vd01en1090e", "title" : "Intussusception of the intestine in the newborn", "authors" : [ "F Becmeur", " D Christmann", " I Kauffmann" ], "date_published" : "2004-09-27", "abstract" : "", "tags" : [ "" ], "spec" : 3, "private" : true, "online" : false }
{ "_id" : "oHWcX3vCBHuZQM9hR", "id" : "vd01en1103e_2", "title" : "Appendicular peritonitis: laparoscopic conversion", "authors" : [ "B Navez" ], "date_published" : "2001-11-05", "abstract" : "", "tags" : [ "" ], "spec" : 2, "private" : true, "online" : false }
{ "_id" : "6uzmxYxhd5DDuS2gG", "id" : "vd01en1108e", "title" : "Diaphragmatic hernias", "authors" : [ "F Becmeur" ], "date_published" : "2001-11-28", "abstract" : "", "tags" : [ "" ], "spec" : 3, "private" : true, "online" : false }
{ "_id" : "yHqruiQYeeQ9SDHpH", "id" : "vd01en1112e", "title" : "Laparoscopic excision of the cystic stump", "authors" : [ "J Leroy" ], "date_published" : "2004-09-27", "abstract" : "", "tags" : [ "" ], "spec" : 2, "private" : true, "online" : false}
{ "_id" : "fmjtk5WAEKitMxyGj", "id" : "vd01en1114e", "title" : "Laparoscopic gastric banding in a patient with a BMI of 40", "authors" : [ "JM Zimmermann", " D Fölscher" ], "date_published" : "2004-09-27", "abstract" : "", "tags" : [ "" ], "spec" : 2, "private" : true, "online" : false}
I've been stuck on this problem for a couple hours, I didn't want to post this on SO since I do believe it's a simple problem. However, it is mind boggling.
The problem here is the publication/subscription issue. You did not mention it, how do you handle publications and subscriptions but that is most likely the issue. What is happening is that when you browse your videos collection, which subscription is ready (since you hae any data in it) here: Videos.find({ online: false}) that not necessarly means that (in that precise moment) subscription handling Spec collection is ready as well. So even if on the server the query is working, on the client it's null, because the data are not synced between client and server YET. so you have to wait until both subscriptions are ready somehow. You can use template subscriptions, or waitOn function in your router.
First of all check on server side that your publication and subscription is ready and returning you back data check this like as
Meteor.publish('Videso', function () {
var result= Videso.find({});
console.log(result);
return result
});
if console.log returning you records on server side i have a solution then your
problem
the solution of your problem is that
Template.List.helpers({
videos: function(){
var vids = Videos.find({ online: false}).map(function (value) {
console.log("id: " + value.spec);
var idValue = Specs.findOne({ "id": value.spec })
console.log("idValue===",idValue)
return _.extend(value, idValue);
});
console.log(vids); //here merged data of both table having
// relvent record will returned
return vids;
}
});
Just implement it and check data on your template it will be ready for you and vote me then :)

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