Currently I am using the javascript library Clusterfck to perform a data grouping. This library generates an array in the following format:
[
{"canonical":[20,120,102],
"size":1
},
{"canonical":[250,255,253],
"left":{
"canonical":[250,255,253],
"size":1
},
"right":{
"canonical":[255,255,240],
"size":1
},
"size":2
},
{"canonical":[100,54,300],
"size":1
}
]
But javascript libraries for data visualization how D3.js and Jit using JSON structures as shown below:
{
"canonical":"Pai",
"children":[
{"canonical":[20,120,102], "size":1},
{
"canonical":[250,255,253],
"children": [
{"canonical":[250,255,253], "size":1},
{"canonical":[255,255,240], "size":1}
],
"size":2
},
{
"canonical":[100,54,300],
"size":1
}
]
}
I would like to convert these structures, using JavaScript or PHP. Could anyone help me?
If you just want to convert between these structures, this is pretty simple. See working fiddle: http://jsfiddle.net/nrabinowitz/vuk94/
function convert(input, rootName) {
// top level
if (Array.isArray(input)) {
return {
"canonical": rootName,
"children": input.map(convert)
};
}
// node
else {
['left', 'right'].forEach(function(side) {
if (input[side]) {
input.children = input.children || [];
input.children.push(convert(input[side]));
delete input[side];
}
});
return input;
}
}
This uses a couple of ECMAScript 1.5 features (Array.isArray, forEach, and map), but if you're using d3 you're probably targeting browsers that support this anyway.
Related
I need to access the nav for a specific date from the below JSON. eg : data[date="20-04-2022"].nav
How do I do it in Google Apps Script?
The standard JSON notation is not working.
{
"meta":{
"fund_house":"Mutual Fund",
"scheme_type":"Open Ended Schemes",
},
"data":[
{
"date":"22-04-2022",
"nav":"21.64000"
},
{
"date":"21-04-2022",
"nav":"21.69000"
},
{
"date":"20-04-2022",
"nav":"21.53000"
}
],
"status":"SUCCESS"
}
In your situation, I thought that it is required to retrieve the element including "date": "20-04-2022" from the array of data. So, how about the following sample script?
Sample script:
const obj = {
"meta": {
"fund_house": "Mutual Fund",
"scheme_type": "Open Ended Schemes",
},
"data": [
{
"date": "22-04-2022",
"nav": "21.64000"
},
{
"date": "21-04-2022",
"nav": "21.69000"
},
{
"date": "20-04-2022",
"nav": "21.53000"
}
],
"status": "SUCCESS"
};
const search = "20-04-2022";
const res = obj.data.find(({ date }) => date == search);
const value = res && res.nav;
console.log(value) // 21.53000
For example, if the search value is always found, you can use the following script.
const res2 = obj.data.find(({ date }) => date == search).nav;
Reference:
find()
Added 1:
From your following reply,
This looks like standard java script. Does not work in google apps script(script.google.com/home). Getting syntax error for this line : const res = obj.data.find(({ date }) => date == search);
I'm worried that you are not enabling V8 runtime. Ref If you cannot use V8 runtime, how about the following sample script?
Sample script:
var obj = {
"meta": {
"fund_house": "Mutual Fund",
"scheme_type": "Open Ended Schemes",
},
"data": [
{
"date": "22-04-2022",
"nav": "21.64000"
},
{
"date": "21-04-2022",
"nav": "21.69000"
},
{
"date": "20-04-2022",
"nav": "21.53000"
}
],
"status": "SUCCESS"
};
var search = "20-04-2022";
var res = obj.data.filter(function (e) { return e.date == search })[0];
var value = res && res.nav;
console.log(value) // 21.53000
Added 2:
From your following reply,
This looks like standard java script. Does not work in google apps script(script.google.com/home). Getting syntax error for this line : const res = obj.data.find(({ date }) => date == search);
I am trying to write a google apps script to fetch data from a url. But google seems to have its own way of handling the JSON data which I am unable to figure out. developers.google.com/apps-script/guides/services/…
I understood that your actual goal was to retrieve the value using Web Apps. If my understanding of your actual goal, how about the following sample script?
1. Sample script:
Please copy and paste the following script to the script editor and save the script.
function doGet(e) {
var search = e.parameter.search;
var obj = {
"meta": {
"fund_house": "Mutual Fund",
"scheme_type": "Open Ended Schemes",
},
"data": [
{
"date": "22-04-2022",
"nav": "21.64000"
},
{
"date": "21-04-2022",
"nav": "21.69000"
},
{
"date": "20-04-2022",
"nav": "21.53000"
}
],
"status": "SUCCESS"
};
var res = obj.data.filter(function (e) { return e.date == search })[0];
var value = res && res.nav;
return ContentService.createTextOutput(value);
}
I think that this sample script can be used with and without V8 runtime.
2. Deploy Web Apps.
In this case, please check the official document. Please set it as follows.
Execute as: Me
Anyone with Google account: Anyone
In this case, it supposes that you are using new IDE. Please be careful this.
3. Testing.
Please access to the URL like https://script.google.com/macros/s/{deploymentId}/exec?search=20-04-2022 using your browser. By this, the result value is returned.
`
I am trying to use both the .std() and the .custom() function in the reductio library.
My code is as follows for the reductio part:
dims.theme1 = myCrossfilter.dimension(function(d) {return(d.theme1);});
groups.theme1 = dims.theme1.group();
var reducer = reductio()
.custom({initial:reduceInit,add:reduceAdd,remove:reduceRemove})
.std("pl");
reducer(groups.theme1);
My code for the custom functions is :
reduceAdd = function(p,v) {
if (!p.fundsData.hasOwnProperty(v.AdyneAccount)) {
p.fundsData[v.AdyneAccount]=0;
}
if (!p.stratsData.hasOwnProperty(v.Strategy)) {
p.stratsData[v.Strategy]=0;
}
p.fundsData[v.AdyneAccount]+=+v.plfund;
p.stratsData[v.Strategy]+=+v.plstrat;
p.value+=+v.pl;
return(p);
};
reduceRemove = function(p,v) {
p.fundsData[v.AdyneAccount]-=+v.plfund;
p.stratsData[v.Strategy]-=+v.plstrat;
p.value-=+v.pl;
return(p);
};
reduceInit = function(p,v) {
return({
value:0,
fundsData:{},
stratsData:{}
});
};
I would expect my result (by doing groups.theme1.all()[0]) to look like the below (the values I have put in are random for this example) :
{
"key": "theTheme",
"value": {
"value": 10,
"fundsData": {
"a": 10,
"b": 5,
"c": 4
},
"stratsData": {
"somename": 8
},
"count": null,
"sum": null,
"sumOfSq": null,
"std": 0
}
}
And it does but it doesn't produce the values for count, sum and sumOfSq (so for std neither of course).
When I run .std("pl") by itself without my custom function on the exact same set of records, it works as expected. I do not understand why the addition of a custom function would prevent the correct calculation for the .std("pl") part.
All help welcome !
It looks to me like this is a bug in Reductio. The test names indicate that this shouldn't interfere with other aspects of reducers, but the content of the test makes clear that it does wipe them out.
https://github.com/crossfilter/reductio/blob/cd99f5043990a838b7e04ea86dbae3c1a1203119/test/custom.spec.js#L48
I've created an issue for this. No idea when I'll be able to get to it though. You'll probably want to just implement a normal Crossfilter custom reducer for this until I can fix it or someone sends a pull request.
I'm using searchkit to try to build a basic text search. I think the query I want to build is fairly simple. It needs to be structured like this:
{
"query":{
"bool":{
"must":[
{
"multi_match":{
"query":"test search",
"type":"phrase_prefix",
"fields":[
"field_1^5",
"field_2^4",
"field_3"
]
}
},
{
"term":
{
"field_id": "3"
}
}
],
"must_not":[
{
"term":
{
"status": "archived"
}
}
]
}
},
"size":6,
"highlight":{
"fields":{
"field_1":{},
"field_2":{},
"field_3":{}
}
}
}
I've tried using the prefixQueryFields attribute, which gave me something fairly close to what I wanted except it was using a BoolShould rather than a BoolMust, plus it was always including the default SimpleQueryString query. It looked something like this:
const prefixQueryFields = [
'field_1^5',
'field_2^4',
'field_3',
];
...
<SearchBox
searchOnChange={true}
prefixQueryFields={prefixQueryFields}
/>
I couldn't figure out the issues there easily and decided to go with the queryBuilder attribute in SearchBox. This is what I came up with:
_queryBuilder(queryString) {
const prefixQueryFields = [
'field_1^5',
'field_2^4',
'field_3',
];
return new ImmutableQuery()
.addQuery(BoolMust([
MultiMatchQuery(queryString, {
type: 'phase_prefix',
fields: prefixQueryFields,
})
]))
.addQuery(BoolMustNot([
TermQuery('status', 'archived'),
]));
}
...
<SearchBox
searchOnChange={true}
queryBuilder={this.queryBuilder}
/>
This query came out even more messed up, and I have no idea what to try next after checking the documentation and a cursory look at the source code.
(For the sake of brevity, I will not bother including the incorrect queries these two attempts created unless someone thinks that info will be useful.)
Figured it out. Using the QueryDSL structures wasn't working out very well, but apparently you can create the query with pure JSON, which worked great. Basically updated my query builder to return as so:
return {
bool: {
must: [
{
multi_match:{
query: queryString,
type: 'phrase_prefix',
fields: prefixQueryFields,
}
}
],
must_not: [
{
term: {
status: 'archived',
}
}
]
}
};
I was trying a sample using breeze and knockout
manager.executeQuery(query).then(function(result){
console.log(result);
ko.applyBindings(result);
}).fail(function(e) {
console.log(e);
alert(e);
});
While printing in the console. I'm getting two objects in the path data.XHR.result.responseJSON and two objects in data.results
But in the view created as a result of knockout binding I'm getting the second set of value populated two times. (I have two set of values in the db)
NOTE: This code is working if I havent defined any metadata. Issue is in the scenario where I use metadata
metadata
var sample=sample||{};sample.metadata=
{
"dataServices":[
{
"serviceName":"/sample",
"hasServerMetadata":true,
"jsonResultsAdapter":"webApi_default",
"useJsonp":false
}
],
"structuralTypes":[
{
"shortName":"Employee",
"autoGeneratedKeyType":"None",
"defaultResourceName":"Employee",
"dataProperties":[
{
"name":"id",
"dataType":"MongoObjectId",
"isNullable":false,
"defaultValue":"",
"isPartOfKey":true,
"validators":[
{
"name":"required"
}
]
},
{
"name":"name",
"dataType":"String",
"maxLength":100,
"validators":[
{
"maxLength":100,
"name":"maxLength"
}
]
},
{
"name":"age",
"dataType":"String",
"maxLength":100,
"validators":[
{
"maxLength":100,
"name":"maxLength"
}
]
}
]
}
],
"resourceEntityTypeMap":{
"Employee":"Employee"
}
};
Make sure that you have a primary key that is defined in your model and coming across the wire. If you leave those values as null they will overwrite each other when breeze adds them to the cache and sees the keys are the same.
Ok, so I am programming a web operating system using js. I am using JSON for the file system. I have looking online for tutorials on JSON stuff for about a week now, but I cannot find anything on writing JSON files from a web page. I need to create new objects in the file, not change existing ones. Here is my code so far:
{"/": {
"Users/": {
"Guest/": {
"bla.txt": {
"content":
"This is a test text file"
}
},
"Admin/": {
"html.html": {
"content":
"yo"
}
}
},
"bin/": {
"ls": {
"man": "Lists the contents of a directory a files<br/>Usage: ls"
},
"cd": {
"man": "Changes your directory<br/>Usage: cd <directory>"
},
"fun": {
"man": "outputs a word an amount of times<br/>Usage: fun <word> <times>"
},
"help": {
"man": "shows a list of commands<br/>Usage: help"
},
"clear": {
"man": "Clears the terminal<br/>Usage: clear"
},
"cat": {
"man": "prints content of a file<br/>Usage: cat <filename>"
}
},
"usr/": {
"bin/": {
},
"dev/": {
}
}
}}
I think the better solution is to stringify your JSON, encode with base64 encoding and then send it to a server-side script (a PHP page, for instance) which could save this file. See:
var json = JSON.stringify(myJson);
var encoded = btoa(json);
You can use ajax for sending:
var xhr = new XMLHttpRequest();
xhr.open('POST','myServerPage.php',true);
xhr.setRequestHeader('Content-type','application/x-www-form-urlencoded');
xhr.send('json=' + encoded);
And in the server-side:
$decoded = base64_decode($_POST['json'])
$jsonFile = fopen('myJson.json','w+');
fwrite($jsonFile,$decoded);
fclose($jsonFile);
I'd take off the "/"'s from the keys then could split on "/" and walk the tree by shifting values off the result. For example, the following code will create the full path if it doesn't already exist, but preserving the folder & contents if it does.
var fs = {
"bin": {
"mkdir": function(inPath) {
// Gets rid of the initial empty string due to starting /
var path = inPath.split("/").slice(1);
var curFolder = fs;
while(path.length) {
curFolder[path[0]] = curFolder[path[0]] || {};
curFolder = curFolder[path.shift()];
}
}
}
}
fs.bin.mkdir("/foo/bar");
console.log(JSON.stringify(fs, function(key,val) {
if(key === 'mkdir') return undefined;
return val;
}, 2));
Output:
{
"bin": {},
"foo": {
"bar": {}
}
}
As others have mentioned, rather than building the JSON object by hand with strings, to avoid syntax errors (and frustration), building it through code then using JSON.stringify to get the final result would likely be simpler.