how do I access nested JSON Objects from Newtonsoft.Json (C#)? - c#

Here is my JSON file that my C# WinForm application will need to access. Based on your account name (Windows AD Login), you will have access to specific files from the application.
Based on the suggestions of other developers, I was encouraged to use Newtonsoft.Json
My question is how do I get the values for the key "file"
[
{
"accountName": "admin.lastname",
"files": [
{
"file": "department1.assistant.division.manager.js"
},
{
"file": "department1.division.manager.js"
},
{
"file": "department2.assistant.division.manager.1.js"
},
{
"file": "department2.assistant.division.manager.2.js"
},
{
"file": "department2.division.manager.js"
},
{
"file": "department3.js"
},
{
"file": "department4.assistant.division.manager.1.js"
},
{
"file": "department4.assistant.division.manager.2.js"
},
{
"file": "department4.division.manager.js"
},
{
"file": "department5.assistant.division.manager.1.js"
},
{
"file": "department5.division.manager.js"
},
{
"file": "department6.division.manager.js"
},
{
"file": "department7.division.manager.js"
},
{
"file": "department8.division.manager.js"
},
{
"file": "department9.division.manager.js"
},
{
"file": "department10.js"
},
{
"file": "department11.division.manager.js"
},
{
"file": "department12.assistant.division.manager.1.js"
},
{
"file": "department12.assistant.division.manager.2.js"
},
{
"file": "department12.assistant.division.manager.3.js"
},
{
"file": "department12.assistant.division.manager.4.js"
},
{
"file": "department12.division.manager.js"
},
{
"file": "department13.js"
}
]
},
{
"accountName": "firstname.lastname",
"files": [
{
"file": "department1.assistant.division.manager.js"
},
{
"file": "department1.division.manager.js"
},
{
"file": "department2.assistant.division.manager.1.js"
},
{
"file": "department2.assistant.division.manager.2.js"
},
{
"file": "department2.division.manager.js"
},
{
"file": "department3.js"
},
{
"file": "department4.assistant.division.manager.1.js"
},
{
"file": "department4.assistant.division.manager.2.js"
},
{
"file": "department4.division.manager.js"
},
{
"file": "department5.assistant.division.manager.1.js"
},
{
"file": "department5.division.manager.js"
},
{
"file": "department6.division.manager.js"
},
{
"file": "department7.division.manager.js"
},
{
"file": "department8.division.manager.js"
},
{
"file": "department9.division.manager.js"
},
{
"file": "department10.js"
},
{
"file": "department11.division.manager.js"
},
{
"file": "department12.assistant.division.manager.1.js"
},
{
"file": "department12.assistant.division.manager.2.js"
},
{
"file": "department12.assistant.division.manager.3.js"
},
{
"file": "department12.assistant.division.manager.4.js"
},
{
"file": "department12.division.manager.js"
},
{
"file": "department13.js"
}
]
},
{
"accountName": "jason.bourne",
"files": [
{
"file": "department1.assistant.division.manager.js"
},
{
"file": "department1.division.manager.js"
},
{
"file": "department2.assistant.division.manager.1.js"
},
{
"file": "department2.assistant.division.manager.2.js"
},
{
"file": "department2.division.manager.js"
},
{
"file": "department3.js"
},
{
"file": "department4.assistant.division.manager.1.js"
},
{
"file": "department4.assistant.division.manager.2.js"
},
{
"file": "department4.division.manager.js"
},
{
"file": "department5.assistant.division.manager.1.js"
},
{
"file": "department5.division.manager.js"
},
{
"file": "department6.division.manager.js"
},
{
"file": "department7.division.manager.js"
},
{
"file": "department8.division.manager.js"
},
{
"file": "department9.division.manager.js"
},
{
"file": "department10.js"
},
{
"file": "department11.division.manager.js"
},
{
"file": "department12.assistant.division.manager.1.js"
},
{
"file": "department12.assistant.division.manager.2.js"
},
{
"file": "department12.assistant.division.manager.3.js"
},
{
"file": "department12.assistant.division.manager.4.js"
},
{
"file": "department12.division.manager.js"
},
{
"file": "department13.js"
}
]
},
{
"accountName": "admin.bourne",
"files": [
{
"file": "department1.assistant.division.manager.js"
},
{
"file": "department1.division.manager.js"
},
{
"file": "department2.assistant.division.manager.1.js"
},
{
"file": "department2.assistant.division.manager.2.js"
},
{
"file": "department2.division.manager.js"
},
{
"file": "department3.js"
},
{
"file": "department4.assistant.division.manager.1.js"
},
{
"file": "department4.assistant.division.manager.2.js"
},
{
"file": "department4.division.manager.js"
},
{
"file": "department5.assistant.division.manager.1.js"
},
{
"file": "department5.division.manager.js"
},
{
"file": "department6.division.manager.js"
},
{
"file": "department7.division.manager.js"
},
{
"file": "department8.division.manager.js"
},
{
"file": "department9.division.manager.js"
},
{
"file": "department10.js"
},
{
"file": "department11.division.manager.js"
},
{
"file": "department12.assistant.division.manager.1.js"
},
{
"file": "department12.assistant.division.manager.2.js"
},
{
"file": "department12.assistant.division.manager.3.js"
},
{
"file": "department12.assistant.division.manager.4.js"
},
{
"file": "department12.division.manager.js"
},
{
"file": "department13.js"
}
]
}
]
Here is my C# code and what I tried so far but I am stuck
public void loadAccessControl(String fileName)
{
var locationInformation = System.Environment.CurrentDirectory + Path.DirectorySeparatorChar + fileName;
using (StreamReader file = File.OpenText(locationInformation))
using (JsonTextReader reader = new JsonTextReader(file))
{
JArray o = (JArray)JToken.ReadFrom(reader);
items = o;
MessageBox.Show(items.ToString());
}
foreach (var item in items.Children())
{
var itemProperties = item.Children<JProperty>();
var myElement = itemProperties.FirstOrDefault(x => x.Name == "accountName");
var myElementValue = myElement.Value; ////This is a JValue type
if (myElementValue.ToString().Contains(Environment.UserName))
{
MessageBox.Show("Authorized");
authorizedAccess = true;
myElement = itemProperties.FirstOrDefault(x => x.Name == "files");
myElementValue = myElement.Value; ////This is a JValue type
break;
}
}
}
As I said before, I am new to using Newtonsoft's library so if there is a better way, please let me know.

Get from Newtonsoft.Json from NuGet.
Add class File.cs:
using Newtonsoft.Json;
namespace Accounts
{
class File
{
[JsonProperty("file")]
public string Filename { get; set; }
}
}
Add class Account.cs:
using Newtonsoft.Json;
using System.Collections.Generic;
namespace Accounts
{
class Account
{
[JsonProperty("accountName")]
public string AccountName { get; set; }
[JsonProperty("files")]
public List<File> Files { get; set; }
}
}
Add class Program.cs:
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
namespace Accounts
{
class Program
{
static void Main(string[] args)
{
const string filename = "accounts.json";
List<Account> accounts = JsonConvert.DeserializeObject<List<Account>>(System.IO.File.ReadAllText(filename));
foreach (Account account in accounts)
{
Console.WriteLine(account.AccountName);
foreach (File file in account.Files)
{
Console.WriteLine(file.Filename);
}
Console.WriteLine();
}
}
}
}

You can use either of these options:
Creating custom Models
public class Account
{
public string AccountName { get; set; }
public List<FileItem> Files { get; set; }
}
public class FileItem
{
public string File { get; set; }
}
And then use:
var result = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Account>>(jsonText);
Without Creating Custom Models
var result = Newtonsoft.Json.JsonConvert.DeserializeObject<JArray>(jsonText)
.Select(a => new
{
Account = a.Value<string>("accountName"),
Files = a.Value<JArray>("files")
.Select(f => new { File = f.Value<string>("file") }).ToList()
}).ToList();

Try the bellow code to deserialize object graph. then you can query the data easily
public class AccessControlFile
{
public string file { get; set; }
}
public class RootObject
{
public string accountName { get; set; }
public List<AccessControlFile> files { get; set; }
}
class Program
{
static void Main(string[] args)
{
var jsonString = ""; // load from remote service or from a local file
var obj = JsonConvert.DeserializeObject<List<RootObject>>(jsonString);
}
}

Related

Mongodb c# - ReplaceOne create duplicate element '_t' error

I try to make an API using ASP NET CORE 7 and mongodb.
I come with a complicated problem that i simply don't understand.
I got a duplicated element name '_t' error when i get my documents after i used ReplaceOneAsync method.
I have Ship documents using polymorphism as :
using MongoDB.Bson.Serialization.Attributes;
using Shipest.Api.Models.DTO.QueriesDTO;
using Shipest.Api.Utils.ExceptionUtils;
using System.Text.Json.Serialization;
using System.Text.RegularExpressions;
namespace Shipest.Api.Models.DTO;
[BsonIgnoreExtraElements(true)]
[BsonDiscriminator(Required = true, RootClass = true)]
[BsonKnownTypes(typeof(ProbatioDTO), typeof(SpeculatorDTO), typeof(ExtractorDTO), typeof(ManusDTO))]
[JsonDerivedType(typeof(ProbatioDTO))]
[JsonDerivedType(typeof(SpeculatorDTO))]
[JsonDerivedType(typeof(ExtractorDTO))]
[JsonDerivedType(typeof(ManusDTO))]
public class BaseShipDTO
{
[BsonIgnore]
private string id;
[BsonId]
[BsonRepresentation(MongoDB.Bson.BsonType.ObjectId)]
public string Id {
get {
if (new Regex("^[0-9a-fA-F]{24}$").IsMatch(id))
return id;
else
throw new ShipestException("Invalid Id : " + id, 400);
}
set => id = value;
}
public string Name { get; set; }
public virtual PlanetGetDTO? Planet { get; set; }
public DateTime Creation_Date { get; set; }
public int Health { get; set; }
public int Max_Health { get; set; }
[BsonElement("_t")]
public ShipType Type { get; set; }
public long? Owner_Id { get; set; }
public PlanetGetDTO? Planet_Arrival { get; set; }
public DateTime? Arrival_Date { get; set; }
public int Speed { get; set; }
public int Energy { get; set; }
public int Max_Energy { get; set; }
public DateTime? Next_Scan_Date { get; set; }
public int Scan_Cost { get; set; }
public DateTime? Next_Heal_Date { get; set; }
public int Creatium_Cost { get; set; }
}
with ShipType enumeration as :
public enum ShipType
{
Manus,
Probatio,
Speculator,
Extractor
}
When i simply query my document using id, all works. Here my aggregation json used :
[
%m,
{
"$lookup": {
"from": "planets",
"localField": "Coordinates",
"foreignField": "_id",
"as": "Planets"
}
},
{
"$lookup": {
"from": "planets",
"as": "Neighbours",
"let": {
"main_x": "$Coordinates.X",
"main_y": "$Coordinates.Y"
},
"pipeline": [
{
"$match": {
"$expr": {
"$or": [
{
"$eq": [
"$_id",
{
"X": {
"$add": [ "$$main_x", 1 ]
},
"Y": {
"$add": [ "$$main_y" ]
}
}
]
},
{
"$eq": [
"$_id",
{
"X": {
"$add": [ "$$main_x", -1 ]
},
"Y": {
"$add": [ "$$main_y" ]
}
}
]
},
{
"$eq": [
"$_id",
{
"X": {
"$add": [ "$$main_x" ]
},
"Y": {
"$add": [ "$$main_y", 1 ]
}
}
]
},
{
"$eq": [
"$_id",
{
"X": {
"$add": [ "$$main_x" ]
},
"Y": {
"$add": [ "$$main_y", -1 ]
}
}
]
}
]
}
}
}
]
}
},
{
"$lookup": {
"from": "planets",
"localField": "Planet_of_Arrival_Coordinate",
"foreignField": "_id",
"as": "planet_of_arrival"
}
},
{
"$lookup": {
"from": "planets",
"as": "Neighbours_Arrival",
"let": {
"main_x": "$Planet_of_Arrival_Coordinate.X",
"main_y": "$Planet_of_Arrival_Coordinate.Y"
},
"pipeline": [
{
"$match": {
"$expr": {
"$or": [
{
"$eq": [
"$_id",
{
"X": {
"$add": [ "$$main_x", 1 ]
},
"Y": {
"$add": [ "$$main_y" ]
}
}
]
},
{
"$eq": [
"$_id",
{
"X": {
"$add": [ "$$main_x", -1 ]
},
"Y": {
"$add": [ "$$main_y" ]
}
}
]
},
{
"$eq": [
"$_id",
{
"X": {
"$add": [ "$$main_x" ]
},
"Y": {
"$add": [ "$$main_y", 1 ]
}
}
]
},
{
"$eq": [
"$_id",
{
"X": {
"$add": [ "$$main_x" ]
},
"Y": {
"$add": [ "$$main_y", -1 ]
}
}
]
}
]
}
}
}
]
}
},
{
"$addFields": {
"Planet": {
"$first": "$Planets"
},
"Planet_Arrival": {
"$first": "$planet_of_arrival"
}
}
},
{
"$addFields":
{
"Planet.Neighbours": "$Neighbours",
"Planet_Arrival.Neighbours": "$Neighbours_Arrival"
}
},
{
"$unset": [
"Coordinates",
"Planet_of_Arrival_Coordinate",
"Planets",
"planet_of_arrival",
"Neighbours_Arrival",
"Neighbours"
]
},
{
"$set":
{
"Planet_Arrival": {
"$cond": {
"if": {
"$eq": [
{
"$type": "$Planet_Arrival._id"
},
"object"
]
},
"then": "$Planet_Arrival",
"else": "$$REMOVE"
}
},
"Planet": {
"$cond": {
"if": {
"$eq": [
{
"$type": "$Planet._id"
},
"object"
]
},
"then": "$Planet",
"else": "$$REMOVE"
}
}
}
}
]
i know my aggregate can surely be simplified but i'm a beginner with these noSQL syntaxe ahah.
And of course %m is replaced in code as :
private string getAggregateWithMatch(string match)
{
return File.ReadAllText(#".\Database\JsonQueries\shipAggregate.json").Replace("%m", match);
}
public async Task<BaseShipDTO?> GetAsync(string shipId)
{
// use the json
var query = getAggregateWithMatch("{'$match': { '_id': ObjectId('" + shipId + "') }}");
var listResulted = await DataBaseUtils.DoGenericAggregate<BaseShipDTO, Ship>(query, _shipsCollection);
return listResulted.FirstOrDefault();
}
and documents that i do tests on it in mongodb is as :
{
"_id": {
"$oid": "63d10256d3d1cc30603cfaf6"
},
"_t": "Speculator",
"Coordinates": {
"X": 12,
"Y": -4
},
"Health": 600,
"Max_Health": 100000,
"Name": "Roger",
"Creation_Date": {
"$date": {
"$numberLong": "1674136353103"
}
},
"Creatium_Cost": 300,
"Energy": 100,
"Max_Energy": 100,
"Next_Scan_Date": null,
"Planet_of_Arrival_Coordinate": {
"X": 13,
"Y": -4
},
"Scan_Cost": 5,
"Speed": 600,
"Next_Heal_Date": {
"$date": {
"$numberLong": "1675610925014"
}
},
"Arrival_Date": null,
"Owner_Id": {
"$numberLong": "279937954565193729"
}
}
so when i change the Health property for exemple, and after that i use this method :
public async Task UpdateAsync(Ship ship)
{
await _shipsCollection.ReplaceOneAsync(s => s._id == ship._id, ship, new ReplaceOptions { IsUpsert = false });
}
i see no change with my document in database except my Health field (and the Health Date, which is normal):
{
"_id": {
"$oid": "63d10256d3d1cc30603cfaf6"
},
"_t": "Speculator",
"Coordinates": {
"X": 12,
"Y": -4
},
"Health": 750,
"Max_Health": 100000,
"Name": "Roger",
"Creation_Date": {
"$date": {
"$numberLong": "1674136353103"
}
},
"Creatium_Cost": 300,
"Energy": 100,
"Max_Energy": 100,
"Next_Scan_Date": null,
"Planet_of_Arrival_Coordinate": {
"X": 13,
"Y": -4
},
"Scan_Cost": 5,
"Speed": 600,
"Next_Heal_Date": {
"$date": {
"$numberLong": "1675611313544"
}
},
"Arrival_Date": null,
"Owner_Id": {
"$numberLong": "279937954565193729"
}
}
But at this point, i get my firstly presented error around _t when i try to re get it using my getAsync method :/ despite the fact that the field '_t' dont change at all.
Thanks you for any help^, sorry for my weird english (i'm french huh) and sorry if i forget useful code parts !
error message after a get on a object who got a replaceOne before
I solved it by removing _t field in my bdd model :
I replace using a model with exact same fields wich my mongo db takes :
public record Ship(
ObjectId _id,
Coordinates? Coordinates,
int Health,
int Max_Health,
string Name,
DateTime Creation_Date,
int Creatium_Cost,
int Energy,
int Max_Energy,
DateTime? Next_Scan_Date,
Coordinates? Planet_of_Arrival_Coordinate,
int Scan_Cost,
string _t,
int Speed,
DateTime? Next_Heal_Date,
DateTime? Arrival_Date,
long? Owner_Id
);
i removed the _t field from it, it cwas causing a weird dual modification on it (on from mongo driver, on from my field)

Remove itens from JSON string based on a comparison between two JSON strings - C#

Given 2 JSON strings:
[
{
"id":"BA",
"description":"BrandA",
"values":[
{
"id":"CategoryA",
"description":"CategoryA"
},
{
"id":"CategoryB",
"description":"CategoryB"
},
{
"id":"CategoryC",
"description":"CategoryC"
},
{
"id":"CategoryD",
"description":"CategoryD"
},
{
"id":"CategoryE",
"description":"CategoryE"
},
{
"id":"CategoryF",
"description":"CategoryF"
},
{
"id":"CategoryG",
"description":"CategoryG"
},
{
"id":"CategoryH",
"description":"CategoryH"
}
]
},
{
"id":"BB",
"description":"BrandB",
"values":[
{
"id":"CategoryA",
"description":"CategoryA"
},
{
"id":"CategoryB",
"description":"CategoryB"
},
{
"id":"CategoryC",
"description":"CategoryC"
}
]
}
]
AND
[
{
"id":"BA",
"description":"BrandA",
"values":[
{
"id":"CategoryA",
"description":"CategoryA"
},
{
"id":"CategoryC",
"description":"CategoryC"
}
]
},
{
"id":"BB",
"description":"BrandB",
"values":[
{
"id":"CategoryB",
"description":"CategoryB"
}
]
}
]
First one is the original. The second are the values that I want to remove from the original. So basically, if there is a match on brand and category between first and second JSON, regardless of the order of the elements, I want that match to be removed.
The expected result would be someting like this:
[
{
"id":"BA",
"description":"BrandA",
"values":[
{
"id":"CategoryB",
"description":"CategoryB"
},
{
"id":"CategoryD",
"description":"CategoryD"
},
{
"id":"CategoryE",
"description":"CategoryE"
},
{
"id":"CategoryF",
"description":"CategoryF"
},
{
"id":"CategoryG",
"description":"CategoryG"
},
{
"id":"CategoryH",
"description":"CategoryH"
}
]
},
{
"id":"BB",
"description":"BrandB",
"values":[
{
"id":"CategoryA",
"description":"CategoryA"
},
{
"id":"CategoryC",
"description":"CategoryC"
}
]
}
]
Catagory A and C in Brand A were removed as well as Category B in Brand B.
Based in some research, I was using https://github.com/wbish/jsondiffpatch.net, tried to work with it's functions, but so far I didn't manage to achieve the result I want. Also, to solve this by processing JSON direcly is not a must. If there is a simpler solution to achieve that by converting them to lists and use something like LINQ for example, it works for me as well (tried that, but didn't manage to find a way to do this comparison).
Thanks in advance.
If performance does not matter, the JsonSerializer and LINQ can be used:
Model
public class JsonModel
{
public class ValueModel
{
public string Id { get; set; }
public string Description { get; set; }
}
public string Id { get; set; }
public string Description { get; set; }
public IEnumerable<ValueModel> Values { get; set; }
}
Deserialize and LINQ
string json1Str = #"[...]";
string json2Str = #"[...]";
var opt = new System.Text.Json.JsonSerializerOptions { PropertyNameCaseInsensitive = true };
var json1 = System.Text.Json.JsonSerializer.Deserialize<List<JsonModel>>(json1Str, opt);
var json2 = System.Text.Json.JsonSerializer.Deserialize<List<JsonModel>>(json2Str, opt);
var result = json1.
Join(json2, j1 => j1.Id, j2 => j2.Id, (j1, j2) => new JsonModel
{
Id = j1.Id,
Description = j1.Description,
Values = j1.Values.Where(j1 => !j2.Values.Select(val => val.Id).Contains(j1.Id))
});
Console.WriteLine(System.Text.Json.JsonSerializer.Serialize(result, new System.Text.Json.JsonSerializerOptions { WriteIndented = true }));
}
Result
[
{
"Id": "BA",
"Description": "BrandA",
"Values": [
{
"Id": "CategoryB",
"Description": "CategoryB"
},
{
"Id": "CategoryD",
"Description": "CategoryD"
},
{
"Id": "CategoryE",
"Description": "CategoryE"
},
{
"Id": "CategoryF",
"Description": "CategoryF"
},
{
"Id": "CategoryG",
"Description": "CategoryG"
},
{
"Id": "CategoryH",
"Description": "CategoryH"
}
]
},
{
"Id": "BB",
"Description": "BrandB",
"Values": [
{
"Id": "CategoryA",
"Description": "CategoryA"
},
{
"Id": "CategoryC",
"Description": "CategoryC"
}
]
}
]

Why my search against elasticsearch using NEST and C# is not returning any document?

I'm new on elasticsearch world and I have a requirement to search for documents with a specific GUIDs. I've already figure out that to do that I need to map my GUID field as a keyword field.
There is a code already in production that is performing the indexing of the documents, but no mapping is being made. See below the actual code:
public class ProjectFileES
{
public Guid ClientId { get; set; }
public Guid ProjectId { get; set; }
public string DisplayName { get; set; }
public string FileName { get; set; }
public string Content { get; set; }
}
[...]
var _bulkDescriptor = new BulkDescriptor();
_bulkDescriptor.CreateMany<ProjectFileES>(list, (bd, q) =>
bd.Id(q.Id.ToString())
.Index($"{AppConstants.ES_IndexName}-{DateTime.UtcNow.ToString("yyyy-MM-dd")}")
.Type(AppConstants.ES_Type)
);
return await Client.BulkAsync(_bulkDescriptor);
As I read in the Elastic documentation, GUIDs are auto mapped to keyword type, as you can see below.
{
"mappings": {
"projectfilees": {
"properties": {
"clientId": {
"type": "keyword"
},
"projectId": {
"type": "keyword"
},
"displayName": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"fileName": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"content": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"id": {
"type": "keyword"
}
}
}
}
}
# Response:
{
"acknowledged" : true,
"shards_acknowledged" : true,
"index" : "roger-test3"
}
But now I'm trying to Search passing a projectId of a document which is indexed, as following:
var response = await _elastic.SearchAsync<ProjectFileES>(s => s.RequestConfiguration(c => c.DisableDirectStreaming())
.Index("*")
.Query(q =>
q.Bool(b => b
.Filter(f => f
.Term(t => t.ProjectId, projectId)
)
)
)
);
but my response object have no documents returned:
Response Object
Here is the JSON response inside the DebugInformation property:
{
"query": {
"bool": {
"filter": [
{
"term": {
"ProjectId": {
"value": "45efa5d0-8b41-11ea-871b-e3b070662e35"
}
}
}
]
}
}
}
# Response:
{
"took" : 48,
"timed_out" : false,
"_shards" : {
"total" : 373,
"successful" : 373,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 0,
"max_score" : null,
"hits" : [ ]
}
}
Any help?

Read JSON output from AppInsights in C#

I want to read AppInsights API output in C# console application.
WebClient wc = new WebClient();
wc.BaseAddress = "https://api.applicationinsights.io/v1/apps/AppInsighID/query?query=requests|where timestamp>= ago(1h)|limit 100";
wc.Headers.Add("Host", "api.applicationinsights.io");
wc.Headers.Add("x-api-key", "key");
string json = wc.DownloadString("");
JObject jsonObject = JObject.Parse(json);
//With this, i got values for Rows
var rowsObject = jsonObject["tables"][0]["rows"];
Now values are in array, under rowObject, so how to read this?
I would also like to know the best practice we should follow when reading json string
I can see data like this
{
"tables": [
{
"name": "PrimaryResult",
"columns": [
{
"name": "timestamp",
"type": "datetime"
},
{
"name": "id",
"type": "string"
},
{
"name": "source",
"type": "string"
},
{
"name": "name",
"type": "string"
},
{
"name": "url",
"type": "string"
},
{
"name": "success",
"type": "string"
},
{
"name": "resultCode",
"type": "string"
},
{
"name": "duration",
"type": "real"
},
{
"name": "performanceBucket",
"type": "string"
},
{
"name": "itemType",
"type": "string"
},
{
"name": "customDimensions",
"type": "dynamic"
},
{
"name": "customMeasurements",
"type": "dynamic"
},
{
"name": "operation_Name",
"type": "string"
},
{
"name": "operation_Id",
"type": "string"
},
{
"name": "operation_ParentId",
"type": "string"
},
{
"name": "operation_SyntheticSource",
"type": "string"
},
{
"name": "session_Id",
"type": "string"
},
{
"name": "user_Id",
"type": "string"
},
{
"name": "user_AuthenticatedId",
"type": "string"
},
{
"name": "user_AccountId",
"type": "string"
},
{
"name": "application_Version",
"type": "string"
},
{
"name": "client_Type",
"type": "string"
},
{
"name": "client_Model",
"type": "string"
},
{
"name": "client_OS",
"type": "string"
},
{
"name": "client_IP",
"type": "string"
},
{
"name": "client_City",
"type": "string"
},
{
"name": "client_StateOrProvince",
"type": "string"
},
{
"name": "client_CountryOrRegion",
"type": "string"
},
{
"name": "client_Browser",
"type": "string"
},
{
"name": "cloud_RoleName",
"type": "string"
},
{
"name": "cloud_RoleInstance",
"type": "string"
},
{
"name": "appId",
"type": "string"
},
{
"name": "appName",
"type": "string"
},
{
"name": "iKey",
"type": "string"
},
{
"name": "sdkVersion",
"type": "string"
},
{
"name": "itemId",
"type": "string"
},
{
"name": "itemCount",
"type": "int"
}
],
"rows": [
[
"2020-01-16T07:07:35.8423912Z",
"ID",
"",
"POST ",
"https://",
"True",
"200",
57.679,
"<250ms",
"request",
"{\"Product Name\":\"Name\",\"Subscription Name\":\"Name\",\"Operation Name\":\"AdvancedSearch\",\"ApimanagementRegion\":\"Region\",\"ApimanagementServiceName\":\"Name\",\"Apim Request Id\":\"ID\",\"Request-Body\":\"{\\\"P1\\\":25,\\\"P2\\\":1,\\\"P3\\\":\\\"All \\\",\\\"P4\\\":\\\"Earliest\\\",\\\"P5\\\":\\\"Extended\\\",\\\"P6\\\":\\\"All \\\",\\\"P6\\\":\\\"Latest\\\",\\\"queryList\\\":[{\\\"P7\\\":\\\"physics\\\",\\\"P8\\\":\\\"A1\\\",\\\"operator\\\":\\\"\\\"}]}\",\"Cache\":\"None\",\"P9\":\"195.43.22.145\",\"API Name\":\"Name\",\"HTTP Method\":\"POST\"}",
"{\"Response Size\":776,\"Request Size\":1092,\"Client Time (in ms)\":0}",
"POST ",
"ID",
"ID",
"",
"",
"",
"1",
"",
"",
"PC",
"",
"",
"0.0.0.0",
"Milan",
"Milan",
"Italy",
"",
"Value1",
"Value2",
"ID1",
"AppInsight Name",
"Name",
"apim:0.12.885.0",
"ID",
1
]
]
}
]
}
You could deserialize the Json and fetch the Rows information. For example,
var result = JsonConvert.DeserializeObject<RootObject>(str);
var rowData = result.tables.SelectMany(x=>x.rows.SelectMany(c=>c));
If you do not want to Flatten the results, you could use
var rowData = result.tables.SelectMany(x=>x.rows.Select(c=>c));
Where RootObject is defined as
public class Column
{
public string name { get; set; }
public string type { get; set; }
}
public class Table
{
public string name { get; set; }
public List<Column> columns { get; set; }
public List<List<string>> rows { get; set; }
}
public class RootObject
{
public List<Table> tables { get; set; }
}
If you intend to eliminate empty values, you could filter them out using Linq
var rowData = result.tables.SelectMany(x=>x.rows.SelectMany(c=>c))
.Where(x=>!string.IsNullOrEmpty(x));
Or for non-Flatten results
var rowData = result.tables.SelectMany(x=>x.rows.Select(c=>c))
.Where(x=>!string.IsNullOrEmpty(x.ToString()));
Update
Based on comment, to retrieve the information and parse it to a Dto based on the position of values in array, you could do the following. The attribute could be used for handling inline Json Properties as well, as mentioned in the comment.
You could begin by defining an attribute as following.
public class DtoDefinitionAttribute:Attribute
{
public DtoDefinitionAttribute(int order)=>Order = order;
public DtoDefinitionAttribute(int order,bool isJson,Type jsonDataType)
{
Order = order;
JsonDataType = jsonDataType;
IsJson = isJson;
}
public bool IsJson{get;} = false;
public int Order{get;}
public Type JsonDataType {get;}
}
And, then you could decorate your Dto properties with the index of corresponding value in the array. Additionally, in case of Json string expected as Json, then you could use the attribute to indicate it as well, as shown in the ClientTime property For example,
public class Dto
{
[DtoDefinition(0)]
public DateTime CurrentDate{get;set;}
[DtoDefinition(1)]
public string ID{get;set;}
[DtoDefinition(2)]
public string Url{get;set;}
[DtoDefinition(11,true,typeof(Response))]
public Response Json1{get;set;}
}
public class Response
{
[JsonProperty("Response Size")]
public string ResponseSize{get;set;}
[JsonProperty("Request Size")]
public string RequestSize{get;set;}
[JsonProperty("Client Time (in ms)")]
public int ClientTime{get;set;}
}
Now you could use the rowData result obtained using
var listDto = new List<Dto>();
foreach(var row in rowData)
{
listDto.Add(AssignValues(row));
}
Where AssignValues are defined as
public Dto AssignValues(List<string> row)
{
var dto = new Dto();
var properties = typeof(Dto).GetProperties().Where(x=>x.GetCustomAttributes<DtoDefinitionAttribute>().Any());
foreach(var property in properties)
{
var attribute = property.GetCustomAttribute<DtoDefinitionAttribute>();
if(attribute.IsJson)
{
var jsonData = row[attribute.Order].ToString();
var deserializedData = JsonConvert.DeserializeObject(jsonData,attribute.JsonDataType);
property.SetValue(dto,deserializedData);
}
else
{
property.SetValue(dto,Convert.ChangeType(row[attribute.Order],property.PropertyType));
}
}
return dto;
}
The AssignValues method uses reflection to read the Attributes and create an instance of Dto based on it. In case, it finds the attribute defines it as Json, then it would deserialize the json value and use the result.
Demo Code

Using Newtonsoft.Json.Schema is there a way to intercept the generation of the "definitions" name?

Given I have the following type:
public class MyClass
{
public MyGenericClass<bool> Gen { get; set; }
}
public class MyGenericClass<T>
{
[Required]
public T Prop { get; set; }
}
and I set up the schema generator like so
var generator = new JSchemaGenerator
{
ContractResolver = new CamelCasePropertyNamesContractResolver(),
SchemaIdGenerationHandling = SchemaIdGenerationHandling.None
};
generator.GenerationProviders.Add(
new StringEnumGenerationProvider { CamelCaseText = true });
I get the following schema
{
"definitions": {
"MyGenericClass<Boolean>": {
"type": [
"object",
"null"
],
"properties": {
"prop": {
"type": "boolean"
}
},
"required": [
"prop"
]
}
},
"type": "object",
"properties": {
"gen": {
"$ref": "#/definitions/MyGenericClass<Boolean>"
}
},
"required": [
"gen"
]
}
Which is great except that the service I need to share the schema with does not like the angle brackets '<>'.
Is there a way to intercept the creation of the definition name so I can strip out the <>? Note none of the other SchemaIdGenerationHandling work either.
Annoyingly something like
public class GenericSchemaProvider : JSchemaGenerationProvider
{
public override bool CanGenerateSchema(JSchemaTypeGenerationContext context)
{
return context.ObjectType.IsGenericType && !context.ObjectType.IsAssignableFrom(typeof(IEnumerable));
}
public override JSchema GetSchema(JSchemaTypeGenerationContext context)
{
return CanGenerateSchema(context) ? CreateSchemaWithCustomId(context, context.Required) : null;
}
private static JSchema CreateSchemaWithCustomId(JSchemaTypeGenerationContext context, Required required)
{
var schema = context.Generator.Generate(context.ObjectType, required != Required.Always);
schema.Id = new Uri(TypeName(context.ObjectType), UriKind.Relative);
return schema;
}
private static string TypeName(Type type, string acc = "")
{
var name = type.Name;
var index = name.IndexOf('`');
return index == -1 ? acc+name : TypeName(type.GetGenericArguments()[0],acc+name.Substring(0, index));
}
}
almost works
{
"definitions": {
"MyGenericClassNullableDouble": {
"$id": "MyGenericClassNullableDouble",
"type": [
"object",
"null"
],
"properties": {
"prop": {
"type": "number"
}
},
"required": [
"prop"
]
},
"MyGenericClassDouble": {
"$id": "MyGenericClassDouble",
"type": [
"object",
"null"
],
"properties": {
"prop": {
"type": "number"
}
},
"required": [
"prop"
]
},
"MyGenericClassBoolean": {
"$id": "MyGenericClassBoolean",
"type": [
"object",
"null"
],
"properties": {
"prop": {
"type": "boolean"
}
},
"required": [
"prop"
]
}
},
"type": "object",
"properties": {
"genBool": {
"$ref": "MyGenericClassBoolean"
},
"genDouble": {
"$ref": "MyGenericClassDouble"
},
"genNulableDouble": {
"$ref": "MyGenericClassNullableDouble"
}
}
}
But the references are not proper references to definitions.
I believe you are on the right track, I think you just need to provide your own implementation of the JSchemaGenerationProvider, instead of using StringEnumGenerationProvider. Something like creating a custom provider:
public class FormatSchemaProvider : JSchemaGenerationProvider
{
public override JSchema GetSchema(JSchemaTypeGenerationContext context)
{
// customize the generated schema for these types to have a format
if (context.ObjectType == typeof(MyGenericClass<bool>))
{
return CreateSchemaWithFormat(
context.ObjectType, context.Required, "MyGenericClass");
}
// use default schema generation for all other types
return null;
}
private JSchema CreateSchemaWithFormat(Type type, Required required, string format)
{
JSchemaGenerator generator = new JSchemaGenerator();
JSchema schema = generator.Generate(type, required != Required.Always);
schema.Format = format;
return schema;
}
}
Then add the new generation provider:
JSchemaGenerator generator = new JSchemaGenerator();
generator.GenerationProviders.Add(new FormatSchemaProvider());
JSchema schema = generator.Generate(typeof(MyClass));

Categories

Resources