koxudaxi / datamodel-code-generator

Pydantic model and dataclasses.dataclass generator for easy conversion of JSON, OpenAPI, JSON Schema, and YAML data sources.
https://koxudaxi.github.io/datamodel-code-generator/
MIT License
2.79k stars 307 forks source link

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

Open zhao004 opened 5 months ago

zhao004 commented 5 months ago

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:

Additional context

errroimage

ZHider commented 4 months ago

可能是编码问题,中文默认系统编码是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.

kondera commented 4 weeks ago

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.