# JSON Modeling

The code generator supports processing json documents directly. That means even without a schema you can easily create at the very least an initial draft of your models just from samples. If you use a directory with multiple samples the transformer will merge and flatten duplicate classes, fields and field types.

    $ xsdata --package tests.fixtures.series tests/fixtures/series/samples

Sample #1

    {
      "id": 2,
      "name": "Person of Interest",
      "type": "Scripted",
      "language": "English",
      "genres": [
        "Action",
        "Crime",
        "Science-Fiction"
      ],
      "status": "Ended",
      "runtime": 60,
      "premiered": "2011-09-22",
      "officialSite": "http://www.cbs.com/shows/person_of_interest/",
      "schedule": {
        "time": "22:00",
        "days": [
          "Tuesday"
        ]
      },
      "rating": {
        "average": 8.9
      },
      "network": {
        "id": 2,
        "name": "CBS",
        "country": {
          "name": "United States",
          "code": "US",
          "timezone": "America/New_York"
        }
      },
      "webChannel": null,
      "externals": {
        "tvrage": 28376,
        "imdb": "tt1839578"
      },
      "summary": "<p>You are being watched. The government has a secret system, a machine that spies on you every hour of every day. I know because I built it. I designed the Machine to detect acts of terror but it sees everything. Violent crimes involving ordinary people. People like you. Crimes the government considered \"irrelevant\". They wouldn't act so I decided I would. But I needed a partner. Someone with the skills to intervene. Hunted by the authorities, we work in secret. You'll never find us. But victim or perpetrator, if your number is up, we'll find you.</p>",
      "updated": 1588773151
    }

Sample #2

    {
      "id": 5,
      "url": "https://www.tvmaze.com/shows/5/true-detective",
      "name": "True Detective",
      "type": "Scripted",
      "language": "English",
      "genres": [
        "Drama",
        "Crime",
        "Thriller"
      ],
      "status": "Running",
      "runtime": 60,
      "premiered": "2014-01-12",
      "officialSite": "http://www.hbo.com/true-detective",
      "schedule": {
        "time": "21:00",
        "days": [
          "Sunday"
        ]
      },
      "rating": {
        "average": 8.3
      },
      "weight": 97,
      "network": {
        "name": "HBO",
        "country": {
          "name": "United States",
          "code": "US"
        }
      },
      "externals": {
        "tvrage": 31369,
        "thetvdb": 270633,
        "imdb": "tt2356777"
      },
      "image": {
        "medium": "https://static.tvmaze.com/uploads/images/medium_portrait/178/445621.jpg",
        "original": "https://static.tvmaze.com/uploads/images/original_untouched/178/445621.jpg"
      },
      "summary": "<p>Touch darkness and darkness touches you back. <b>True Detective</b> centers on troubled cops and the investigations that drive them to the edge. Each season features a new cast and a new case.</p>",
      "updated": 1614762066,
      "_links": {
        "self": {
          "href": "https://api.tvmaze.com/shows/5"
        },
        "previousepisode": {
          "href": "https://api.tvmaze.com/episodes/1590271"
        }
      }
    }

Output

    from dataclasses import dataclass, field
    from typing import List, Optional
    from xsdata.models.datatype import XmlDate

    @dataclass
    class Country:
        class Meta:
            name = "country"

        name: Optional[str] = field(
            default=None,
            metadata={
                "type": "Element",
                "required": True,
            },
        )
        code: Optional[str] = field(
            default=None,
            metadata={
                "type": "Element",
                "required": True,
            },
        )
        timezone: Optional[str] = field(
            default=None,
            metadata={
                "type": "Element",
            },
        )

    @dataclass
    class Externals:
        class Meta:
            name = "externals"

        tvrage: Optional[int] = field(
            default=None,
            metadata={
                "type": "Element",
                "required": True,
            },
        )
        thetvdb: Optional[int] = field(
            default=None,
            metadata={
                "type": "Element",
            },
        )
        imdb: Optional[str] = field(
            default=None,
            metadata={
                "type": "Element",
                "required": True,
            },
        )

    @dataclass
    class Image:
        class Meta:
            name = "image"

        medium: Optional[str] = field(
            default=None,
            metadata={
                "type": "Element",
                "required": True,
            },
        )
        original: Optional[str] = field(
            default=None,
            metadata={
                "type": "Element",
                "required": True,
            },
        )

    @dataclass
    class Previousepisode:
        class Meta:
            name = "previousepisode"

        href: Optional[str] = field(
            default=None,
            metadata={
                "type": "Element",
                "required": True,
            },
        )

    @dataclass
    class Rating:
        class Meta:
            name = "rating"

        average: Optional[float] = field(
            default=None,
            metadata={
                "type": "Element",
                "required": True,
            },
        )

    @dataclass
    class Schedule:
        class Meta:
            name = "schedule"

        time: Optional[str] = field(
            default=None,
            metadata={
                "type": "Element",
                "required": True,
            },
        )
        days: List[str] = field(
            default_factory=list,
            metadata={
                "type": "Element",
                "min_occurs": 1,
            },
        )

    @dataclass
    class Self:
        class Meta:
            name = "self"

        href: Optional[str] = field(
            default=None,
            metadata={
                "type": "Element",
                "required": True,
            },
        )

    @dataclass
    class Links:
        class Meta:
            name = "_links"

        self_value: Optional[Self] = field(
            default=None,
            metadata={
                "name": "self",
                "type": "Element",
                "required": True,
            },
        )
        previousepisode: Optional[Previousepisode] = field(
            default=None,
            metadata={
                "type": "Element",
                "required": True,
            },
        )

    @dataclass
    class Network:
        class Meta:
            name = "network"

        id: Optional[int] = field(
            default=None,
            metadata={
                "type": "Element",
            },
        )
        name: Optional[str] = field(
            default=None,
            metadata={
                "type": "Element",
                "required": True,
            },
        )
        country: Optional[Country] = field(
            default=None,
            metadata={
                "type": "Element",
                "required": True,
            },
        )

    @dataclass
    class Series:
        class Meta:
            name = "series"

        id: Optional[int] = field(
            default=None,
            metadata={
                "type": "Element",
                "required": True,
            },
        )
        url: Optional[str] = field(
            default=None,
            metadata={
                "type": "Element",
            },
        )
        name: Optional[str] = field(
            default=None,
            metadata={
                "type": "Element",
                "required": True,
            },
        )
        type_value: Optional[str] = field(
            default=None,
            metadata={
                "name": "type",
                "type": "Element",
                "required": True,
            },
        )
        language: Optional[str] = field(
            default=None,
            metadata={
                "type": "Element",
                "required": True,
            },
        )
        genres: List[str] = field(
            default_factory=list,
            metadata={
                "type": "Element",
                "min_occurs": 1,
            },
        )
        status: Optional[str] = field(
            default=None,
            metadata={
                "type": "Element",
                "required": True,
            },
        )
        runtime: Optional[int] = field(
            default=None,
            metadata={
                "type": "Element",
                "required": True,
            },
        )
        premiered: Optional[XmlDate] = field(
            default=None,
            metadata={
                "type": "Element",
                "required": True,
            },
        )
        official_site: Optional[str] = field(
            default=None,
            metadata={
                "name": "officialSite",
                "type": "Element",
                "required": True,
            },
        )
        schedule: Optional[Schedule] = field(
            default=None,
            metadata={
                "type": "Element",
                "required": True,
            },
        )
        rating: Optional[Rating] = field(
            default=None,
            metadata={
                "type": "Element",
                "required": True,
            },
        )
        weight: Optional[int] = field(
            default=None,
            metadata={
                "type": "Element",
            },
        )
        network: Optional[Network] = field(
            default=None,
            metadata={
                "type": "Element",
                "required": True,
            },
        )
        web_channel: Optional[object] = field(
            default=None,
            metadata={
                "name": "webChannel",
                "type": "Element",
            },
        )
        externals: Optional[Externals] = field(
            default=None,
            metadata={
                "type": "Element",
                "required": True,
            },
        )
        image: Optional[Image] = field(
            default=None,
            metadata={
                "type": "Element",
            },
        )
        summary: Optional[str] = field(
            default=None,
            metadata={
                "type": "Element",
                "required": True,
            },
        )
        updated: Optional[int] = field(
            default=None,
            metadata={
                "type": "Element",
                "required": True,
            },
        )
        links: Optional[Links] = field(
            default=None,
            metadata={
                "name": "_links",
                "type": "Element",
            },
        )