1

I am developing a system where we create documents that are created from json-files. The json files are described by a json schema of the following kind, where a key either could have a static default value or have one of multiple enumerated values:

{
  "name" : { "default" : "John Doe" },
  "bilingual" : { "type" : "boolean" },
  "kind_of_document" : {
    "type" : "integer",
    "enum" : [0, 1, 2]
  }
}

What I want to do is to create test files for all possible combinations of values from the schema. In the above case there would be six different json files:

{  "name" : "John Doe", "bilingual" : true, "kind_of_document" : 0 }


{  "name" : "John Doe", "bilingual" : true, "kind_of_document" : 1 }


{  "name" : "John Doe", "bilingual" : true, "kind_of_document" : 2 }


{  "name" : "John Doe", "bilingual" : false, "kind_of_document" : 0 }


{  "name" : "John Doe", "bilingual" : false, "kind_of_document" : 1 }


{  "name" : "John Doe", "bilingual" : false, "kind_of_document" : 2 }

This is a simplified version; in a normal case there may be 20-30 keys, of which most have default values, but there may still 5-10 keys that can have multiple values. They number of keys may vary from one document to another.

The problem: How do I iterate over the list of keys to generate all possible combinations of values? I suppose that some form of recursion is the way to go, but I can't figure out what the algorithm should be. It seems to me that this should be a fairly common problem, so somebody has most likely solved it before.

I have tried googling for it, but I don't know how to formulate the query to avoid just general questions about generating combinations of list elements, such as

How to get all possible combinations of a list’s elements?

I have also looked at itertools, but I am not sure how it could me solve the problem: https://docs.python.org/3/library/itertools.html#itertools.permutations

I am writing this in python, but of course the general algorithm for solving this would be language independent.

NiklasR
  • 473
  • 6
  • 19
  • > I don't know how to formulate the query to avoid just general questions about generating combinations of list elements That's what programming is -- adapting a general algorithm to your specific usecase. You're not going to find the literal code that exactly matches what you need. You'll want to use a few nested loops to generate all the combinations. – Ether Mar 08 '22 at 17:55
  • 1
    I don't think that you can get all permutations for a schema in general. But I have a library (.net) that can generate random data for a schema. You can also try it online at https://json-everything.net/json-schema. Input your schema and hit `Generate`. There are links on the site to the library and docs. – gregsdennis Mar 08 '22 at 19:17

0 Answers0