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

[Help] How to access data from JSON or other file

1659596912
Julexar
Pro
API Scripter
Hi, I stumbled upon a little Problem, when I was trying to use a JSON file for a list of things that I wanted my Script to use but it didn't want to work. How could I make use of an external JSON file in my Script without it getting too complicated. Specifically I'm trying to read from the file and use it as like ItemList.Rarity.ItemType.Item.Description and then have that Output what I wrote in the description for that Item for example. Here a little example: CODE readTextFile = function ( file , callback ) {         var rawFile = new XMLHttpRequest ();         rawFile . overrideMimeType ( "application/json" );         rawFile . open ( "GET" , file , true );         rawFile . onreadystatechange = function () {             if ( rawFile . readyState === 4 && rawFile . status == "200" ) {                 callback ( rawFile . responseText );             }         }         rawFile . send ( null ); }, craftmenu = function ( charid , type , specific , rarity , amount , msg ) {          readTextFile ( "./items.json" , function ( text ) {             var data = JSON . parse ( text );             log ( data );         }); } JSON {     "Common" : {         "Weapon" : {             "Armblade" : {                 "description" : "An Armblade is a magic weapon that attaches to your arm, becoming inseperable from you as long as you're attuned to it. To attune to this item, you must hold it against your forearm for the entire attunement period.<br><br>As a bonus action, you can retract the armblade into your forearm or extend it from there. While it is extended, you can use the weapon as if you were holding it, and you can't use that hand for other purposes." ,                 "attunement" : true ,                 "attune-requirement" : "warforged"             }         }     } }
1659599029

Edited 1659600068
Oosh
Sheet Author
API Scripter
Does the readTextFile() function work? It's probably a silly question, but maybe there's some clever way to do it, and you've found it. Assuming it doesn't work: a) you can't make your own XHRs from the sandbox. It's a sandbox and you can't connect to other domains unless the sandbox API allows it b) you've given a relative file path, by leading with "./" which tells the browser to look in the relative local path. That means you'd need to know the context of where the sandbox is looking, and to upload the JSON to that URI. c) all the CORS issues that come along with the above points Some clever sheet authors have come up with a few systems for grabbing JSON data through either API & handouts, or a field in character sheets - I'm assuming this is what you're looking for. Aforementioned clever sheet authors/script authors can probably expand on that. Bear in mind, this requires user action. If you want JSON data coming from your own source - you're going to need to use a clever browser extension or something equally insidious. The sandbox is.... well... sandboxed. Grabbing random data from external domains is exactly what it's supposed to be stopping.
1659602422
Julexar
Pro
API Scripter
I see, so instead of a JSON it would probably be easier to just do it through an array Thanks for the help though! This is what I've come up with:     setItemList = function() {         state.List = {             common: {                 weapon: [                     {                         Item: {                             description: "Description of the Item here",                             attunement: true,                             attune_requirement: "Attunement requirement here",                             specifics: "Specific item types (martial, light etc) here",                             name: "Name of the item here"                         }                     }                 ]             }         };     },
1659605158
Julexar
Pro
API Scripter
Little correction in case anyone else wants to use something like this and is stuggeling:     setItemList = function() {         state.List = {             common: {                 weapon: [                     {                         description: "Description of the Item here",                         attunement: true,                         attune_requirement: "Attunement requirement here",                         specifics: "Specific item types (like martial, light etc) here",                         name: "Name of the item here"                     },                    ...                 ]             }         };     }, You can simply find any Object with this: state.List.common.weapon.find(item => item.name==="Name of the item here");