Related
I have a ruby script that works pretty well. I'm parsing JSON from an API and passing it into and array.
Most of the top elements parse fine. But I have a nested object that looks like this after its a hash...
I did and inspect on it and outputted the current data object I'm looking at.
orf1: ["displayValue", "DNAORF999-N9, DNAORF888-N9, DNAORF777-N9, DNAORF444-N9"]
orf1_inspect: ["displayValue", "DNAORF999-N9, DNAORF888-N9, DNAORF777-N9, DNAORF444-N9"]
orf1: ["isMulti", true]
orf1_inspect: ["isMulti", true]
orf1: ["textValue", "DNAORF999-N9, DNAORF888-N9, DNAORF777-N9, DNAORF444-N9"]
orf1_inspect: ["textValue", "DNAORF999-N9, DNAORF888-N9, DNAORF777-N9, DNAORF444-N9"]
orf1: ["type", "entity_link"]
orf1_inspect: ["type", "entity_link"]
orf1: ["value", ["seq_dfdfdfdfd", "seq_fdfdfd", "seq_fdfdfdd", "seq_jfdfdfd"]]
Here is the section of code that returns the above..Let me know if you need more info?
if row["fields"]["ORF"].nil? || row["fields"]["ORF"].empty?
orf = nil
else
row["fields"]["ORF"].each do |orf1|
puts 'orf1: ' + orf1.to_s
orfinspect = orf1.inspect
puts 'orf1_inspect: ' + orfinspect
end
end
I cant seem to parse oput the individual values.
I need to get the values/data from these fields... displayValue, isMulti, textValue, type, and value
Ive tried all kinds of approaches.. Some give conversion errors. I can use orf1.first and that works... but its only part of it...
Even this will get me the header for textvalue
orf = orf1[0]["textValue"]
puts 'orf: ' + orf.to_s
here is the inspect on row["fields"]["ORF"].inspect if it helps..
{
"displayValue" => "DNAORF888-N9, DNAORF999-N9, DNAORF444-N9, DNAORF321-N9, DNAORF111-N9, DNAORF777-N9, DNAORF222-N9, DNAORF425-N9, DNAORF122-N9",
"isMulti" => true,
"textValue" => "DNAORF888-N9, DNAORF999-N9, DNAORF444-N9, DNAORF321-N9, DNAORF111-N9, DNAORF777-N9, DNAORF222-N9, DNAORF425-N9, DNAORF122-N9", "type"=>"entity_link",
"value" => ["seq_jddddaA2", "seq_sfgsfff", "seq_osfsffs", "seq_fsdfsd", "seq_fsdfsd", "seq_fsfsfsfs", "seq_sfsfss", "seq_sfsfsf", "seq_sfsfs"]
}
This seems to work. Is this the best approach?
row["fields"]["ORF"].each do |key,value|
if key == 'displayValue'
unless value.nil?
orf_displayValue=value
end
end
if key == 'isMulti'
unless value.nil?
orf_isMulti=value
end
end
if key == 'textValue'
unless value.nil?
orf_textValue=value
end
end
if key == 'type'
unless value.nil?
orf_type=value
end
end
if key == 'value'
unless value.nil?
orf_value=value
end
end
end
end
But I would need to do this for each field. I feel like there is a better way..
As the error says:
can't convert Symbol into Integer
textValue is of type Symbol and when you are looping over data you end with first key and value which is displayValue and DNAORF004-N9 and inside do end block you are accessing DNAORF004-N9 index with textValue which is Symbol which is not possible it should be integer as the error states.
data = {"displayValue"=>"DNAORF004-N9", "isMulti"=>true, "textValue"=>"DNAORF001-N9", "type"=>"entity_link", "value"=>["seq_fdfdf", "seq_9fdfdfdfd"]}
datasequences = [ :displayValue, :textValue ]
datasequences.each do |textValue|
puts "textValue is #{textValue.inspect}"
data.each do |key, value|
puts "\t#{value[textValue]} at #{key}"
end
end
To resolve the issue you can change the code as below:
data = {"displayValue"=>"DNAORF004-N9", "isMulti"=>true, "textValue"=>"DNAORF001-N9", "type"=>"entity_link", "value"=>["seq_fdfdf", "seq_9fdfdfdfd"]}
datasequences = [ :displayValue, :textValue ]
# changing all keys from string to symbol
data = data.transform_keys(&:to_sym)
datasequences.each do |textValue|
puts "textValue is #{textValue.inspect}"
puts "Value: #{data[textValue]}"
end
value = {
"dnaSequences": [
{
"aliases": [],
"annotations": [],
"apiURL": "https://url",
"archiveRecord": nil,
"authors": [
{
"handle": "dsdsd",
"id": "ent_dsdsd",
"name": "dsdsd"
}
],
"bases": "",
"createdAt": "2020-07-14T21:39:26.991794+00:00",
"creator": {
"handle": "dsds",
"id": "ent_dsdsd",
"name": "dsdd Fdsdsdso"
},
"customFields": {},
"dnaAlignmentIds": [],
"entityRegistryId": "MOUSE006",
"fields": {
"Identical mouses": {
"displayValue": nil,
"isMulti": false,
"textValue": nil,
"type": "part_link",
"value": nil
},
"Library Constructed By": {
"displayValue": "dsdsd",
"isMulti": false,
"textValue": "dsdsd",
"type": "dropdown",
"value": "sfso_dsdsd"
},
"Library Construction Date": {
"displayValue": "2020-06-01",
"isMulti": false,
"textValue": "2020-06-01",
"type": "date",
"value": "2020-06-01"
},
"Library Description": {
"displayValue": "dsdsdds",
"isMulti": false,
"textValue": "dsdsdsd",
"type": "text",
"value": "dsdsdsdsd"
},
"Library Sample Source": {
"displayValue": "dsdsds",
"isMulti": false,
"textValue": "dsdsdsds",
"type": "dropdown",
"value": "sfso_dsdsdsd"
},
"ORF": {
"displayValue": "DNAORF004-N9, DNAORF005-N9, DNAORF008-N9, DNAORF001-N9",
"isMulti": true,
"textValue": "DNAORF004-N9, DNAORF005-N9, DNAORF008-N9, DNAORF001-N9",
"type": "entity_link",
"value": [
"seq_aaaaaa",
"seq_bbbbbb",
"seq_ccccc",
"seq_ddddd"
]
},
"Re-Run ORF?": {
"displayValue": nil,
"isMulti": false,
"textValue": nil,
"type": "dropdown",
"value": nil
},
"Sampling GPS Coordinates": {
"displayValue": nil,
"isMulti": false,
"textValue": nil,
"type": "text",
"value": nil
},
"Sequencing Approach": {
"displayValue": "Single Sequence",
"isMulti": false,
"textValue": "Single Sequence",
"type": "dropdown",
"value": "gfgf"
},
"Sequencing Method": {
"displayValue": "gfgf fgfgfg",
"isMulti": false,
"textValue": "gfgf gfgfg",
"type": "dropdown",
"value": "sfsogfgfg_irlx6NfZ"
}
},
"folderId": "gfgfg",
"id": "gfgfgf",
"isCircular": false,
"length": 25129,
"modifiedAt": "2022-04-05T17:06:25.491926+00:00",
"name": "COPE03-P19",
"primers": [],
"registrationOrigin": {
"originEntryId": nil,
"registeredAt": "2020-07-14T22:15:09.541243+00:00"
},
"registryId": "gfgfgfg",
"schema": {
"id": "ps_fdfdfd",
"name": "mouse"
},
"translations": [],
"url": "hyyps:///COPE/f//edit",
"webURL": "https://url"
}
]
}
datasequences = [ :displayValue, :isMulti, :textValue, :type, :value ]
result = value["dnaSequences".to_sym].map do |v|
row = {}
if v.key?(:fields) && v[:fields].key?(:ORF)
datasequences.map do |key|
row[key] = v[:fields][:ORF][key.to_sym]
end
end
row
end
puts result
Note: Tested with Ruby 3.0.0
I am working in fauna DB. I have a confusion for writing an query to update a field . From the given array object I want to update the Boolean fields ( "presentation", "keyTakeaways", "whitepaper", "downloadAll") with the reference to the SegmentAnonymousID. I am adding the code that I tried. Can someone help me to sort out the problem.
[{
"ref": Ref(Collection("Downloads"), "322568726157197900"),
"ts": 1643884359510000,
"data": {
"SegmentAnonymousID": "57f22fc0-2ace-4522-b0a6-4d0cd45cfc3a",
"response": {
"firstname": "test ",
"lastname": "test 1",
"email": "test#gmail.com",
"company": "test",
"presentation": false,
"keyTakeaways": false,
"whitepaper": false,
"downloadAll": false
}
}
},
{
"ref": Ref(Collection("Downloads"), "322484599970071113"),
"ts": 1643870069845000,
"data": {
"SegmentAnonymousID": "15ba9e0d-e646-4d31-beaa-b2a4d3eac56f",
"response": {
"firstname": "test 4",
"lastname": "test4",
"email": "test#gmail.com",
"company": "test",
"presentation": false,
"keyTakeaways": false,
"whitepaper": false,
"downloadAll": false
}
}
}]
The code I tried
const result = await faunaClient.query(
q.Update(
q.Select("ref", q.Get(q.Match(q.Index("SegmentAnonymousID"), "57f22fc0-2ace-4522-b0a6-4d0cd45cfc3a")), { data: { response: { "presentation": true } } })
)
)
Your query has brackets in the wrong place. As written, the object containing the new data appears as the "default" value for the Select call. You'll likely have better success with this update (reformatted to show function calls and parameters better):
const result = await faunaClient.query(
q.Update(
q.Select(
"ref",
q.Get(
q.Match(
q.Index("SegmentAnonymousID"),
"57f22fc0-2ace-4522-b0a6-4d0cd45cfc3a"
)
)
),
{
data: {
response: {
presentation: true
}
}
}
)
)
i have a model of how this:
_id:ObjectId("5705005b240166e927f841cb")
figures: [
{
id: "5433234"
figure: [
{
"id":"figure_543",
"active": false,
"name": "Alvar"
},
{
"id":"figure_431",
"active": false,
"name": "Peter"
}
]
},
{
id: "1276544"
figure: [
{
"id":"figure_123",
"active": false,
"name": "Jhon"
}
]
},
{
id: "1231645"
figure: [
{
"id":"figure_154",
"active": false,
"name": "Luka"
}
]
}
]
I'am doing this:
db.collection.update({"_id": userId, "figures.figure.id": figureId}, {$set:{"figures.figure.$.active": true}})
userId and figureId are variables that come from req.body on expressjs.
But i see this:
The positional $ operator cannot be used for queries which traverse more than one array, such as queries that traverse arrays nested within other arrays, because the replacement for the $ placeholder is a single value
My question is, how can i update the field active of figure on nested array with mongoose?
Thank's!!!
I am using one complex JSON structure as a result of one Ajax call. Here I need "many different kinds of nested models in one parent model".For each person, I am using one EmployeeContext. I need this as my parent model.
Within this EmployeeContext, I need many different collection having associated model. Example:I have a collection named expenseContextCollection with "expense" as the model of the same.And I have travellContextCollection with travel as the model.
I need to display details of each model and need to update the same and save back to mongo database.
Currently I am using one file for model (EmployeeModel.js),one file for view (EmployeeView.js), one file for template (EmployeePage.html).
And in the EmployeeView.js file I am setting the result of ajax call to model.
initialize: function(instance) {
this.model.set({
"travelContext": ajaxResponse.EmployeeContext.travellContextCollection
});
this.model.set({
"expenseContext": ajaxResponse.EmployeeContext.expenseContextCollection
});
this.render();
}
But I need this travelContext as a backbone collection so that I can loop though it and take each travel model from it
How Can I handle this situation with model view collection approach in backbone.js ?
following is one sample of JSON structure:
{
"EmployeeContext": {
"expenseContextCollection": [{
"currencyType": "INR",
"empID": "00123456",
"imageID": "d69ce74a9b4e075d2111cf0619e27c503d",
"toDate": "11-12-2015",
"billDate": "11-12-2015"
}, {
"currencyType": "INR",
"empID": "00123456",
"imageID": "ab2f78d9f9e7897b4a11c5bc82618d09f4",
"toDate": "25-01-2016",
"billDate": "20-01-2016",
}],
"claimContextCollection": [],
"travellContextCollection": [{
"empID": "00123456",
"isOneWay": true,
"eligibility": "true",
"createTravelRequest": {
"purposeOfTravelDetailsCollection": [{
"isPrimary": "true",
"purposeOfTravel": "Trans",
"leadOpportunity": "BAA12346",
"account": "BASS"
}],
"travelDetailsCollection": [{
"travelToCity": "BANGALORE",
"travelType": "DTR",
"travelTime": "",
"travelFromCity": "CHENNAI",
"checkoutDate": "25-01-2016",
"travelDate": "20-01-2016",
"travelTo": "INDIA",
"travelFrom": "INDIA"
}],
"Preference": {
"empnum": "00123456",
"employeeProfilePreference": {
"emergencyContacCity": "9412345678",
"travelSeatPreference": "aisle",
"smsNotification": "true",
"emergencyContactNumber": "9412345678",
"frequentFlierPreference": {
"frequentFlierNo": "EJK7861",
"frequentFlierAirlines": "Virgin"
},
"employeeContactNumber": "8712345678",
"emergencyContactPerson": "Moll Mathew",
"emergencyContactAddress": "KOOODC",
"travelMealPreference": "vegetarian"
}
},
"otherDetails": {
"smsNotification": true,
"forexAmount": "0",
"forexRequiredDate": "20141002",
"employeeContactNumber": "00123456",
"billable": "true"
},
"emergencyContactDetails": {
"emergencyContacCity": "KOOODC",
"emergencyContactNumber": "9412345678",
"emergencyContactPerson": "Moll Mathew",
"emergencyContactAddress": "KOOODC"
}
},
"billSubmissionMode": "manual",
"travelClass": "Economy",
"access_token": "",
"travelEndDate": "25-01-2016",
"timeStamp": "2015-12-11 16:00:47.395",
"travelType": "DTR",
"travelID": "3000553702",
"travelStartDate": "20-01-2016",
"approvalGIMS": "approvalGIMS",
"expenseCodes": {
"ExpenseTypeCollection": [{
"travelType": "DTR",
"client": "200",
"expenseDescription": "LODGING",
"glAccount": "0000834110",
"lastChangedBy": "",
"expenseCode": "DCN",
"changedOn": "0000-00-00",
"expenseStatus": "ACTIVE"
}, {
"travelType": "DTR",
"client": "200",
"expenseDescription": "BUSINESS",
"glAccount": "0000839301",
"lastChangedBy": "",
"expenseCode": "DMT",
"changedOn": "0000-00-00",
"expenseStatus": "ACTIVE"
}],
"ErrorCodeCollection": [{
"ErrorText": "S",
"ErrorType": "S"
}]
},
"empEmailID": "jaiseephen#gmail.com",
"entry_type": "new_entry",
"approvalBFM": "approverBFM",
"status": "Pending for Expense"
}],
"location_contextCollection": [{
"Status": "success"
}],
"user_context": {
"timeStamp": "2015-12-11 16:00:47.754",
"access_token": "",
"empID": "00123456",
"buDetailsCollection": [{
"buHeadADID": "",
"buHeadName": "",
"buHeadEmail": ""
}],
"empTechManager": {
"techMgrEmpID": "",
"techMgrADID": "",
"techMgrName": "",
"techMgrEmail": ""
},
"empPassportDetails": {
"endDate": "",
"dateOfBirthAsOfPassport": "0000-00-00",
},
"empDetails": {
"secondSupervisorEmpNumber": "00000000",
"empDOB": "15.05.1999",
},
"empEmailID": "jaiseephen#gmail.com",
"supDetailsCollection": [{
"ADID": "FAMM",
"supADID": "FAMM",
"supEmail": "fazee_ammed#gmail.com",
"lastName": "Faz Mammed",
"supCostCenter": "",
"Email": "fazee_ammed#gmail.com",
"CostCenter": "",
"empNumber": "00678444",
"supEmpID": "00678444",
"supName": "Faz Mammed",
"Name": "Faz Mammed"
}],
"altSupDetailsCollection": [{
"supADID": "FAMM",
"supEmail": "fazee_ammed#gmail.com",
"supEmpID": "00678444",
"supName": "Faz Mammed"
}]
}
}
}
I believe the simplest solution would be parse JSON.parse(text). Then, you can use that object in your Backbone model like this:
var obj = JSON.parse(income);
obj.EmployeeContext.travellContextCollection[0]
Also for finding a target value you can create function for searching target node.
All the question is in my title =).
I started with checkBox Tree exemple, add property checked:false for the first level that work i see my checkbox just on left icone folder.
But have radio in level that is so different, i tryed some change to transform checkbox in radio, but nothing *:_:*
Some topic given by broogle talk only some levels with checkbox but none with radio.
Have you an idea ?
Thx a lot, have a nice day.
The Example is using a JSON file to load the nodes.
In de JSON file you can set "checked" to true or false.
{"text": "Grocery List",
"cls": "folder",
"children": [{
"text": "Bananas",
"leaf": true,
"checked": false
},{
"text": "Milk",
"leaf": true,
"checked": false
},{
"text": "Cereal",
"leaf": true,
"checked": false
},{
"text": "Energy foods",
"cls": "folder",
"children": [{
"text": "Coffee",
"leaf": true,
"checked": false
},{
"text": "Red Bull",
"leaf": true,
"checked": false
}]
}]}
You can only have a checkbox in the tree but you can always write your own extension :)
Well, i'm brb on this thread for give a little function. put that on your tree and normaly you have checkbox with radio behavior.
listeners: {
'checkchange': function( n, check )
{
if( check )
{
var p = n.parentNode;
p.eachChild( function( c )
{
if( c.isLeaf() && c.get('checked') )
{
c.set('checked', false);
}
});
n.set('checked', true);
}
else
{
n.set('checked', false);
}
}
}