datamodel-code-generator icon indicating copy to clipboard operation
datamodel-code-generator copied to clipboard

When generating pydantic with jsonData, an error occurred: "Invalid file format"

Open zhao004 opened this issue 1 year ago • 2 comments

Describe the bug When generating pydantic with jsonData, an error occurred: "Invalid file format"

To Reproduce

Example schema:

Used commandline:

$ datamodel-codegen  --input douyin.json --input-file-type json --output model.py

Expected behavior Invalid file format

Version:

  • OS: windows10
  • Python version: 3.11.1
  • datamodel-code-generator version: 0.25.6

Additional context

errroimage

zhao004 avatar Jun 05 '24 02:06 zhao004

可能是编码问题,中文默认系统编码是gbk,而你的json可能是utf-8。你可以尝试以gbk保存后再试。 It might be a problem with file encoding. The datamodel-codegen loads your json file with the system default encoding (GBK in simplified chinese env) and it failed. Try save it with GBK encoding and run the code-gen command again.

ZHider avatar Jul 10 '24 07:07 ZHider

I had the same problem with my json files (and docs from mongo) - I dunno why but somewhere in the code yaml file reader is used and it throws errors when encouters specific unicode. Made a gist with script I use to clean json from those characters and generate pydantic class.

kondera avatar Oct 30 '24 10:10 kondera