C
C#8mo ago
SWEETPONY

how to add new property to json?

I have following code:
var schema = File.ReadAllText("legData.json");
var result = Parse(schema);

Console.WriteLine(result);

string Parse(string jsonSchema)
{
using var document = JsonDocument.Parse(jsonSchema);
var root = document.RootElement;

if(!root.TryGetProperty("properties", out _))
return "";

var properties = root.GetProperty("properties").EnumerateObject();
var property = properties.FirstOrDefault(prop => prop.Value.TryGetProperty("generateModelClassName", out _));
var propertyProperties = property.Value.GetProperty("properties");
var formatted = $$"""{"properties": {{propertyProperties}}}""";
return formatted;
}
var schema = File.ReadAllText("legData.json");
var result = Parse(schema);

Console.WriteLine(result);

string Parse(string jsonSchema)
{
using var document = JsonDocument.Parse(jsonSchema);
var root = document.RootElement;

if(!root.TryGetProperty("properties", out _))
return "";

var properties = root.GetProperty("properties").EnumerateObject();
var property = properties.FirstOrDefault(prop => prop.Value.TryGetProperty("generateModelClassName", out _));
var propertyProperties = property.Value.GetProperty("properties");
var formatted = $$"""{"properties": {{propertyProperties}}}""";
return formatted;
}
I need to add following property to each property in propertyProperties: "additionalProperties": true but I don't understand how to do it correctly
6 Replies
SWEETPONY
SWEETPONYOP8mo ago
I tried this:
string Change(JsonElement properties)
{
var lst = new List<JsonNode>();

foreach(var prop in properties.EnumerateObject())
{
var node = JsonSerializer.SerializeToNode(prop);
node["additionalProperties"] = false;
lst.Add(node);
}

var result = JsonSerializer.Serialize(lst);
return result;
}
string Change(JsonElement properties)
{
var lst = new List<JsonNode>();

foreach(var prop in properties.EnumerateObject())
{
var node = JsonSerializer.SerializeToNode(prop);
node["additionalProperties"] = false;
lst.Add(node);
}

var result = JsonSerializer.Serialize(lst);
return result;
}
but it doesn't work correctly because of is should be this: { "properties": { "aircraft": { "type": "object", "properties": { "aircraftType": { "type": "string", "example": "77W" }, "aircraftNumber": { "type": "string", "example": "72348" } } }, not this: [ { "Value": { "type": "object", "properties": { "aircraftType": { "type": "string", "example": "77W" }, "aircraftNumber": { "type": "string", "example": "72348" } } }, "Name": "aircraft", "additionalProperties": false }, hmm this works:
string Change(JsonElement properties)
{
var newObj = new JsonObject();

foreach (var prop in properties.EnumerateObject())
{
var propObj = new JsonObject { { "additionalProperties", true } };

foreach (var p in prop.Value.EnumerateObject())
propObj.Add(p.Name, JsonSerializer.SerializeToNode(p.Value));

newObj.Add(prop.Name, propObj);
}

var result = JsonSerializer.Serialize(newObj);
return result;
}
string Change(JsonElement properties)
{
var newObj = new JsonObject();

foreach (var prop in properties.EnumerateObject())
{
var propObj = new JsonObject { { "additionalProperties", true } };

foreach (var p in prop.Value.EnumerateObject())
propObj.Add(p.Name, JsonSerializer.SerializeToNode(p.Value));

newObj.Add(prop.Name, propObj);
}

var result = JsonSerializer.Serialize(newObj);
return result;
}
but looks ugly asf
Pobiega
Pobiega8mo ago
any reason you are not serializing to a known type in the first place?
SWEETPONY
SWEETPONYOP8mo ago
json contains unknown structure for me, I just need to find property that has generateModelClassName and do some work with properties
Pobiega
Pobiega8mo ago
in that case, just identify the JsonObject you wish to add something to, and use .Add(string, JsonValue). also, your example seems... really weird as your "it should look like this" doesnt contain the property you wanted to add
reflectronic
reflectronic8mo ago
do not deseiralize to JsonDocument deseiralize directly to JsonObject instead
Want results from more Discord servers?
Add your server