首页 > 解决方案 > Remove specific properties from JSON object according to JSON Schema

问题描述

JSON Schema:

{
   "title": "Amenities",
   "additionalProperties": false,
   "properties": {
      "Footer": {
         "type": "string",
         "editType": "textarea"
      },
      "RowType": {
         "type": "integer",
         "editType": null
      },
      "answers": {
            "type": "array",
            "items": {
                "type": "object",
                "properties": {
                    "answer": {
                        "type": "integer",
                        "editType": null
                    },
                    "FooterInner": {
                     "type": "string",
                     "editType": "textarea"
                  }
                }
            }
        }
   },
   "type": "object"
}

JSON Object:

{
   "Footer": "",
   "RowType": 0,
   "answers": [
      {
         "answer": 1,
         "FooterInner": "innerfooter"
      },
      {
         "answer": 2,
         "FooterInner": "innerfooter2"
      }
   ]
}

I need to find properties with "type=integer" in JSON Schema and remove those properties from JSON Object.

Expected JSON Object is:

{
   "Footer": "",
   "answers": [
      {
         "FooterInner": "innerfooter"
      },
      {
         "FooterInner": "innerfooter2"
      }
   ]
}

JSON Schema and JSON Objects may differ, so I need to validate and remove "type=integer" properties from any type of JSON Object.

I have searched and could not find something useful, and the main problem is there can be multiple nested elements in JSON.

Might be I need to write recursive iterating function, is there any existing solution?

标签: c#jsonjson.netjsonschema

解决方案


Here is the solution:

Instead of searching "type=integer" properties in schema I did it in JSON object.

But, firstly I validated JSON object against the JSON schema to be sure that there is no any additional property in JSON object.

1.Step - Validating JSON Object against JSON Schema:

JsonValue loadedSchema = JsonValue.Parse(jsonSchema);
var schema = JsonSchemaFactory.FromJson(loadedSchema);
JsonValue loadedObject = JsonValue.Parse(json);
var schemaValidationResult = schema.Validate(loadedObject);

If 1.Step is OK then execute 2.Step

2.Step - Remove Integer, Boolean and Float type properties from JSON Object:

static void Main(string[] args)
{
    var json =
        @"{
           ""Footer"": ""footer"",
                ""RowType"": 4,
                ""answers"": 
                [
                    {
                        ""answer"": 1,
                        ""FooterInner"": ""innerfooter""
                    },
                    {
                        ""answer"": 2,
                        ""FooterInner"": ""innerfooter2""
                    }
                ]
            }";
    JToken nodeList = JToken.Parse(json);
    List<JTokenType> typesToRemove = new List<JTokenType>(){JTokenType.Boolean, JTokenType.Float, JTokenType.Integer};

    removeFields(nodeList, typesToRemove);

    Console.WriteLine(nodeList.ToString());
    Console.ReadKey();
}

private static void removeFields(JToken token, List<JTokenType> typesToRemove)
{
    JContainer container = token as JContainer;
    if (container == null) return;

    List<JToken> removeList = new List<JToken>();
    foreach (JToken el in container.Children())
    {
        JProperty p = el as JProperty;
        if (p != null && typesToRemove.Contains(p.Value.Type))
        {
            removeList.Add(el);
        }

        removeFields(el, typesToRemove);
    }

    foreach (JToken el in removeList)
    {
        el.Remove();
    }
}

推荐阅读