C
C#4mo 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
SWEETPONY4mo 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
Pobiega4mo ago
any reason you are not serializing to a known type in the first place?
SWEETPONY
SWEETPONY4mo ago
json contains unknown structure for me, I just need to find property that has generateModelClassName and do some work with properties
Pobiega
Pobiega4mo 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
reflectronic4mo ago
do not deseiralize to JsonDocument deseiralize directly to JsonObject instead
Want results from more Discord servers?
Add your server
More Posts
EF Core - Runtime-Based "OR" "AND" Comparison in Where()I am allowing users on a web application to create filters on a queried table. It will generate an a✅ Inconsistent accessibility: property type is less accessible than property.Hey! I'm making a UWP app, but from my understanding this is universal to C#. I have 2 different UWP✅ Code issue?I am not sure what the problem is (the tutorial i seen had the same code) can some one help me with ✅ Android Emulator in MAUI is not connecting to webAPIHey there! I am trying to get data using web API but this exception raises everytime: [0:] ExceptionIssue with generic interfaces (covariance/contravariance)Hello! Currently, I have these interfaces within my project: ```c# public interface IResult<out TREntity Framework 6 error - 'Value cannot be null. Parameter name: entitySet'This piece of code is throwing the above error: ```using (var dbContext = new ApplicationDbContext(ASP.NET Core - Database Model and ViewModel ValidationWhat is the point of writing Validation attributes outside of the EF Core Data Model (for example in✅ how to create and insert images that represent program data into excel fileI am building a desktop app that has a feature to create an excel report. i used open-source librar✅ Get Gmail inbox using C#Is there a way to get my Gmail emails from my inbox using C#? I've taken a look at multiple results ✅ Windows Forms ProblemI have a Problem it only shows "<" then the COmputer name but nothing else ```cs using System; using