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

JSON Parsing issue

March 21 (4 years ago)
Ernest P.
Pro
API Scripter

Writing a character importer. In the JSON of the imported character I am getting an error on a closing square bracket "]". I have dropped the JSON (after some inline repairs) to a parser , which says it is valid JSON. I already have code to handle the \t, it's just commented. Any suggestions?

Error:

SyntaxError: Unexpected token ] in JSON at position 3910

Code:

      var token = getObj("graphic",selected[0]._id);

      var character = getObj("character",token.get("represents"));

      var herodesignerData = [];


//    log(character);

      var characterName = findObjs({type: 'attribute', characterid: character.id, name: 'name'})[0];


      character.get("gmnotes", function(gmnotes) {

        gmnotes = gmnotes.replace(/<[^>]*>/g, '');

        gmnotes = gmnotes.replace(/&[^;]*;/g, '');

        //gmnotes = gmnotes.replace(/\},\s{2,}\]/g, '\}\]');

        //gmnotes = gmnotes.replace(/[^ -~]/g, '');

        //gmnotes = gmnotes.replace('Other Attacks:', 'Other Attacks - 1/2 ');


      log(gmnotes);


        var hdJSON = JSON.parse(gmnotes);

JSON: (Prettied up a bit for reading, Error specifies 3910, line is bold and the first occurrence, line also bolded)
"{\"name\":\"Musala\",
\"version\":1.8,
\"str\":{\"value\":\"16\",
\"shortname\":\"str\",
\"name\":\"Strength\",
\"roll\":\"12-\",
\"damage\":\"\",
\"value2\":\"16\",
\"roll2\":\"12\",
\"damage2\":\"\"},
\"dex\":{\"value\":\"18\",
\"shortname\":\"dex\",
\"name\":\"Dexterity\",
\"roll\":\"13-\",
\"damage\":\"\",
\"value2\":\"18\",
\"roll2\":\"13\",
\"damage2\":\"\"},
\"con\":{\"value\":\"14\",
\"shortname\":\"con\",
\"name\":\"Constitution\",
\"roll\":\"12-\",
\"damage\":\"\",
\"value2\":\"14\",
\"roll2\":\"12\",
\"damage2\":\"\"},
\"int\":{\"value\":\"14\",
\"shortname\":\"int\",
\"name\":\"Intelligence\",
\"roll\":\"12-\",
\"damage\":\"\",
\"value2\":\"14\",
\"roll2\":\"12\",
\"damage2\":\"\"},
\"ego\":{\"value\":\"18\",
\"shortname\":\"ego\",
\"name\":\"Ego\",
\"roll\":\"13-\",
\"damage\":\"\",
\"value2\":\"18\",
\"roll2\":\"13\",
\"damage2\":\"\"},
\"pre\":{\"value\":\"15\",
\"shortname\":\"pre\",
\"name\":\"Presence\",
\"roll\":\"12-\",
\"damage\":\"\",
\"value2\":\"15\",
\"roll2\":\"12\",
\"damage2\":\"\"},
\"spd\":{\"value\":\"3\",
\"shortname\":\"spd\",
\"name\":\"Speed\",
\"roll\":\"\",
\"damage\":\"\",
\"value2\":\"3\",
\"roll2\":\"\",
\"damage2\":\"\"},
\"ocv\":{\"value\":\"4\",
\"shortname\":\"ocv\",
\"name\":\"Offensive Combat Value\",
\"roll\":\"\",
\"damage\":\"\",
\"value2\":\"4\",
\"roll2\":\"\",
\"damage2\":\"\"},
\"dcv\":{\"value\":\"6\",
\"shortname\":\"dcv\",
\"name\":\"Defensive Combat Value\",
\"roll\":\"\",
\"damage\":\"\",
\"value2\":\"6\",
\"roll2\":\"\",
\"damage2\":\"\"},
\"omcv\":{\"value\":\"3\",
\"shortname\":\"omcv\",
\"name\":\"Offensive Mental Combat Value\",
\"roll\":\"\",
\"damage\":\"\",
\"value2\":\"3\",
\"roll2\":\"\",
\"damage2\":\"\"},
\"dmcv\":{\"value\":\"6\",
\"shortname\":\"dmcv\",
\"name\":\"Defensive Mental Combat Value\",
\"roll\":\"\",
\"damage\":\"\",
\"value2\":\"6\",
\"roll2\":\"\",
\"damage2\":\"\"},
\"pd\":{\"value\":\"4\",
\"shortname\":\"pd\",
\"name\":\"Physical Defence\",
\"roll\":\"\",
\"damage\":\"\",
\"value2\":\"4\",
\"roll2\":\"\",
\"damage2\":\"\"},
\"pdr\":{\"value\":\"0\",
\"shortname\":\"pdr\",
\"name\":\"Physical Resistant Defence\",
\"roll\":\"\",
\"damage\":\"\",
\"value2\":\"0\",
\"roll2\":\"\",
\"damage2\":\"\"},
\"ed\":{\"value\":\"4\",
\"shortname\":\"ed\",
\"name\":\"Energy Defence\",
\"roll\":\"\",
\"damage\":\"\",
\"value2\":\"4\",
\"roll2\":\"\",
\"damage2\":\"\"},
\"edr\":{\"value\":\"0\",
\"shortname\":\"edr\",
\"name\":\"Energy Resistant Defence\",
\"roll\":\"\",
\"damage\":\"\",
\"value2\":\"0\",
\"roll2\":\"\",
\"damage2\":\"\"},
\"per\":{\"value\":\"12-\",
\"shortname\":\"per\",
\"name\":\"Perception Roll\",
\"roll\":\"12-\",
\"damage\":\"\"},
\"rec\":{\"value\":\"7\",
\"shortname\":\"rec\",
\"name\":\"Recovery\",
\"roll\":\"\",
\"damage\":\"\",
\"value2\":\"7\",
\"roll2\":\"\",
\"damage2\":\"\"},
\"body\":{\"value\":\"12\",
\"shortname\":\"body\",
\"name\":\"Body\",
\"roll\":\"\",
\"damage\":\"\",
\"value2\":\"12\",
\"roll2\":\"\",
\"damage2\":\"\"},
\"end\":{\"value\":\"30\",
\"shortname\":\"end\",
\"name\":\"Endurance\",
\"roll\":\"\",
\"damage\":\"\",
\"value2\":\"30\",
\"roll2\":\"\",
\"damage2\":\"\"},
\"stun\":{\"value\":\"26\",
\"shortname\":\"stun\",
\"name\":\"Stun\",
\"roll\":\"\",
\"damage\":\"\",
\"value2\":\"26\",
\"roll2\":\"\",
\"damage2\":\"\"},
\"movement\":{\"run\":{\"combat\":\"13m\",
\"noncombat\":\"26m\",
\"icon\":\"ui_run\"},
\"swim\":{\"combat\":\"7m\",
\"noncombat\":\"14m\",
\"icon\":\"ui_swim\"},
\"leap\":{\"combat\":\"7m\",
\"noncombat\":\"14m\",
\"icon\":\"ui_leap\"}},
\"skills\":[\t{\"name\":\"Perception\",
\"roll\":\"12-\"},
\t{\"name\":\"Acting\",
\"roll\":\"8-\"},
\t{\"name\":\"Paramedics\",
\"roll\":\"8-\"},
\t{\"name\":\"Stealth\",
\"roll\":\"8-\"},
\t{\"name\":\"PS\",
\"roll\":\"11-\"},
\t{\"name\":\"KS\",
\"roll\":\"11-\"},
\t{\"name\":\"KS\",
\"roll\":\"11-\"},
\t{\"name\":\"Shadowing\",
\"roll\":\"8-\"},
\t{\"name\":\"Persuasion\",
\"roll\":\"8-\"},
\t{\"name\":\"No Name Supplied\",
\"roll\":\"11-\"},
\t{\"name\":\"Climbing\",
\"roll\":\"8-\"},
\t{\"name\":\"Concealment\",
\"roll\":\"8-\"},
\t{\"name\":\"Wistian Language (idiomatic) (4 Active Points)\",
\"roll\":\"\"},
\t{\"name\":\"Navigation (Marine)\",
\"roll\":\"12-\"},
\t{\"name\":\"TF: Large Wind-Powered Boats, Small Wind-Powered Boats\",
\"roll\":\"\"},
\t{\"name\":\"WF: Blades, Clubs, Thrown Knives, Axes, and Darts, Unarmed Combat\",
\"roll\":\"\"},
\t{\"name\":\"+2 with a small group of attacks\",
\"roll\":\"\"},
\t{\"name\":\"Conversation\",
\"roll\":\"8-\"},
\t{\"name\":\"Deduction\",
\"roll\":\"8-\"},
\t{\"name\":\"AK: Wist\",
\"roll\":\"8-\"}],
\"attacks\":[],
\"martialArts\":[\t\t{\"name\":\"Other Attacks\",
\"gameEdition\":6 ,
\"ocvMod\":0,
\"dcvMod\":0,
\"dice\":3,
\"halfDice\":false,
\"effect\":\"Normal Attack\",
\"time\":\"½\",
\"level\":\"0\",
\"killingAttack\":false,
\"active\":\"1\",
\"targetNumber\":0,
\"targetCheck\":false,
\"stunMultiplierMod\":0,
\"tool\":\"Other Attacks:½ Phase, +0 OCV, +0 DCV, Normal attack\",
\"toHit\":true,
\"againstOCV\":false,
\"toPost\":false,
\"post\":\"\",
\"rngMod\":0,
\"abort\":false},
\t],
\"rolls\":[],
\"lightningReflex\":[{\"name\":\"Lightning Reflexes\",
\"levels\":0,
\"tool\":\"No Lightning Reflexes\",
\"isActive\":false}]}"

March 22 (4 years ago)
The Aaron
Roll20 Production Team
API Scripter

I think the root of the issue is likely in your preprocessing of the gmnotes text. If I remember correctly, you should call decode() (or maybe it was unescape() ?  This is all from memory...) on it first, then strip out the HTML tags, then the JSON.stringify should work. 

March 22 (4 years ago)
The Aaron
Roll20 Production Team
API Scripter

This is what I use in UniversalVTTImporter to strip down the gmnotes to just JSON:

 const importUVTTonGraphic = (token) => {
    let rawNotes = token.get('gmnotes');
    let notes = unescape(rawNotes).replace(/(?:<[^>]*>|\\t|&nbsp;)/g,'');
    let data;
    try {
      data = JSON.parse(notes);
    } catch( e ){
      return {error: "Universal VTT Data is missing or corrupt.", token};
    }

    /* ... */



March 22 (4 years ago)

Edited March 22 (4 years ago)
Ernest P.
Pro
API Scripter

Thanks, Aaron. I looked up decode and found a function you had over in the API Cookbook utility functions. Implemented that, removed the extraneous HTML tags (span, etc) and I was still getting the same error in the JSON parse. But the formatting in the log let me find the parse error (an invalid character) which turned out to be an extra trailing comma in the line \"abort\":false}, so thanks for the assist. 

- E