-
Notifications
You must be signed in to change notification settings - Fork 160
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Not an issue] How to create schema to generate a JSON Array using Schema and transformers provided by DataFaker API in Java? #720
Comments
could you please share the code to make the discussion more fruitful? |
Hi @amodmahajan2016 , I think the documentation on this is a little out of date, apologies for that. I think something like this should work:
Alternatively, in Java:
Hope this helps! |
Thanks, it worked. But how to create JSON Array at field level then? Something as below - [
{
"firstName": "Oleta",
"lastName": "Toy",
"skills" : [{
"lang": "some"
},
{
"lang": "boo"
}]
}
] Will "Format" be removed soon as that has more clear and easy syntax? |
just add compositeField("skills", new Field[] {field("lang", () -> faker.nation().language())}) full code snippet @Test
void test() {
var faker = new Faker();
Schema<String, String> schema =
Schema.of(
field("firstName", () -> faker.name().firstName()),
field("lastName", () -> faker.name().lastName()),
compositeField("skills", new Field[] {field("lang", () -> faker.nation().language())})
);
JsonTransformer<String> transformer = JsonTransformer.<String>builder().formattedAs(JSON_ARRAY).build();
String json = transformer.generate(schema, 10);
System.out.println(json);
} |
it's already removed in main if we are talking about json |
compositeField is generating nested JSON Object as below. My requirement is to generate a key with JSON ARRAY value. [
{
"Text": "Elicia",
"Bool": false,
"skills": {
"lang": "Hindi"
}
},
{
"Text": "Sheldon",
"Bool": false,
"skills": {
"lang": "Arabic"
}
}
] I tried as below and it generates like "ints": [1, 2, 3]. I tried putting Fields instead of Integers but it is not working. |
ok, in this case it will be a bit different void test() {
var faker = new Faker();
Schema<String, ?> schema =
Schema.of(
field("firstName", () -> faker.name().firstName()),
field("lastName", () -> faker.name().lastName()),
field("skills", () ->
List.of(compositeField(null,
new Field[]{field("lang", () -> faker.nation().language())})
)
)
);
JsonTransformer<String> transformer = JsonTransformer.<String>builder().formattedAs(JSON_ARRAY).build();
String json = transformer.generate(schema, 10);
System.out.println(json);
} |
It is giving output (not correct) as below which is similar to when I used new Object[] - {
"firstName": "Darryl",
"lastName": "Champlin",
"skills": [
"net.datafaker.transformations.CompositeField@22101c80"
]
} |
for me it outputs [
{"firstName": "Tiffiny", "lastName": "Casper", "skills": [{"lang": "Zulu"}]},
{"firstName": "Lorena", "lastName": "Dibbert", "skills": [{"lang": "Japanese"}]},
{"firstName": "Jamal", "lastName": "Dibbert", "skills": [{"lang": "Russian"}]},
{"firstName": "Mario", "lastName": "Prohaska", "skills": [{"lang": "Korean"}]},
{"firstName": "Agustina", "lastName": "Pfeffer", "skills": [{"lang": "Hakka"}]},
{"firstName": "Edra", "lastName": "Watsica", "skills": [{"lang": "English"}]},
{"firstName": "Clarice", "lastName": "Ryan", "skills": [{"lang": "Marathi"}]},
{"firstName": "Edra", "lastName": "Schneider", "skills": [{"lang": "Kazakh"}]},
{"firstName": "Marlyn", "lastName": "Luettgen", "skills": [{"lang": "Japanese"}]},
{"firstName": "Art", "lastName": "Wisozk", "skills": [{"lang": "Hindi"}]}
] are you sure you are running exactly the same code as i put in comment above? |
ok i see,
that is probably the reason |
Thanks @snuyanzin, @erikpragt-connectid. Waiting eagerly for the next release. This feature is really awesome and unfortunately many are aware of this. This will be great for API automation testing. I will post some blogs/videos on these concepts. |
I am using version 1.9.0 currently, but I experience the same behaviour as @amodmahajan2016. The #693 seems to be merged. What could be wrong? Has it been released yet? |
it's merged to 2.x (main branch) |
Aha that makes sense. Also a merge into 1.x would be nice, since the 2.x is only available as SNAPSHOT version. I will make a remark in the original bug with a question if it can be merged into 1.x as well. |
Well, 2.0 is the future release, we will release a 2.0 version soon. |
merged to 1.x as 8ad4e56 |
closing since it was merged to both 1.x and 2.x |
Sorry to ask here but I am doubtful if I can get a solution on Stack overflow.
I am using DataFaker library to create schema for JSON Object and Array. Since the use of Format is deprecated, I am trying with Transformation Schemas. I tried examples given under the Format section of official documentation to create a schema for Json Array but wasn't successful. Example JSON Array -
[ { "firstName": "Oleta", "lastName": "Toy" }, { "firstName": "Gerard", "lastName": "Windler" } ]
The text was updated successfully, but these errors were encountered: