c# - 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?
解决方案
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();
}
}
推荐阅读
- vim - 从 autocmd 模式中排除帮助和插件页面
- html - 如何向引导程序版本 5 的容器流体类添加水平填充?
- python - TLS握手超时ssl python
- android - 网格布局未在真机 Android 中显示
- node.js - 拆分 Camunda 和 UI
- java - Heroku 错误:没有主要清单属性,在 Spring Boot 应用程序的 JAR 文件中
- json - 在 pyspark 中读取 DStrem 中的嵌套 JSON 数据
- python - 在pytorch中获取gradCam的渐变
- python - 拆分具有不同的txt文件
使用 python 进行搜索 - flutter - 通过按下按钮更改颤振未来构建器中的未来值