In a json response, there are multiple nested objects named "generic_equipment_name" and I need to confirm if there is one whose value is "Push-button start". How can I check for the presence of this "generic_equipment_name" as well as the value of its associated object "generic_equipment_value"? Not all responses will contain this object, and other objects not listed below may be included (response is dynamic) which is why I'm looking to check for it. Sample response:
"standard_generic_equipment": [
{
"generic_equipment_category_group": "Interior",
"generic_equipment_categories": [
{
"generic_equipment_category": "Air Conditioning",
"generic_equipment": [
{
"generic_equipment_name": "Air filtration",
"generic_equipment_values": [
{
"generic_equipment_id": "429",
"generic_equipment_value": "active charcoal"
}
]
}
]
},
{
"generic_equipment_category": "Comfort Features",
"generic_equipment": [
{
"generic_equipment_name": "Armrests",
"generic_equipment_values": [
{
"generic_equipment_id": "712",
"generic_equipment_value": "rear center folding with pass-thru"
},
{
"generic_equipment_id": "1557",
"generic_equipment_value": "rear center with cupholders"
}
]
},
{
"generic_equipment_name": "Center console trim",
"generic_equipment_values": [
{
"generic_equipment_id": "2865",
"generic_equipment_value": "leatherette"
},
{
"generic_equipment_id": "558",
"generic_equipment_value": "wood"
}
]
}
]
},
{
"generic_equipment_category": "Convenience Features",
"generic_equipment": [
{
"generic_equipment_name": "Power steering",
"generic_equipment_values": [
{
"generic_equipment_id": "862",
"generic_equipment_value": "variable/speed-proportional"
}
]
},
{
"generic_equipment_name": "Power windows",
"generic_equipment_values": [
{
"generic_equipment_id": "1425",
"generic_equipment_value": "lockout button"
}
]
},
{
"generic_equipment_name": "Push-button start",
"generic_equipment_values": [
{
"generic_equipment_id": "1665",
"generic_equipment_value": ""
}
]
},
{
"generic_equipment_name": "Reading lights",
"generic_equipment_values": [
{
"generic_equipment_id": "850",
"generic_equipment_value": "front"
},
{
"generic_equipment_id": "1700",
"generic_equipment_value": "rear"
}
]
},
{
"generic_equipment_name": "Rearview mirror",
"generic_equipment_values": [
{
"generic_equipment_id": "2154",
"generic_equipment_value": "auto-dimming"
}
]
}
]
}
]
}
]
Related
i have some trouble in control device from google assistant. I have synced 2 devices to google assistant, only have diffence in ids and names, just like this
{
"payload":{
"devices":[
{
"id":"FtxvyT43euwCPT4b7pVzam-d113a8cb2ccb4f6ba9e4831cb90a7695",
"type":"action.devices.types.AC_UNIT",
"traits":[
"action.devices.traits.OnOff",
"action.devices.traits.TemperatureSetting",
"action.devices.traits.FanSpeed",
"action.devices.traits.Modes"
],
"name":{
"defaultNames":[
"西蒙电气"
],
"name":"I7温控器中弘",
"nicknames":[
"i7-温控器-中弘"
]
},
"willReportState":false,
"attributes":{
"availableFanSpeeds":{
"speeds":[
{
"speed_name":"auto",
"speed_values":[
{
"speed_synonym":[
"auto",
"automatic"
],
"lang":"en"
}
]
},
{
"speed_name":"low",
"speed_values":[
{
"speed_synonym":[
"low",
"slow"
],
"lang":"en"
}
]
},
{
"speed_name":"medium",
"speed_values":[
{
"speed_synonym":[
"medium"
],
"lang":"en"
}
]
},
{
"speed_name":"high",
"speed_values":[
{
"speed_synonym":[
"high",
"fast"
],
"lang":"en"
}
]
}
],
"ordered":true
},
"availableModes":[
{
"name":"mode",
"name_values":[
{
"name_synonym":[
"mode"
],
"lang":"en"
}
],
"settings":[
{
"setting_name":"Sleep",
"setting_values":[
{
"setting_synonym":[
"sleep"
],
"lang":"en"
}
]
},
{
"setting_name":"Airsupply",
"setting_values":[
{
"setting_synonym":[
"airsupply",
"air supply"
],
"lang":"en"
}
]
}
],
"ordered":true
}
],
"availableThermostatModes":[
"heat",
"cool"
],
"thermostatTemperatureUnit":"C"
},
"deviceInfo":{
"manufacturer":"西蒙电气",
"model":"I7温控器中弘",
"hwVersion":"1.0",
"swVersion":"2.0"
},
"customData":{}
},
{
"id":"bYJDj2XYve3yLKPUnjNCp3-bbb06180c6c647738b3eb12e3d474e71",
"type":"action.devices.types.AC_UNIT",
"traits":[
"action.devices.traits.OnOff",
"action.devices.traits.TemperatureSetting",
"action.devices.traits.FanSpeed",
"action.devices.traits.Modes"
],
"name":{
"defaultNames":[
"西蒙电气"
],
"name":"I7中央空调",
"nicknames":[
"i7-中央空调"
]
},
"willReportState":false,
"attributes":{
"availableFanSpeeds":{
"speeds":[
{
"speed_name":"auto",
"speed_values":[
{
"speed_synonym":[
"auto",
"automatic"
],
"lang":"en"
}
]
},
{
"speed_name":"low",
"speed_values":[
{
"speed_synonym":[
"low",
"slow"
],
"lang":"en"
}
]
},
{
"speed_name":"medium",
"speed_values":[
{
"speed_synonym":[
"medium"
],
"lang":"en"
}
]
},
{
"speed_name":"high",
"speed_values":[
{
"speed_synonym":[
"high",
"fast"
],
"lang":"en"
}
]
}
],
"ordered":true
},
"availableModes":[
{
"name":"mode",
"name_values":[
{
"name_synonym":[
"mode"
],
"lang":"en"
}
],
"settings":[
{
"setting_name":"Sleep",
"setting_values":[
{
"setting_synonym":[
"sleep"
],
"lang":"en"
}
]
},
{
"setting_name":"Airsupply",
"setting_values":[
{
"setting_synonym":[
"airsupply",
"air supply"
],
"lang":"en"
}
]
}
],
"ordered":true
}
],
"availableThermostatModes":[
"heat",
"cool"
],
"thermostatTemperatureUnit":"C"
},
"deviceInfo":{
"manufacturer":"西蒙电气",
"model":"I7中央空调",
"hwVersion":"1.0",
"swVersion":"2.0"
},
"customData":{}
}
]
}
}
When i say something like set aircondition temperature to 22℃, 'i7-中央空调' returns a success, but 'i7-温控器-中弘' shows 'not support this mode'. So what cause it? And, what should i do?
You should have WillReportState = true since you do not have commandOnlyTemperatureSetting = true. You either need to specify the device cannot report state and is commandOnly or have the devices report state. Check out the TemperatureSetting trait attributes for more details.
If you're still having issues after that update, check yourlogs to identify the particular issue for that device.
I have a multidimensional array below is my JSON data, I want the output mentioned below. Thanks for the help. I have tried two methods, 1. project with map there I am unable to group the data.
2. tried with unwind and group there unable to get the inside errorField array ("recordCount" : 73).
By using groupBy key needs to group by the results, if filed key matches & inside field--> detail key matches we need to merge those do document.
My JSON:
[
{
"_id":{
"$oid":"60f5c60fbc43cb00965ac641"
},
"groupBy":{
"$oid":"60f5c60fbc43cb00965ac641"
},
"employer":{
"$oid":"60d0e4001a6ccd764cb26df4"
},
"parameters":{
"begin":"2020-01-01",
"end":"2020-12-31"
},
"recordCount":74,
"errorFields":[
{
"field":"employeeStatus",
"recordCount":62,
"errorDetails":[
{
"recordIds":[
"10000986",
"10000990",
"10001020"
],
"detail":"Active employees should not have term date. Termed employees should have term date.",
"recordCount":3
},
{
"recordIds":[
"10001032"
],
"detail":"Stale pay period data (no new check for over 30 days from queried end date)",
"recordCount":1
}
]
},
{
"field":"ytdGrossWages.ytdTotalGrossWages",
"recordCount":8,
"errorDetails":[
{
"recordIds":[
"10001211",
"10001269",
"10001328",
"10001395"
],
"detail":"YTD total does not equal sum of YTD wage buckets",
"recordCount":4
}
]
}
],
"timestamp":{
"$date":"2021-07-19T18:35:59.031Z"
},
"__v":0
},
{
"_id":{
"$oid":"60f5c615bc43cb00965ac647"
},
"groupBy":{
"$oid":"60f5c60fbc43cb00965ac641"
},
"employer":{
"$oid":"60d0e4001a6ccd764cb26df4"
},
"parameters":{
"begin":"2020-01-01",
"end":"2020-12-31"
},
"recordCount":11,
"errorFields":[
{
"field":"employeeStatus",
"recordCount":11,
"errorDetails":[
{
"recordIds":[
"10003644",
"10003680"
],
"detail":"Active employees should not have term date. Termed employees should have term date.",
"recordCount":2
},
{
"recordIds":[
"10003667",
"10003694",
"10003807",
"10003789"
],
"detail":"Stale pay period data (no new check for over 30 days from queried end date)",
"recordCount":4
}
]
},
{
"field":"ssn",
"recordCount":2,
"errorDetails":[
{
"recordIds":[
"10003667"
],
"detail":"The ssn field is required.",
"recordCount":1
},
{
"recordIds":[
"10003694"
],
"detail":"The ssn must be 9 digits.",
"recordCount":1
}
]
},
{
"field":"employeeHomeAddressCountry",
"recordCount":1,
"errorDetails":[
{
"recordIds":[
"10003694"
],
"detail":"The employeeHomeAddressCountry field is required.",
"recordCount":1
}
]
}
],
"timestamp":{
"$date":"2021-07-19T18:36:05.135Z"
},
"__v":0
}
]
I want output like this:
{
"_id" : ObjectId("60f5c60fbc43cb00965ac641"),
"errorFields" : [
{
"field" : "employeeStatus",
"recordCount" : 73,
"errorDetails" : [
{
"recordIds" : [
"10001032",
"10003667",
"10003694",
"10003807",
"10003789"
],
"detail" : "Stale pay period data (no new check for over 30 days from queried end date)",
"recordCount" : 5
},
{
"recordIds" : [
"10000986",
"10000990",
"10001020",
"10001031",
"10001035"
],
"detail" : "Active employees should not have term date. Termed employees should have term date.",
"recordCount" : 5
}
]
},
{
"field" : "ytdGrossWages.ytdTotalGrossWages",
"recordCount" : 8,
"errorDetails" : [
{
"recordIds" : [
"10001211",
"10001269",
"10001328",
"10001395"
],
"detail" : "YTD total does not equal sum of YTD wage buckets",
"recordCount" : 8
}
]
},
{
"field" : "ssn",
"recordCount" : 2,
"errorDetails" : [
{
"recordIds" : [
"10003667"
],
"detail" : "The ssn field is required.",
"recordCount" : 1
},
{
"recordIds" : [
"10003694"
],
"detail" : "The ssn must be 9 digits.",
"recordCount" : 1
}
]
},
{
"field":"employeeHomeAddressCountry",
"recordCount":1,
"errorDetails":[
{
"recordIds":[
"10003694"
],
"detail":"The employeeHomeAddressCountry field is required.",
"recordCount":1
}
]
}
]
}
Here is the mycode method 1:
db.collection.aggregate([
{ $match: { groupBy: ObjectId("60f5c60fbc43cb00965ac641") } },
{ "$project": {
"_id": "$groupBy",
"errorFields": { "$map": {
"input": "$errorFields",
"as": "ef",
"in": {
"field": "$$ef.field",
"recordCount": {
$sum:"$$ef.recordCount"
},
"errorDetails": { "$map": {
"input": "$$ef.errorDetails",
"as": "ed",
"in": {
"detail": "$$ed.detail",
"recordIds": { "$map": {
"input": "$$ed.recordIds",
"as": "ri",
"in": {
$concat: [ "$$ri"]
}
}},
"recordCount": {
$size:"$$ed.recordIds"
}
}
}}
}
}}
}}
]).pretty()
Here is the mycode method 2:
db.collection.aggregate([
{ $match: { groupBy: ObjectId("60f5c60fbc43cb00965ac641") } },
{ $unwind: "$errorFields" },
{ $unwind: "$errorFields.errorDetails" },
{ $unwind: "$errorFields.errorDetails.recordIds" },
{ "$group": {
"_id": {
"_id": "$groupBy",
"errorFields": {
"field": "$errorFields.field",
"errorDetails": {
"detail": "$errorFields.errorDetails.detail"
}
}
},
"recordIds": {
"$push" : "$errorFields.errorDetails.recordIds",
},
"Idscount": { $sum: 1 }
}},
{ "$group": {
"_id": {
"_id": "$_id._id",
"errorFields": {
"field": "$_id.errorFields.field"
}
},
"errorDetails": {
"$push": {
"recordIds": "$recordIds",
"detail": "$_id.errorFields.errorDetails.detail",
"recordCount" : "$Idscount"
}
}
}},
{ "$group": {
"_id": 0,
"errorFields": {
"$push": {
"field": "$_id.errorFields.field",
"recordCount": "$fieldCount",
"errorDetails": "$errorDetails"
}
}
}}
]).pretty()
I am having the following collection:
{
"price" : [
55800000,
62800000
],
"surface" : [
81.05,
97.4
],
}
I would want to calculate the price/m2. I have tried the following but got the following error: "$divide only supports numeric types, not array and array".
db.entries.aggregate(
[
{ $project: { url: 1, pricePerSquareMeter: { $divide: [ "$price", "$surfaces" ] } } }
]
)
Would you know how to solve this? Ultimately would want to have an array like this:
{
"price" : [
55800000,
62800000
],
"surface" : [
81.05,
97.4
],
"pricePerSquareMeter" : [
688463.91,
644763.86
]
}
Important: The price and surface should also be ordered so that the calculation is valid.
You can use below aggregation
db.collection.aggregate([
{ "$addFields": {
"pricePerSquareMeter": {
"$map": {
"input": { "$range": [0, { "$size": "$price" }] },
"in": {
"$divide": [
{ "$arrayElemAt": ["$price", "$$this"] },
{ "$arrayElemAt": ["$surface", "$$this"] }
]
}
}
}
}}
])
MongoPlayground
You can use below aggregation
db.entries.aggregate([
{ $addFields: {
pricePerSquareMeter: {
$map: {
input: '$price',
as: 'item',
in: {
$divide: [
"$$item",
{ $arrayElemAt: [
"$surface",
{ "$indexOfArray": ["$price", "$$item"] }
]}
]
}
}
},
}}])
Upon the aggregate function, the output i have got is of the form :-
> [ { DescriptionArray:
[ 'Des1',
'Des2',
'Des3',
'Des4' ],
PicArray:
[ 'Pic1.jpeg',
'Pic2.jpeg',
'Pic3.jpeg',
'Pic4.jpeg' ] } ]
But, i want the output to be of the form :-
>[ { DescriptionArray:
[
['Des1'],
['Des2'],
['Des3'],
['Des4' ]
],PicArray:
[
['Pic1.jpeg'],
['Pic2.jpeg'],
['Pic3.jpeg'],
['Pic4.jpeg']
] } ]
The aggregate function of mine is as follows:-
>User.aggregate([
{
"$project": {
"_id": 0,
"DescriptionArray": {
"$reduce": {
"input": "$services.description",
"initialValue": [],
"in": {
"$concatArrays": [
"$$this",
"$$value"
]
}
}
},
"PicArray": {
"$reduce": {
"input": "$services.badpic",
"initialValue": [],
"in": {
"$concatArrays": [
"$$this",
"$$value"
]
}
}
}
}
}
])
The schema of my database is as follows:-
> var serviceSchema = new mongoose.Schema({
description:[String],
pic:[String]
})
Is there any way, i can get the output of my desired format?
I am new in groovy and I want to construct a json object with the builder
{
"query": {
"bool": {
"must": [
{
"bool": {
"should": [
{ "match": { "content": "scontent" } },
{ "match": { "title":"stitle" } }
]
}
},
{
"bool": {
"should": [
{ "match": { "a1": "v1" } },
{ "match": { "a2":"v2" } },
... and so on ...
{ "match": { "an":"vn" } }
]
}
}
]
}
},
"highlight": {
"fields": {
"content":{}
}
}
}
I search a lot of on other posts on stackoverflow and I write this code
So I did this but no way to get what I want :
JsonBuilder builder = new JsonBuilder()
def body = builder {
from Lib.or(qQuery.start, 0)
size Lib.or(qQuery.num, 10)
query {
bool {
must [
{
bool {
should [
{ match { content 'scontent' } },
{ match { title 'stitle' } }
]
}
},
{
bool {
should myVals.collect {[
'match' : { it.key it.value }
]}
}
}
]
}
}
highlight {
fields {
content {}
}
}
}
Thanks for any help !
I think you can make this work with the JsonBuilder as is, but it is usually easier to create the data structure using maps and lists (which is what the builder outputs) in groovy as you have more control there.
Example code:
import groovy.json.*
def data = [
query: [
bool: [
must: [
[bool:
[should: [
[match: [ content: 'scontent']],
[match: [ title: 'stitle']]
]]
],
[bool:
[should: [
[match: [ a1: 'v1']],
[match: [ a2: 'v2']],
[match: [ vn: 'vn']]
]]
]
]
]
]
]
println JsonOutput.prettyPrint(JsonOutput.toJson(data))
produces:
{
"query": {
"bool": {
"must": [
{
"bool": {
"should": [
{
"match": {
"content": "scontent"
}
},
{
"match": {
"title": "stitle"
}
}
]
}
},
{
"bool": {
"should": [
{
"match": {
"a1": "v1"
}
},
{
"match": {
"a2": "v2"
}
},
{
"match": {
"vn": "vn"
}
}
]
}
}
]
}
}
}
I did not include your full json as it takes up some space, but the structure is there. Note the use of lists ([valueA, valueB]) vs maps ([someKey: someValue]) in the data structure.
Granted this makes the JsonBuilder less than 100% useful but I haven't seen any concise ways of including lists of large json objects in a list within the structure. You can do:
def json = JsonBuilder()
json.query {
bool('list', 'of', 'values')
}
but for larger structures as list elements I would say go with the lists and maps approach.