Roll20 uses cookies to improve your experience on our site. Cookies enable you to enjoy certain features, social sharing functionality, and tailor message and display ads to your interests on our site and others. They also help us understand how our site is being used. By continuing to use our site, you consent to our use of cookies. Update your cookie preferences .
×
Create a free account

Need help Modifing Json Export/import script to filter null values

Help... I am using Michael N.'s Scrypt from&nbsp; <a href="https://app.roll20.net/forum/post/1755413/imports-" rel="nofollow">https://app.roll20.net/forum/post/1755413/imports-</a>... and have been attempting to rewrite a section of it to remove all null values exported from sheets in the below section of code but I keep going in circles. function exportAttributes( charID ){ var attributes = findObjs({_type: "attribute", _characterid: charID}); var exported = []; for(var i in attributes) { a = attributes[i]; exported.push({ "name": a.get("name"), "current": a.get("current"), "max": a.get("max") }); } return exported; } // My knowledge in Javascript is limited to what I learned in high school many years ago. but what I am looking for is if the json created would contain this: { "name": "Generic_Attribute1", "current": "42 is the Answer to the Ultimate Question of Life, the Universe, and Everything", }, { "name": "Generic_Attribute2", "max": "11d7+33" }, instead of this { "name": "Generic_Attribute1", "current": "42 is the Answer to the Ultimate Question of Life, the Universe, and Everything", "max": "" }, { "name": "Generic_Attribute2", "current": "", "max": "11d7+33" }, { "name": "Generic_Attribute3", "current": "", "max": "" }, //even if "name": "Generic_Attribute3", could not be removed it would make things better for me
1520610641

Edited 1520611383
Jakob
Sheet Author
API Scripter
Try this function exportAttributes(charID) { &nbsp; "use strict"; &nbsp; return findObjs({ &nbsp; &nbsp; _type: "attribute", &nbsp; &nbsp; _characterid: charID &nbsp; }).reduce((m, attr) =&gt; { // _.omit filters an object by property values - in this case, it filters out properties // whose values are empty strings &nbsp; &nbsp; const data = _.omit({ &nbsp; &nbsp; &nbsp; current: attr.get('current'), &nbsp; &nbsp; &nbsp; max: attr.get('max'), &nbsp; &nbsp; &nbsp; name: attr.get('name'), &nbsp; &nbsp; }, x =&gt; x === ''); // Only include an attribute in the final data set if the name is nonempty and either // current or max are present &nbsp; &nbsp; if ('name' in data && ('current' in data || 'max' in data)) m.push(data); return m; &nbsp; }, []); }
1520610801
The Aaron
Pro
API Scripter
Something like this: function exportAttributes( charID ){ &nbsp; &nbsp; var attributes = findObjs({_type: "attribute", _characterid: charID}); &nbsp; &nbsp; var exported = []; &nbsp; &nbsp; for(var i in attributes) &nbsp; &nbsp; { &nbsp; &nbsp; &nbsp; &nbsp; a = attributes[i]; &nbsp; &nbsp; &nbsp; &nbsp; let o = {}, &nbsp; &nbsp; &nbsp; &nbsp; let p = ['name','current','max']; &nbsp; &nbsp; &nbsp; &nbsp; _.each(p, (k)=&gt;{ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let v = a.get(p); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(v.length){ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; o[p]=v; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; &nbsp; &nbsp; }); &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; exported.push(o); &nbsp; &nbsp; } &nbsp; &nbsp; return exported; }
error: TypeError: Cannot read property 'push' of undefined TypeError: Cannot read property 'push' of undefined at findObjs.reduce (apiscript.js:148:66) at Array.reduce (native) at exportAttributes (apiscript.js:137:6) at exportChar (apiscript.js:97:25) at exportAll (apiscript.js:55:7) at apiscript.js:43:3 at eval (eval at (/home/node/d20-api-server/api.js:146:1), :65:16) at Object.publish (eval at (/home/node/d20-api-server/api.js:146:1), :70:8) at checkForReady (/home/node/d20-api-server/api.js:1314:12) at /home/node/d20-api-server/api.js:1394:9
The Arron this is the error I got with your edit: For reference, the error message generated was: SyntaxError: let is disallowed as a lexically bound name &nbsp;
1520611400
Jakob
Sheet Author
API Scripter
E. ManWhoLaughs said: error: TypeError: Cannot read property 'push' of undefined TypeError: Cannot read property 'push' of undefined at findObjs.reduce (apiscript.js:148:66) at Array.reduce (native) at exportAttributes (apiscript.js:137:6) at exportChar (apiscript.js:97:25) at exportAll (apiscript.js:55:7) at apiscript.js:43:3 at eval (eval at (/home/node/d20-api-server/api.js:146:1), :65:16) at Object.publish (eval at (/home/node/d20-api-server/api.js:146:1), :70:8) at checkForReady (/home/node/d20-api-server/api.js:1314:12) at /home/node/d20-api-server/api.js:1394:9 Ooops, forgot a line. Try again.
that worked jakob thanks&nbsp;
I've been playing with it and is it possible to modify the .omit to include other values: I'm interested in "0" and 0. I've been reading on it but I cant seem to grasp it with my limited knowledge.
1520617284
Jakob
Sheet Author
API Scripter
E. ManWhoLaughs said: I've been playing with it and is it possible to modify the .omit to include other values: I'm interested in "0" and 0. I've been reading on it but I cant seem to grasp it with my limited knowledge. Absolutely. You want to modify this line: }, x =&gt; x === ''); and replace the boolean expression x === '' by something else. For example, to also exclude 0 (I'm not exactly sure if attribute values are ever integers, though, they should normally be strings): }, x =&gt; (x === '' || x === 0) ); To explain what's happening: the second argument of&nbsp; _.omit,&nbsp; x =&gt; x === '' , is a function that sends x to the value of the expression&nbsp; x === '' (so either true or false), and _.omit omits properties where the value of the property makes this expression true. &nbsp;So by substituting another expression, namely&nbsp; (x === '' || x === 0) , we change the conditions under which this evaluates to true. Adding "0" is an exercise :).
a million thanks I knew what expressions I need just not how I formated them into the function (Json size is reduced by almost 50% now)
1520619926
The Aaron
Pro
API Scripter
E. ManWhoLaughs said: The Arron this is the error I got with your edit: For reference, the error message generated was: SyntaxError: let is disallowed as a lexically bound name &nbsp; Ah.. had a , instead of a ; at the end of the first let definition. =D&nbsp; Glad to see you got it working though.