Skip to content

Instantly share code, notes, and snippets.

@robobario
Created May 23, 2017 22:25
Show Gist options
  • Save robobario/f47e43a09ad883d5094abbe1aa0f4470 to your computer and use it in GitHub Desktop.
Save robobario/f47e43a09ad883d5094abbe1aa0f4470 to your computer and use it in GitHub Desktop.
A script that takes a valid json file and permute garbage into it to generate test data
#!/usr/bin/env python
import sys, json, copy;
data = json.load(sys.stdin)
def permute(data):
permutations = [None, "", "wubbalub-gibberish", 1, [], {}]
if isinstance(data, list):
if len(data) > 0:
inner = [[x] for x in permute(data[0])]
permutations.extend(inner)
elif isinstance(data, dict):
for key in data:
perms = permute(data[key])
for perm in perms:
c = copy.deepcopy(data)
c[key] = perm
permutations.append(c)
c = copy.deepcopy(data)
c.pop(key, None)
permutations.append(c)
return permutations
print(len(permute(data)))
for index, permutation in enumerate(permute(data)):
with open('output-' + str(index) + '.json', 'w') as out:
json.dump(permutation, out, indent=4, sort_keys=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment