Shuffle JSON Data

Shuffle JSON Data

shuffle-json-data skill for programming & development

Category: development Source: github

An AI skill that randomizes element order within JSON arrays and generates varied test data permutations, helping developers test applications with unpredictable data ordering and avoid hidden position dependencies.

What Is This?

Overview

This skill takes JSON data and shuffles it according to specified rules. It can randomize array element order, permute object key sequences, shuffle nested structures at multiple levels, and produce multiple distinct variations from a single source. The output remains valid JSON with identical data, just in a different arrangement. This is essential for revealing bugs caused by implicit ordering assumptions in application logic.

Who Should Use This

Useful for QA engineers preparing test datasets, backend developers validating API responses, and frontend developers testing list rendering regardless of data ordering.

Why Use It?

Problems It Solves

Many applications accidentally depend on data arriving in a specific order. APIs return arrays sorted by creation time, database queries have implicit ordering, and tests pass only because the data happens to be arranged conveniently. These hidden dependencies cause subtle production bugs when data ordering changes unexpectedly.

Core Highlights

  • Array Shuffling randomizes element order within JSON arrays using Fisher-Yates algorithm
  • Deep Shuffling processes nested arrays and objects at configurable depth levels
  • Batch Permutations generates multiple shuffled versions from a single source file
  • Seed Support allows reproducible shuffles for consistent test environments
  • Structure Preservation maintains all data types and values while changing only order

How to Use It?

Basic Usage

Pass a JSON file or object and receive a shuffled version with randomized ordering.

// Original JSON
const data = {
  users: [
    { id: 1, name: "Alice", role: "admin" },
    { id: 2, name: "Bob", role: "editor" },
    { id: 3, name: "Charlie", role: "viewer" },
    { id: 4, name: "Diana", role: "editor" },
  ],
};

// After shuffle
const shuffled = {
  users: [
    { id: 3, name: "Charlie", role: "viewer" },
    { id: 1, name: "Alice", role: "admin" },
    { id: 4, name: "Diana", role: "editor" },
    { id: 2, name: "Bob", role: "editor" },
  ],
};

Real-World Examples

API Response Testing

A team discovered that their React dashboard only rendered correctly when the API returned users sorted by ID. After shuffling the mock API response, several components broke because they assumed the first item was always the most recently created.

import json
import random

def shuffle_json_arrays(data, seed=None):
    if seed is not None:
        random.seed(seed)
    if isinstance(data, list):
        items = [shuffle_json_arrays(item, seed=None) for item in data]
        random.shuffle(items)
        return items
    if isinstance(data, dict):
        return {k: shuffle_json_arrays(v, seed=None) for k, v in data.items()}
    return data

with open("test_fixtures/users.json") as f:
    original = json.load(f)

for i in range(5):
    variant = shuffle_json_arrays(original, seed=i)
    with open(f"test_fixtures/users_v{i}.json", "w") as f:
        json.dump(variant, f, indent=2)

Advanced Tips

Generate shuffled variants as part of your CI pipeline to catch ordering bugs automatically. Use seeded shuffles for debugging so you can reproduce the exact arrangement that triggered a failure. Combine with property based testing to verify all permutations.

When to Use It?

Use Cases

  • Test Data Preparation create varied test fixtures from a single source file
  • API Mock Variation ensure frontend handles any response ordering
  • Database Migration Testing verify queries work regardless of row order
  • Rendering Verification confirm UI components sort data themselves
  • Fuzz Testing generate randomized inputs for robustness testing

Related Topics

When working with JSON shuffling, these prompts activate the skill:

  • "Shuffle this JSON array"
  • "Randomize the order of elements in this data"
  • "Generate shuffled test fixtures"
  • "Create permutations of this JSON file"

Important Notes

Requirements

  • Input must be valid JSON format for the skill to process
  • Works with arrays, objects, and nested structures of any depth
  • Seed values must be integers for reproducible shuffles
  • Output file format matches input structure exactly

Usage Recommendations

Do:

  • Use seeded shuffles in tests so failures are reproducible
  • Shuffle at multiple nesting levels to catch deep ordering dependencies
  • Generate several variants to increase coverage of different arrangements
  • Run shuffled data through your full test suite not just individual units

Don't:

  • Shuffle data that has intentional ordering like sorted leaderboards
  • Use random seeds in production as shuffle is for testing environments
  • Ignore failures from shuffled data since they reveal real bugs
  • Shuffle extremely large files in memory as streaming is better for big datasets

Limitations

  • Cannot shuffle data that depends on positional relationships like matrix coordinates
  • Very large JSON files may require streaming approaches rather than in-memory shuffling
  • Object key ordering is not guaranteed by the JSON specification anyway
  • Shuffling alone does not cover all data variation scenarios like missing fields