I am trying to display a force directed tree. There are 3 models
Company / Division / Department
Each Company has many divisions. Each division belongs to one Company.
Each Division has many departments. Each department belongs to one Division.
amcharts expects a syntax like:
[{
"name": "First",
"children": [{
"name": "A1", "value": 100
}, {
"name": "A2", "value": 60
}, {
"name": "A3", "value": 30
}]
}, {
"name": "Second",
"children": [{
"name": "B1", "value": 135
}, {
"name": "B2", "value": 98
}, {
"name": "B3", "value": 56
}]
}, {
"name": "Third",
"children": [{
"name": "C1", "value": 335
}, {
"name": "C2", "value": 148
}, {
"name": "C3", "value": 126
}, {
"name": "C4", "value": 26
}]
}, {
"name": "Fourth",
"children": [{
"name": "D1", "value": 415
}, {
"name": "D2", "value": 148
}, {
"name": "D3", "value": 89
}, {
"name": "D4", "value": 64
}, {
"name": "D5", "value": 16
}]
}, {
"name": "Fifth",
"children": [{
"name": "E1", "value": 687
}, {
"name": "E2", "value": 148
}]
}]
I am only able to get the first level here:
foreach ($companies as $company) {
$data[] =
(object) [
'name' => $company->name,
'value' => $company->value,
];
}
I am sure that I need to further iterate through $company->devisions and so on but not sure how to build the final construct amcharts is expecting. Any hints?
You can try something in the lines of:
$data = [];
foreach ($companies as $company) {
$childrenArray = [
'name' => $company->name,
];
foreach ($company->divisions as $division) {
$childArray['children'][] = [
'name' => $division->name,
'value' => $division->value,
];
}
$data[] = $childrenArray;
}
return json_encode($data);
Related
I have following code:
%dw 2.0 output application/json var attributeIdMapping =
${vault::attributeIdMapping}
--- {
"objectTypeId": 3,
"attributes": vars.requestBody mapObject (value, key) ->
{
"objectTypeAttributeId": (attributeIdMapping.attributes filter ($.name == key))[0].id,
"objectAttributeValues": [{
"value": value,
"key": key
}]
} }
attributeIdMapping:
{
"attributes": [
{
"name": "accountType",
"id": "87"
},
{
"name": "accountClass",
"id": "89"
},
{
"name": "accountName",
"id": "85"
},
{
"name": "displayName",
"id": "18"
},
{
"name": "accountCategory",
"id": "88"
},
{
"name": "accountNumber",
"id": "84"
},
{
"name": "description",
"id": "86"
},
{
"name": "accountGroup",
"id": "90"
}
]
}
vars.requestBody:
{
"displayName": "TestMulesoft2",
"description": "Test"
}
But my filter shows null in the end. As I understand key is not passed to level below map itself. How can I get this working?
I put all the input data inside the script to simplify reproduction. The problem seems to be using the operator == to compare a string and a key types returns empty. Using the operator ~= which attempts to coerce appears to return the expected result:
%dw 2.0
output application/json
var attributeIdMapping =
{
"attributes": [
{
"name": "accountType",
"id": "87"
},
{
"name": "accountClass",
"id": "89"
},
{
"name": "accountName",
"id": "85"
},
{
"name": "displayName",
"id": "18"
},
{
"name": "accountCategory",
"id": "88"
},
{
"name": "accountNumber",
"id": "84"
},
{
"name": "description",
"id": "86"
},
{
"name": "accountGroup",
"id": "90"
}
]
}
var requestBody = {
"displayName": "TestMulesoft2",
"description": "Test"
}
--- {
"objectTypeId": 3,
"attributes": requestBody mapObject (value, key) ->
{
"objectTypeAttributeId": (attributeIdMapping.attributes filter ($.name ~= key))[0].id,
"objectAttributeValues": [{
"value": value,
"key": key
}]
} }
Output:
{
"objectTypeId": 3,
"attributes": {
"objectTypeAttributeId": "18",
"objectAttributeValues": [
{
"value": "TestMulesoft2",
"key": "displayName"
}
],
"objectTypeAttributeId": "86",
"objectAttributeValues": [
{
"value": "Test",
"key": "description"
}
]
}
}
How I can access to a depth key value from a nested hash?
I want to extract values inside of _Data, I want to extract the field and size inside data[], to be printed like fieldvalue_sizevalue
elemento3_3
I have this code but only get the all the data keys and values and i had some troubles making this iteration, I am new at ruby
_Data[0][:checks].each do |intera|
puts "#{itera[:data]}
end
this is the nested array
_Data =[
{
"agent": "",
"bottom_comments": [],
"checks": [
{
"title": "new",
"data": [{
"field": "elemento1",
"value": "0",
}
]
},
{
"title": "new",
"data": [{
"field": "elemento2",
"value": "0",
"size":"4",
}
]
},
{
"title": "new",
"data": [{
"field": "elemento3",
"value": "0",
"size":"3",
}
]
},
{
"title": "new",
"data": [{
"field": "elemento4",
"value": "0",
"size":"17",
}
]
},
{
"title": "new",
"data": [{
"field": "elemento5",
"value": "0",
"size":"12",
}
]
},
{
"title": "new",
"data": [{
"field": "elemento6",
"value": "0",
"size":"19",
}
]
},
{
"title": "new",
"data": [{
"field": "elemento7",
"value": "0",
"size":"9",
}
]
},
{
"title": "new",
"data": [{
"field": "elemento8",
"value": "0",
"size":"10",
}
]
},
{
"title": "new",
"data": [{
"field": "elemento11",
"value": "0",
"size":"14",
}
]
},
{
"title": "new",
"data": [{
"field": "elemento19",
"value": "0",
"size":"14",
}
]
},
{
"type": "condiciones",
"elementos": [
{
"table": [
{
"title": "",
"name": "radio",
},
{
"title": "",
"name": "xenon",
},
{
"title": "",
"name": "aluminio",
},
{
"title": "",
"name": "boro",
},
{
"title": "",
"name": "oro",
},
{
"title": "",
"name": "bromo",
},
{
"title": "",
"name": "oxigeno",
}
]
}
]
}
]
}
]
_Data[0][:checks].each do |intera|
intera[:data].each do |data|
puts "#{data[:filed]}_#{data[:value]}"
end
end
Hope this may help you.
Here is one line code to print the values if they exist. Let me know if any further details on any of the methods used will be helpful.
_Data[0][:checks].pluck(:data).flatten.compact.each{|i| puts "#{i[:field]}_#{i[:size]}" if i[:field] && i[:size]}
sample data
[
{
"createdDate": 1508588333821,
"data": {
"image_extension": "png",
"name": "Golden",
"qty": 1,
"remark": "#296-2",
"status": "RETURN",
"owner": [
{
"name": "app1emaker",
"location": 1
},
{
"name": "simss92_lmao",
"location": 31
}
]
},
"deleted": false,
"docId": 307,
"docType": "product",
"id": "db0131f9-9359-4aa3-b6ed-cd9f3ff4aa3e",
"updatedDate": 1553155281691
},
{
"createdDate": 1508588333324,
"data": {
"image_extension": "png",
"name": "Golden",
"qty": 1,
"remark": "#296-2",
"status": "DISCARD",
"owner": [
{
"name": "At533",
"location": 7
},
{
"name": "madsimon",
"location": 64
},
{
"name": "boyboy96",
"location": 1
},
{
"name": "xinfengCN",
"location": 5
}
]
},
"deleted": false,
"docId": 308,
"docType": "product",
"id": "3790bdaa-5347-4ab0-8149-37332c23c6ea",
"updatedDate": 1554555231691
},
...
...
]
And said that, I would like to select the data.owner on array index 0 only (or I should say data.owner[0]), which are
{
"name": "app1emaker",
"location": 1
}
and
{
"name": "At533",
"location": 7
}
in this case. I have a failed code below.
r.db('carbon').table("items").pluck(['id', 'docId', 'createdDate',{data:{name: true, owner:[0]}}])
I saw that for some functions like orderBy, rethinkdb allowed to use orderBy(r.row('data')('owner')(0)('name')) for access nested object, but I have no idea how to do this for pluck? could anyone give me some hints?
Thanks a lot
pluck can not do that, but you can fall back to use the map:
r.db("carbon").table("items").map(function(doc){
return {
"id": doc("id"),
"docId": doc("docId"),
"createdDate": doc("createdDate"),
"data": {
"name": doc("data")("name"),
"owner": doc("data")("owner")(0)
}
}
})
I want to turn the fetching of data from JSON to a hardcoded variable. This is the link to the d3 treemap chart: Zoomable Treemap Template.
If you mean you want to put JSON data into a variable, then you would represent this:
[
{
"key": "Asia",
"values": [
{
"key": "India",
"value": 1236670000
},
{
"key": "China",
"value": 1361170000
}]
},
{
"key": "Africa",
"values": [
{
"key": "Nigeria",
"value": 173615000
},
{
"key": "Egypt",
"value": 83661000
}]
}
]
like this:
var x = [
{
"key": "Asia",
"values": [
{
"key": "India",
"value": 1236670000
},
{
"key": "China",
"value": 1361170000
}]
},
{
"key": "Africa",
"values": [
{
"key": "Nigeria",
"value": 173615000
},
{
"key": "Egypt",
"value": 83661000
}]
}
];
I have a JSON data which in the following format:
[{
"id": 1,
"children": [{
"id": 7,
"children": [{
"id": 8,
"children": [{
"id": 4
}, {
"id": 5
}, {
"id": 11
}
]
}, {
"id": 9
}
]
}, {
"id": 6
}, {
"id": 10
}
]
}, {
"id": 2,
"children": [{
"id": 3
}, {
"id": 12
}
]
}, {
"id": 13
}
]
The tree for this JSON data is:
I want to loop through all the nodes and extract the "id" of all data maintaining its parent child hierarchy. How can i do this using recursive function.
The idea is to parse the json structure in ruby.
I just coded a node js code, you can still make it work on a browser in js, but you will need to download the underscore library here.
_und = require('underscore');
data = [{
"id": 1,
"children": [{
"id": 7,
"children": [{
"id": 8,
"children": [{
"id": 4
}, {
"id": 5
}, {
"id": 11
}
]
}, {
"id": 9
}
]
}, {
"id": 6
}, {
"id": 10
}
]
}, {
"id": 2,
"children": [{
"id": 3
}, {
"id": 12
}
]
}, {
"id": 13
}
]
function parse_tree_2(n) {
return(_und.map(n, parse_tree));
}
function parse_tree(n) {
if (n['children']) {
return({id: n['id'], children: parse_tree_2(n['children'])});
} else {
return({id: n['id']});
}
}
result = _und.map(data, parse_tree);
console.log("Result: %j", result);
You can put that into a file, and execute it with node (by download underscore with nmp install node).
On plain js it would be something like:
<script type="text/javascript" src="http://underscorejs.org/underscore-min.js"></script>
<script type="text/javascript">
data = [{
"id": 1,
"children": [{
"id": 7,
"children": [{
"id": 8,
"children": [{
"id": 4
}, {
"id": 5
}, {
"id": 11
}
]
}, {
"id": 9
}
]
}, {
"id": 6
}, {
"id": 10
}
]
}, {
"id": 2,
"children": [{
"id": 3
}, {
"id": 12
}
]
}, {
"id": 13
}
]
function parse_tree_2(n) {
return(_.map(n, parse_tree));
}
function parse_tree(n) {
if (n['children']) {
return({id: n['id'], children: parse_tree_2(n['children'])});
} else {
return({id: n['id']});
}
}
result = _.map(data, parse_tree);
console.log("Result: %j", result);
</script>
Ruby code:
require 'json'
data = <<EOF
[{
"id": 1,
"children": [{
"id": 7,
"children": [{
"id": 8,
"children": [{
"id": 4
}, {
"id": 5
}, {
"id": 11
}
]
}, {
"id": 9
}
]
}, {
"id": 6
}, {
"id": 10
}
]
}, {
"id": 2,
"children": [{
"id": 3
}, {
"id": 12
}
]
}, {
"id": 13
}
]
EOF
json = JSON.parse(data)
def parse_tree(n)
if n["children"]
{id: n["id"], children: n['children'].map {|c| parse_tree(c)} }
else
{id: n["id"]}
end
end
result = json.map {|n| parse_tree(n) }
puts result