fgeller / json-structure

converts a json value to one that describes its structure
1 stars 0 forks source link

It would be much more useful if this created an actual JSONSchema. #1

Open jarrodhroberson opened 2 years ago

jarrodhroberson commented 2 years ago

It would be much more useful if this created an actual JSONSchema.

fgeller commented 2 years ago

thanks for the feedback @jarrodhroberson!

it was fun to add support for schema, but as i'm not a direct user i'd be keen for your feedback. here's a quick sample:

sample % cat sample.json 
{
  "productId": 1,
  "productName": "A green door",
  "price": 12.50,
  "tags": [ "home", "green" ]
}
sample % cat sample.json | json-structure -flatten -schema | jq
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "properties": {
    "price": {
      "type": "number"
    },
    "productId": {
      "type": "number"
    },
    "productName": {
      "type": "string"
    },
    "tags": {
      "type": "array",
      "contains": {
        "type": "string"
      }
    }
  }
}
jarrodhroberson commented 2 years ago

that looks like it, I can not contribute right now due to medical issues, but when I get back into coding I will definitely fork this project and contribute. I have a project that does the opposite, it takes JSON Schema and generates Go code from it. Not just structs, interfaces, but repository interfaces and stubs and text/binary/json marshall implemenations as well.

fgeller commented 2 years ago

no worries, hope you get better soon! was curious if you had a use case where you wanted to derive a schema that we could test this against. took some api responses and checked via an online schema validator, but nothing beats an actual use case ;)