3

To my understanding, JSON strings were ways to package information to be sent around, much like xml.

This is also what's highly circulated in the stack-exchange questions eg: What is JSON and why would I use it?

However, a recent bot for a game that I play took json files as "scripts" of actions to perform. In this way, users of the bot were able to customize actions that the bot was expected to perform.

This seemed to violate my mental model of what json's were and what they could accomplish. My current suspicion is that rather than using these "script" json files as packages of information to send, they are instead processed internally by the bot, which then translates our "scripts" into real actions.

Please enlighten me if I've misunderstood what json is.

Community
  • 1
  • 1
user3605508
  • 303
  • 2
  • 12
  • 1
    It's just a way to format data that can be easily transferable, a wire format if you will. There's nothing else special about it. Just because the game is able to take that data and do something about it doesn't make it special. – Jeff Mercado Mar 23 '16 at 23:53

1 Answers1

2

JSON is just a structure, literally it is "JavaScript Object Notation", http://json.org/.

processed internally by the bot

is basically what is going on.

The json string is parsed into an object, and based on the values of that object the bot reacts. There is no scripting involved in it. However, it is possible that some of the values are literally script in a string, which can be used in JavaScript with eval in order to execute.

I suspect that eval is not being used in that fashion though, and that the bot is simply reading key value pairs to take as instruction for example moveright:5 feet.

Here is a very quick example of taking expected commands in json and then executing them in some sort of process. The implementation is basic, just a proof of concept.

var json = '{ "actions": [ { "speak": "hello world" }, { "color" : "red" } ]}';
var obj = JSON.parse(json);

var i = 0;
var bot = document.querySelector("#bot");

var actions = {
 speak : function(text){ bot.innerText = text; },
 color : function(c){ bot.style.color = c; }
};

function act(action){
  for(var key in action){
      var value = action[key];
      actions[key](value);
  }
  if(i <= obj.actions.length) 
    setTimeout(function(){
        act(obj.actions[i++]);
    },500);
}

setTimeout(function(){
  act(obj.actions[i++]);
},500);
<div id="bot">:)</div>
Travis J
  • 81,153
  • 41
  • 202
  • 273