admin管理员组

文章数量:1312783

  1. Process JSON files containing $ref references that may point to local definitions or external files.

  2. Resolve all references recursively, ensuring every $ref points to an existing definition.

  3. Keep $ref references intact instead of replacing them with full object data.

  4. If a reference points to an external file (e.g., "references.json"), that file should also be processed recursively.

  5. Generate a _validated.json output where all necessary definitions are placed inside $defs.

Example Input: main_request_original.json

{
  "$schema": ";,
  "title": "Main Schema",
  "type": "object",
  "properties": {
    "localReference": { "$ref": "#/$defs/Person" },
    "fileReference": { "$ref": "references.json" },
    "fileSubReference": { "$ref": "references.json#/definitions/Address" }
  },
  "$defs": {
    "Person": {
      "type": "object",
      "properties": {
        "name": { "type": "string" },
        "age": { "type": "integer" }
      }
    }
  }
}

Example Input: references.json

{
  "definitions": {
    "Address": {
      "type": "object",
      "properties": {
        "street": { "type": "string" },
        "city": { "type": "string" }
      }
    },
    "User": {
      "type": "object",
      "properties": {
        "id": { "type": "integer" },
        "contact": { "$ref": "#/definitions/Address" }
      }
    }
  }
}

Expected Output: main_request_original_validated.json

{
  "$schema": ";,
  "title": "Main Schema",
  "type": "object",
  "properties": {
    "localReference": { "$ref": "#/$defs/Person" },
    "fileReference": { "$ref": "references.json" },
    "fileSubReference": { "$ref": "#/$defs/Address" }
  },
  "$defs": {
    "Person": {
      "type": "object",
      "properties": {
        "name": { "type": "string" },
        "age": { "type": "integer" }
      }
    },
    "Address": {
      "type": "object",
      "properties": {
        "street": { "type": "string" },
        "city": { "type": "string" }
      }
    },
    "User": {
      "type": "object",
      "properties": {
        "id": { "type": "integer" },
        "contact": { "$ref": "#/$defs/Address" }
      }
    }
  }
}

As you can see the expected output should look something like above. I am having a hard time figuring out exactly how to get to this point. I am using Jackson for traversing the json.

I thought this task would be much simpler than it is turning out to be. I want to know if this feasible and if there is an easier way to go about this?

本文标签: Resolving ref References in JSON Using Kotlin and JacksonStack Overflow