Accessing nested JSON structure in D3 - json

I'm trying to create a set of graphs using D3 and I'm having trouble figuring out how to access nested data structures in my JSON. The data looks something like this (truncated):
{ "date": "20120927",
"hours": [
{ "hour": 0, "hits": 823896 },
{ "hour": 1, "hits": 654335 },
{ "hour": 2, "hits": 548812 },
{ "hour": 3, "hits": 512863 },
{ "hour": 4, "hits": 500639 }
],
"totalHits": "32,870,234",
"maxHits": "2,119,767",
"maxHour": 12,
"minHits": "553,821",
"minHour": 3 }
{ "date": "20120928",
"hours": [
{ "hour": 0, "hits": 1235923 },
{ "hour": 1, "hits": 654335 },
{ "hour": 2, "hits": 1103849 },
{ "hour": 3, "hits": 512863 },
{ "hour": 4, "hits": 488506 }
],
"totalHits": "32,870,234",
"maxHits": "2,119,767",
"maxHour": 12,
"minHits": "553,821",
"minHour": 3 }
What I eventually want to do is create multiple radar graphs, one for each day, plotting the hits for each hour. But I'm having trouble even just getting inside the "hours" array. I can , for example, get a list of all the dates, like so:
d3.select("body")
.append("ul")
.selectAll("li")
.data(data)
.enter()
.append("li")
.text(function (d,i) {
return d.date;
});
But I can't get at anything more nested. Can someone help point me in the right direction?

Assuming data is an two-element array containing your two objects, you could do something like this:
d3.select("body").append("ul").selectAll("li")
.data(data) // top level data-join
.enter().append("li")
.each(function() {
var li = d3.select(this);
li.append("p")
.text(function(d) { return d.date; });
li.append("ul").selectAll("li")
.data(function(d) { return d.hours; }) // second level data-join
.enter().append("li")
.text(function(d) { return d.hour + ": " + d.hits; });
});
This would give you a nested structure like this:
<ul>
<li>
<p>20120927</p>
<ul>
<li>0: 823896</li>
<li>1: 654335</li>
<li>2: 548812</li>
<li>3: 512863</li>
<li>4: 500639</li>
</ul>
</li>
<li>
<p>20120928</p>
<ul>
<li>0: 1235923</li>
<li>1: 654335</li>
<li>2: 1103849</li>
<li>3: 512863</li>
<li>4: 488506</li>
</ul>
</li>
</ul>
See also Mike's Nested Selections article.

Related

delete object from nested document in mongodb

I have this JSON above with _id, date, and two objects: sessionData and metaData. I want to delete from every document the object metaData where "userId":123456
{
"_id": {
"$oid": "60feedd4b3aefff2629b93b7"
},
"insertionDate": {
"$date": "2021-07-26T18:15:57.564Z"
},
"sessionData": {
"time": [1364, 1374, 1384],
"yaw": [0.15, 0.3, 0.45],
"pitch": [0.36, 0.76, 1.08],
"roll": [-0.13, -0.25, -0.35],
"heading": [-3.24, -3.25, -3.17],
"ax": [-0.42, -0.41, -0.41],
"ay": [-0.15, -0.13, -0.1],
"az": [0.9, 0.91, 1],
"gx": [0, 0, 0],
"gy": [-0.01, 0, -0.01],
"gz": [0.02, 0.02, 0.02],
"mx": [0.26, 0.26, 0.26],
"my": [0.01, 0.01, 0.01],
"mz": [-0.04, -0.04, -0.07]
},
"metaData": {
"userId": 123456,
"gender": "M",
"ageGroup": "SENIOR",
"weightKg": 70,
"heightCm": 175,
"poolSizeM": 50
}
}
The code I have:
#app.route('/data/anonymousData/<int:l>', methods = ['POST'])
def makeanonymous(l):
result = collection.update_many({}, {"$pull":{ "metaData": {"$in": {"userId": l }}}}, multi=True )
t=result.deleted_count
return f'Deleted {t} documents.'
You can use $unset to remove the object like this:
collection.update_many({
"metaData.userId": 123456
},
{
"$unset": {
"metaData": ""
}
})
Check this example.

How to make dynamic Sidebar from Json api?

I am trying to create a React Dynamic Sidebar from the Json API.
I need a coding assist for double iteration in React by using below three conditions:
if json element id === 0 ===> outer 1st level Mennuitem
if json element[i] id ===[j] parent_id ===> 2nd level submenu item
if parent_id[i]===[j] id && [j]parent_id===0 ===>3rd level submenuitem
But I can achieve outer MenuItem. But I fail to create SubMenuItem.
Please help me to fix this issue. Please see the attached image. Thanks
class App extends Component {
constructor(props) {
super(props);
this.state = {
mz:[
{
"id": 1,
"name": "Menu1",
"parent_id": 0
},
{
"id": 9,
"name": "SubMenu1-Menu1",
"parent_id": 1
},
{
"id": 26,
"name": "8989",
"parent_id": 9
},
{
"id": 10,
"name": "SubMenu2-Menu1",
"parent_id": 1
},
{
"id": 2,
"name": "Menu2",
"parent_id": 0
},
{
"id": 11,
"name": "SubMenu1-Menu2",
"url": "",
"order": 210,
"type": 3,
"is_active": true,
"parent_id": 2
},
{
"id": 12,
"name": "SubMenu2-Menu2",
"url": "",
"order": 220,
"type": 3,
"is_active": true,
"parent_id": 2
}],};
}
render() {
return (
<div>
{this.state.api.filter((el) => (el.parent_id === 0 ? el.name : null)).map((el) => (
<ReactBootStrap.Navbar >
{el.name}
<ReactBootStrap.NavDropdown>
{(el.parent_id === el.id ? el.name : null).map((el) =>el.name)}//I CANNOT ITERATE CORRECTLY THIS PART
</ReactBootStrap.NavDropdown>
</ReactBootStrap.Navbar>
))}
</div>
);
}
}
export default App;
see this map:
this.state.mz.map(item => {
if(item.parent_id === 0){
output[item.id] = item
} else {
if(!output[item.parent_id]['submenu']){
output[item.parent_id]['submenu'] = {}
}
output[item.parent_id]['submenu'][item.id] = item
}
})

Representing matrix in JSON

I am trying to have matrix style data in JSON, but it doesn't seem to work. Can anyone help me understand what am I doing wrong?
{
"took": 12,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 44,
"max_score": 1,
"hits": [
{
"_index": "transactions",
"_type": "transaction",
"_id": "trans0007",
"_score": 1,
"_source": {
"fundRelation": "[1,0,0,1,0,0,1,0,1,1,0,1,0,1,1,1,0,1,0,0],
[0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,1,1,0],
[0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[1,0,0,1,0,0,1,0,0,1,0,1,0,1,1,1,0,1,0,0],
[0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,1,0,0],
[0,0,1,1,0,0,0,0,0,0,0,1,0,0,0,0,0,1,1,0],
[0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1,1,0],
[0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1,1,0],
[1,0,0,1,0,0,1,0,1,1,0,1,0,0,1,1,0,1,1,0],
[1,0,0,1,0,0,1,0,1,1,0,1,0,1,0,1,0,1,1,0],
[1,0,0,1,0,0,1,0,1,1,0,1,0,1,1,0,0,1,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0],
[0,0,1,1,0,0,1,0,0,1,1,0,0,0,0,0,0,0,0,0],
[0,0,1,0,0,0,0,0,0,0,1,1,0,0,0,0,0,1,0,0],
[0,0,1,1,0,0,1,0,0,0,0,0,0,0,0,0,0,1,0,0]
",
"fundName": ["Fund A","Fund B","Fund C","Fund D","Fund E","Fund F","Fund G","Fund H","Fund I","Fund J","Fund K","Fund L","Fund M","Fund N","Fund O","Fund P","Fund Q","Fund R","Fund S","Fund T"],
"fundColor": ["#9ACD32","#377DB8","#F5DEB3","#EE82EE","#40E0D0","#FF6347","#D8BFD8","#D2B48C","#4682B4","#00FF7F","#FFFAFA","#708090","#708090","#6A5ACD","#87CEEB","#A0522D","#FFF5EE","#2E8B57","#F4A460","#FA8072"]
}
} ]
}
}
Not sure what i am doing wrong.
I am getting the following error message:
> Parse error on line 19: ... "fundRelation": "[1,0,0,1,0,0,1,0,1,
> -----------------------^ Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '['
at http://jsonlint.com/
Here problem is, you are trying to assign multi line string value to fundRelation which is not valid JSON.
....
"fundRelation": "[1,0,0,1,0,0,1,0,1,1,0,1,0,1,1,1,0,1,0,0],[0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]",
...
Or you can do something like this :
{
"took": 12,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 44,
"max_score": 1,
"hits": [
{
"_index": "transactions",
"_type": "transaction",
"_id": "trans0007",
"_score": 1,
"_source": {
"fundRelation": [
[1,0,0,1,0,0,1,0,1,1,0,1,0,1,1,1,0,1,0,0],
[1,0,0,1,0,0,1,0,1,1,0,1,0,1,1,1,0,1,0,0]
],
"fundName": ["Fund A","Fund B","Fund C","Fund D","Fund E","Fund F","Fund G","Fund H","Fund I","Fund J","Fund K","Fund L","Fund M","Fund N","Fund O","Fund P","Fund Q","Fund R","Fund S","Fund T"],
"fundColor":["#9ACD32","#377DB8","#F5DEB3","#EE82EE","#40E0D0","#FF6347","#D8BFD8","#D2B48C","#4682B4","#00FF7F","#FFFAFA","#708090","#708090","#6A5ACD","#87CEEB","#A0522D","#FFF5EE","#2E8B57","#F4A460","#FA8072"]
}
}
]
}
}
You can't have multi line strings in javascript (except in the newish engines using the ` operator). You need to escape the end of each line of fundRelation by adding a \ to end end of each line.
Alternatively, don't store the matrix data as a string. remove the quote at the beginning and end of the array and store it as a standard array
Looks like it's not ok with the " splitting in multiple lines.
In any case, shouldn't that be like this:
...
"fundRelation": [
[1,0,0,1,0,0,1,0,1,1,0,1,0,1,1,1,0,1,0,0],
[0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
...
[0,0,1,1,0,0,1,0,0,0,0,0,0,0,0,0,0,1,0,0]
]
...
Note the extra enclosing brackets [...].

Nested JSON not working in d3.js

I am using nested JSON file in my data visualization using d3.js. I was referring to a previous Smilar Queastion. I created a jsfiddle of the answer of the question to see whether things are working. But i am confused why the code is not working. I have similar type of problem in my project. How can i solve that. Here is sample code I am using for printing the data in the form of a list
d3.select("body").append("ul").selectAll("li")
.data(data).enter().append("li").each(function() {
var li = d3.select(this);
li.append("p")
.text(function(d) { return d.date; });
li.append("ul").selectAll("li")
.data(function(d) { return d.hours; }) // second level data-join
.enter().append("li")
.text(function(d) { return d.hour + ": " + d.hits; });
});
It looks like the JSON wasn't formed correctly. I fixed the JSON like this -
var data=[{ "date": "20120927",
"hours": [
{ "hour": 0, "hits": 823896 },
{ "hour": 1, "hits": 654335 },
{ "hour": 2, "hits": 548812 },
{ "hour": 3, "hits": 512863 },
{ "hour": 4, "hits": 500639 }
],
"totalHits": "32,870,234",
"maxHits": "2,119,767",
"maxHour": 12,
"minHits": "553,821",
"minHour": 3 },
{ "date": "20120928",
"hours": [
{ "hour": 0, "hits": 1235923 },
{ "hour": 1, "hits": 654335 },
{ "hour": 2, "hits": 1103849 },
{ "hour": 3, "hits": 512863 },
{ "hour": 4, "hits": 488506 }
],
"totalHits": "32,870,234",
"maxHits": "2,119,767",
"maxHour": 12,
"minHits": "553,821",
"minHour": 3 }];
Also, I think there was a problem with the HTTP d3.js library not being loaded. I added an external HTTPS reference and it is working.
Here is a working version of your fiddle - https://jsfiddle.net/ferlin_husky/wzuvob6m/

Delete element of Solr

I deleted an item you do not need to solr, but I solr response still appears.
The json:
{
"responseHeader": {
"status": 0,
"QTime": 1,
"params": {
"facet": "true",
"q": "*:*",
"facet.limit": "-1",
"facet.field": "manufacturer",
"wt": "json",
"rows": "0"
}
},
"response": {
"numFound": 84,
"start": 0,
"docs": []
},
"facet_counts": {
"facet_queries": {},
"facet_fields": {
"manufacturer": [
"Chevrolet",
0,
"abarth",
1,
"audi",
7,
"austin",
1,
"bmw",
2,
"daewoo",
2,
"ford",
1,
"fso",
1,
"honda",
1,
"hyundai",
1,
"jaguar",
3,
"lexus",
1,
"mazda",
1,
"mitsubishi",
1,
"nissan",
1,
"pontiac",
1,
"seat",
1
]
},
"facet_dates": {},
"facet_ranges": {}
}
}
the deleted item is "chevrolet", now this to '0 'but it still appears.
"manufacturer":["Chevrolet",0,
I wish I could delete the item completely, is that possible.. Thanks.
Here is a two step approach I would follow:
Make sure changes(deletion) is committed. You may issue a commit
If it still shows facets with zero count, you may append &facet.mincount=1 to your query
&facet.mincount=1 will make sure facets with zero count do not show up.
For more details, please refer to: http://wiki.apache.org/solr/SimpleFacetParameters#facet.mincount
In your case probably it is because of uninverted index created by solr.
Pass facet.mincount=1 in your query to get rid of this problem.