Export / Import JSON Schema from/to PKL classes #43

Open
opened 2025-12-30 01:19:53 +01:00 by adam · 6 comments
Owner

Originally created by @buremba on GitHub (Feb 8, 2024).

It would be nice to generate PKL classes from JSON Schema definitions. Similarly, it can go the other way around from PKL to JSON Schema. I'm aware that the typing will be lost, but since JSON Schema is integrated pretty much everywhere, it can provide interoperability for the language.

Originally created by @buremba on GitHub (Feb 8, 2024). It would be nice to generate PKL classes from JSON Schema definitions. Similarly, it can go the other way around from PKL to JSON Schema. I'm aware that the typing will be lost, but since JSON Schema is integrated pretty much everywhere, it can provide interoperability for the language.
Author
Owner

@bioball commented on GitHub (Feb 8, 2024):

Good timing; we just submitted https://github.com/apple/pkl-pantry/pull/12 that adds support for this!

@bioball commented on GitHub (Feb 8, 2024): Good timing; we just submitted https://github.com/apple/pkl-pantry/pull/12 that adds support for this!
Author
Owner

@bioball commented on GitHub (Feb 9, 2024):

That PR has landed.

You can generate Pkl schema from JSON Schema; for example:

pkl eval package://pkg.pkl-lang.org/pkl-pantry/org.json_schema.contrib@1.0.0#/generate.pkl -m . -p source="https://json.schemastore.org/github-action.json"

Note: depending on the input JSON Schema, you might need to tinker with the generated result to make it more useful. Some of JSON Schema is more expressive than Pkl; take a look at the docs for more details: https://pkl-lang.org/package-docs/pkg.pkl-lang.org/pkl-pantry/org.json_schema.contrib/current/generate/index.html

@bioball commented on GitHub (Feb 9, 2024): That PR has landed. You can generate Pkl schema from JSON Schema; for example: ``` pkl eval package://pkg.pkl-lang.org/pkl-pantry/org.json_schema.contrib@1.0.0#/generate.pkl -m . -p source="https://json.schemastore.org/github-action.json" ``` Note: depending on the input JSON Schema, you might need to tinker with the generated result to make it more useful. Some of JSON Schema is more expressive than Pkl; take a look at the docs for more details: https://pkl-lang.org/package-docs/pkg.pkl-lang.org/pkl-pantry/org.json_schema.contrib/current/generate/index.html
Author
Owner

@buremba commented on GitHub (Feb 11, 2024):

Great to hear! JSON Schema also conditional blocks i.e if/else and I assume that it's not supported yet. Not sure if it's feasible in addition to the work for anyOf/allOff but maybe something to consider.
BTW, the speed you ship the integrations today is impressive. This was something that I tried with Jsonnet a while and I couldn't manage to get it done in a seamless way. Great work!

@buremba commented on GitHub (Feb 11, 2024): Great to hear! JSON Schema also conditional blocks i.e `if/else` and I assume that it's not supported yet. Not sure if it's feasible in addition to the work for `anyOf/allOff` but maybe something to consider. BTW, the speed you ship the integrations today is impressive. This was [something that I tried](https://groups.google.com/g/jsonnet/c/4qNlvw1VGXE) with Jsonnet a while and I couldn't manage to get it done in a seamless way. Great work!
Author
Owner

@gobetti commented on GitHub (Mar 5, 2024):

generate.pkl works great and was absolutely critical for our team to start considering pkl adoption, thank you for that 👏

I see https://github.com/apple/pkl-pantry/pull/12 mentions it:

provides support for generating JSON Schema from Pkl

but I'm wondering if one is actually able to convert Pkl classes into a JSON Schema; for example:

  1. use generate.pkl to generate Pkl from a JSON Schema
  2. now de-transform the generated Pkl code back into a JSON Schema

I'm describing the scenario above just to be clear about what kind of Pkl code I'm trying to convert to a JSON Schema, in general I don't expect people to do those exact 2 steps. But something like those steps would be helpful for our project to confidently migrate from JSON Schemas to Pkl, and not have to change all the systems to Pkl at once.

Thank you!

@gobetti commented on GitHub (Mar 5, 2024): `generate.pkl` works great and was absolutely critical for our team to start considering pkl adoption, thank you for that 👏 I see https://github.com/apple/pkl-pantry/pull/12 mentions it: > provides support for generating JSON Schema from Pkl but I'm wondering if one is actually able to convert Pkl classes into a JSON Schema; for example: 1. use `generate.pkl` to generate Pkl from a JSON Schema 2. now de-transform the generated Pkl code back into a JSON Schema I'm describing the scenario above just to be clear about what kind of Pkl code I'm trying to convert to a JSON Schema, in general I don't expect people to do those exact 2 steps. But something like those steps would be helpful for our project to confidently migrate from JSON Schemas to Pkl, and not have to change all the systems to Pkl at once. Thank you!
Author
Owner

@dig commented on GitHub (Jun 4, 2024):

Allowing conversion of Pkl to JSON Schema would be useful!

@dig commented on GitHub (Jun 4, 2024): Allowing conversion of Pkl to JSON Schema would be useful!
Author
Owner

@bioball commented on GitHub (Jun 4, 2024):

We don't get have a Pkl -> JSON Schema generator, but we do have one for OpenAPI v3.1 Schema, which is mostly compatible with JSON Schema.

See the details here: https://pkl-lang.org/package-docs/pkg.pkl-lang.org/pkl-pantry/org.openapis.v3.contrib/current/SchemaGenerator/index.html#generate%28%29

For example, given:

// personSchema.pkl
import "package://pkg.pkl-lang.org/pkl-pantry/org.openapis.v3.contrib@1.0.3#/SchemaGenerator.pkl"

class Person {
  /// The person's legal name.
  name: String?
}

output = SchemaGenerator.generate(Person).output

The command pkl eval personSchema.pkl produces:

{
  "type": "object",
  "title": "Person",
  "properties": {
    "name": {
      "type": "string",
      "description": "The person's legal name.",
      "nullable": true
    }
  },
  "additionalProperties": false
}

There's a couple limitations here; see the details in pkldoc for method generate.

@bioball commented on GitHub (Jun 4, 2024): We don't get have a Pkl -> JSON Schema generator, but we do have one for OpenAPI v3.1 Schema, which is mostly compatible with JSON Schema. See the details here: https://pkl-lang.org/package-docs/pkg.pkl-lang.org/pkl-pantry/org.openapis.v3.contrib/current/SchemaGenerator/index.html#generate%28%29 For example, given: ```groovy // personSchema.pkl import "package://pkg.pkl-lang.org/pkl-pantry/org.openapis.v3.contrib@1.0.3#/SchemaGenerator.pkl" class Person { /// The person's legal name. name: String? } output = SchemaGenerator.generate(Person).output ``` The command `pkl eval personSchema.pkl` produces: ```json { "type": "object", "title": "Person", "properties": { "name": { "type": "string", "description": "The person's legal name.", "nullable": true } }, "additionalProperties": false } ``` There's a couple limitations here; see the details in pkldoc for method `generate`.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/pkl#43