JSON Bag-of-Tokens (JSON-Bag) Model
- JSON-Bag is a formalism that tokenizes JSON-serialized game states into multisets of tokens, forming a normalized probability distribution for trajectory comparison.
- It leverages Jensen–Shannon distance with prototype-based classification to outperform hand-crafted features across diverse tabletop games.
- The model treats JSON as a multiset of lexical and structural tokens, enabling flexible handling of ordered and unordered lists for various analysis tasks.
JSON Bag-of-Tokens Model (JSON-Bag) denotes a family of bag-of-tokens formalisms built around JSON serialization. In the sense introduced by "JSON-Bag: A generic game trajectory representation," JSON-Bag is a game-agnostic representation of trajectories obtained by tokenizing JSON-serialized game states into a multinomial distribution over tokens and comparing trajectories with Jensen–Shannon distance (JSD) (Nguyen et al., 1 Aug 2025). Later work reused the same expression for token-accounting models of structured output generation and JSON-like tabular encodings, where the emphasis shifts from behavioral similarity to prompt, structure, and repair costs in serialized data (Matveev, 8 Feb 2026, Nandakishore, 7 Apr 2026). Across these usages, the common principle is to treat JSON not primarily as a schema-bound object graph, but as a multiset of lexical and structural tokens whose frequencies support comparison, classification, or cost estimation.
1. Conceptual basis and scope
In the game-trajectory formulation, a trajectory is the full sequence of game states from start to end. Each state is serialized to JSON, and a trajectory is the ordered list of these JSON-serialized states. No explicit action history is required; only state data are serialized (Nguyen et al., 1 Aug 2025).
The motivation is to provide a single representation and distance measure that works across diverse games and agents. The paper explicitly situates JSON-Bag in tasks such as agent identification, playstyle clustering, parameter analysis, seed analysis, and novelty estimation. JSON is adopted as the canonical substrate because it is widely supported, human-readable, and close-to-free to produce in many game frameworks. The approach assumes that game objects can be serialized into JSON with little code, yielding a uniform, language-neutral, schema-flexible basis for trajectory comparison (Nguyen et al., 1 Aug 2025).
This framing is also a methodological contrast. Hand-crafted features require domain expertise and often do not transfer well across games, while deep models may still require game-specific configuration. JSON-Bag instead converts any JSON-logged trajectory into a bag of tokens and treats that bag as a probability distribution, enabling direct comparison through an information-theoretic metric. In the reported experiments, the method is applied to six tabletop games: 7 Wonders, Dominion, Sea Salt and Paper, Can’t Stop, Connect4, and Dots and Boxes (Nguyen et al., 1 Aug 2025).
2. Tokenization and probabilistic representation
JSON-Bag tokenization operates on key-paths and atomic values. An atomic JSON component is any value that is not a dictionary or list. Each atomic value becomes a token labeled by its dot-separated key-path plus the value itself. The paper’s example serializes {"currentAge": 2, "playerResources": [ {"Wood": 2}, {"Wood": 2} ]} into unordered tokens such as .currentAge.2, .playerResources.Wood.2, and .playerResources.Wood.2; if list order matters, indexed forms such as .playerResources[0].Wood.2 and .playerResources[1].Wood.2 are used (Nguyen et al., 1 Aug 2025).
List handling is therefore central. If list order is semantically meaningful, ordered tokenization includes indices; if not, unordered tokenization suppresses them so that equivalent content does not become spuriously distinct. The reported configuration is game-specific but not schema-specific beyond this indexing rule: 7 Wonders, Dominion, and Sea Salt and Paper use unordered tokenization; Can’t Stop uses both; Connect4 and Dots and Boxes use ordered tokenization (Nguyen et al., 1 Aug 2025).
Both categorical and numeric values are used as literal suffixes in tokens. There is no discretization or binning, no string normalization, and no vocabulary pruning or frequency thresholding in the reported experiments. This choice preserves exact serialized values, but it also exposes a limitation: treating every numeric value as a distinct token can fragment signal, as with many unique turnCount.* tokens. The paper identifies binning and special handling of coordinates as concrete future refinements (Nguyen et al., 1 Aug 2025).
For a trajectory, token occurrences are counted to form a bag-of-tokens and then normalized to sum to one, yielding a multinomial distribution over vocabulary :
No smoothing is applied. In the trajectory setting, this is sufficient because the Jensen–Shannon mixture distribution remains non-zero wherever either trajectory assigns non-zero mass (Nguyen et al., 1 Aug 2025).
3. Jensen–Shannon distance and prototype-based classification
The distance function used with JSON-Bag is Jensen–Shannon distance. For discrete distributions and on shared support , the paper defines Kullback–Leibler divergence, mixture distribution, Jensen–Shannon divergence, and the distance form as follows:
The logarithm base is not specified. The paper notes that either natural or base-2 logarithms are acceptable because changing log base scales JS by a positive constant and does not affect nearest-neighbor rankings. No Laplace or add-0 smoothing is used; terms with 1 are taken as zero, and when 2 but 3, the mixture still has positive mass, so JS remains finite on the union of supports (Nguyen et al., 1 Aug 2025).
Classification is performed with prototype-based nearest-neighbor search (P-NNS). For a class 4 with trajectory distributions 5, the class prototype is the normalized average distribution,
6
The paper interprets 7 as the maximum-likelihood estimate of the class’s true token distribution under a multinomial model. Given a query trajectory 8, one computes 9 for every class and predicts the class with the smallest value (Nguyen et al., 1 Aug 2025).
This procedure is computationally simple. Building prototypes requires 0 time to count tokens over 1 trajectories with 2 tokens per trajectory, with storage 3 for 4 classes. Query time is 5, with sparse supports reducing constants in practice. The paper also compares JSD against cosine similarity and Euclidean distance on normalized token vectors. JSON-L2 and JSON-Cosine are generally comparable and sometimes slightly better, but JSD is often better on parameter and seed tasks and is preferred for its probabilistic interpretation (Nguyen et al., 1 Aug 2025).
4. Experimental evaluation across six tabletop games
The empirical study evaluates six games under three trajectory-classification tasks: classifying the playing agents, game parameters, or game seeds. Agent classification uses five classes—Random, OSLA, MCTS-V, MCTS64, and MCTS128—while parameter and seed classification each use four classes. Connect4 and Dots and Boxes include only agent and parameter tasks. The dataset contains 500 trajectories per class, with a 50/50 train/test split; games are 4-player except Connect4 and Dots and Boxes, which are 2-player. For agents, trajectories are generated by self-play within class; for parameters and seeds, they are generated by MCTS64 at 32 ms per decision. The study also removes explicit variables that would trivially encode labels, in order to prevent data leakage (Nguyen et al., 1 Aug 2025).
Hand-crafted baseline features include game duration, end scores, per-player linear score-trend parameters, and additional game-specific features. The counts reported are 47 for 7 Wonders, 37 for Dominion, 46 for Sea Salt and Paper, 17 for Can’t Stop, 13 for Connect4, and 8 for Dots and Boxes (Nguyen et al., 1 Aug 2025).
| Game | JSON-Bag + JSD + P-NNS | Hand-crafted baseline |
|---|---|---|
| 7 Wonders | 0.742 / 0.546 / 0.942 | 0.696 / 0.476 / 0.573 |
| Dominion | 0.938 / 1.000 / 0.350 | 0.911 / 0.996 / 0.462 |
| Sea Salt and Paper | 0.718 / 0.990 / 0.983 | 0.425 / 0.854 / 0.856 |
| Can’t Stop | 0.493 / 0.977 / 0.922 | 0.474 / 0.414 / 0.504 |
| Connect4 | 0.644 / 1.000 | 0.476 / 0.943 |
| Dots and Boxes | 0.509 / 1.000 | 0.702 / 0.852 |
The table shows the reported test accuracies, ordered as Agent/Param/Seed where seed classification exists. The principal empirical claim is that JSON-Bag outperforms the hand-crafted baseline in the majority of tasks. The strongest gains are concentrated in parameter and seed classification, including Sea Salt and Paper parameters at 0.990 versus 0.854, 7 Wonders seeds at 0.942 versus 0.573, Can’t Stop parameters at 0.977 versus 0.414, Connect4 parameters at 1.000 versus 0.943, and Dots and Boxes parameters at 1.000 versus 0.852 (Nguyen et al., 1 Aug 2025).
The exceptions are also instructive. In Dominion seed classification, the hand-crafted baseline exceeds P-NNS, and in Dots and Boxes agent classification, the hand-crafted baseline is substantially stronger. The paper identifies sparse games and games with strong spatial dependence as settings where raw JSON may carry less signal for plain nearest-prototype classification. A further control, JSON-Char, tokenizes individual JSON characters rather than structured key-path tokens and is reported to be surprisingly competitive in some settings, underscoring that regular JSON structure itself carries information (Nguyen et al., 1 Aug 2025).
The study also reports N-shot prototype construction with 6, averaged over 20 trials. For easy tasks, defined in the paper as those with full-data accuracy at least 0.8, 5–10 shots per class already achieve high accuracy. Examples include 7 Wonders seeds from 0.545 at 3-shot to 0.904 at 40-shot, Dominion parameters at 1.000 from as low as 3-shot, Sea Salt and Paper seeds from 0.874 at 3-shot to 0.980 at 40-shot, Connect4 parameters at 1.000 for all 7, and Dots and Boxes parameters at approximately 1.000 for all 8. Diminishing returns beyond about 20–40 shots are evident on many tasks (Nguyen et al., 1 Aug 2025).
5. Automatic feature extraction and behavioral interpretation
JSON-Bag is not limited to nearest-prototype classification. The paper also treats each token’s normalized frequency as a feature and uses Random Forest (RF) for downstream classification. This reframes JSON-Bag as an automatic feature extractor: the representation remains game-agnostic, but RF can learn splits and interactions among tokens without manual feature design (Nguyen et al., 1 Aug 2025).
The reported gains are substantial on tasks where P-NNS underperforms. In 7 Wonders, RF increases accuracy from 0.742/0.546/0.942 to 0.785/0.841/1.000; in Dominion, from 0.938/1.000/0.350 to 0.989/1.000/0.609; in Can’t Stop, from 0.493/0.977/0.922 to 0.548/0.999/0.983; in Connect4, from 0.644/1.000 to 0.868/1.000; and in Dots and Boxes, from 0.509/1.000 to 0.716/1.000. The paper summarizes these improvements as especially strong for agent classification in sparse games, for 7 Wonders parameter classification, and for Dominion seeds. It further suggests using RF’s mean decrease in impurity to surface discriminative tokens for human analysis, although no such analysis is reported (Nguyen et al., 1 Aug 2025).
A separate line of analysis compares distances between agent-class prototypes with distances between agents’ policies. For a set of states 9, the policy distance between agents 0 and 1 is defined as
2
The state set 3 is generated via random play. Random and OSLA policies are estimated by sampling actions 4 times at each state, while MCTS policies are approximated via a softmax over root-visit counts. The reported Pearson correlations between prototype JSD and policy distance are 0.8824 for 7 Wonders, 0.773 for Dominion, 0.6235 for Sea Salt and Paper, 0.9688 for Can’t Stop, 0.6225 for Connect4, and 0.813 for Dots and Boxes (Nguyen et al., 1 Aug 2025).
The paper’s stated implication is that JSON-Bag prototype distances track behavioral differences across agents, supporting its use in playstyle analysis and diversity or novelty search. At the same time, significance tests are not reported, so the correlation evidence is descriptive rather than inferential in the statistical sense (Nguyen et al., 1 Aug 2025).
6. Related JSON-Bag formulations in structured output generation
Later work uses the same name for a different object of study: token usage in structured output generation rather than similarity between game trajectories. In "Token-Oriented Object Notation vs JSON: A Benchmark of Plain and Constrained Decoding Generation," the JSON-Bag model decomposes total token usage into fixed prompt overhead 5, payload-dependent lexical and structural cost, whitespace overhead, and repair-loop overhead 6. The general form is
7
with separate instantiations for plain JSON, constrained JSON, and TOON. Constrained decoding keeps JSON structure costs essentially unchanged but can reduce retries and model chatter; TOON reduces structural emissions on aligned layouts but introduces a prompt tax and, when failures occur, a repair-loop tax. Empirically, JSON-SO achieves the lowest token totals on the simple users and order cases, TOON beats plain JSON on users and order, and TOON even finishes with the lowest total tokens on the deep company case despite 0.0% one-shot accuracy, because its syntax savings outweigh repairs (Matveev, 8 Feb 2026).
A second reuse appears in "JTON: A Token-Efficient JSON Superset with Zen Grid Tabular Encoding for LLMs," where a JSON-Bag model estimates token counts for standard compact JSON versus JTON Zen Grid. Zen Grid factors shared headers into a single row and encodes rows with semicolon-delimited, comma-separated values. The paper writes token totals as 8 and 9, with the dominant savings term arising from eliminating repeated key emissions across rows. Reported token reductions are 15–60% versus JSON compact, with a 28.5% average and 32% with bare_strings; across seven real-world domains, Zen Grid yields a net +0.3 percentage-point comprehension accuracy gain over JSON and 100% syntactic validity in generation tests (Nandakishore, 7 Apr 2026).
These later formulations are related in method but distinct in purpose. The 2025 game-trajectory model uses bags of tokens as probability distributions over behavioral traces; the 2026 structured-output models use bags of tokens as accounting devices for serialization overhead, prompt tax, and structural redundancy. This suggests a terminological ambiguity: "JSON-Bag" names a general perspective on JSON token multisets, but not a single uniform task or estimator.
7. Limitations, broader applicability, and future directions
The principal limitation of the trajectory formulation is that bag-of-tokens discards within-trajectory order beyond token multiplicity. The paper notes that subtle temporal differences can still be reflected indirectly through repeated presence of early-acquired items, but full temporal structure is lost. Tokenization choices also matter: ordered versus unordered list handling is selected per game through light preliminary testing, numeric values treated as distinct tokens can fragment signal, and sparse or grid-heavy games may be under-specified by raw JSON alone (Nguyen et al., 1 Aug 2025).
Other limitations are methodological. Prototype aggregation uses a simple mean distribution; no alternative aggregators are tested. No vocabulary pruning, smoothing, or TF–IDF weighting is used. Distance comparisons are limited to JSD, cosine, and Euclidean distance; Hellinger distance and Earth Mover’s Distance are explicitly noted as untested. Domain shift is another concern: the approach is game-agnostic, but its comparability depends on serialization quality, consistency, and stable semantics of keys and values across datasets (Nguyen et al., 1 Aug 2025).
The paper proposes several concrete refinements: numeric binning, coordinate pairing for grids, token n-grams over key paths, hierarchical pooling across path prefixes, learned token embeddings, sequence-aware models such as RNNs or Transformers, and alternative divergences or optimal-transport distances. It also emphasizes broader applicability beyond the six tabletop games. Any domain with structured event logs serialized to JSON—including telemetry from digital games, process mining logs, and web or app interaction traces—can in principle adopt JSON-Bag, because key-path tokenization embeds hierarchical context directly into the token vocabulary (Nguyen et al., 1 Aug 2025).
In practical terms, the method remains deliberately lightweight. The implementation sketch is recursive tokenization of JSON, counting and normalizing per trajectory, prototype averaging per class, and nearest-prototype classification by JSD. Training cost is 0, inference is 1 per query, no hyperparameters are tuned for P-NNS, and RF is used essentially as is to demonstrate the gains obtainable from automatic feature extraction. The authors provide a repository with parameters, features, and implementation details at https://github.com/dienn1/JSONBag (Nguyen et al., 1 Aug 2025).