C
C#10mo ago
Sticky

✅ Filter Deserialized JSON data

My issue is that i want to filter a food list API's json data by the date and i am quite new to Newtonsoft.Json library and API in general. I can leave a snippet of the food list too .
32 Replies
Sticky
StickyOP10mo ago
[{"title":"01.02.2024","date":"01-02-2024","date2":"1 \u015eubat 2024 Per\u015fembe","type":"","lunch_menu":"\u003Cp\u003EDOMATES \u00c7ORBASI(150)\u003C\/p\u003E\u003Cp\u003EELBASAN TAVA(220)\u003C\/p\u003E\u003Cp\u003ETEL \u015eEHR\u0130YEL\u0130 P\u0130R\u0130N\u00c7 P\u0130LAVI(190)\u003C\/p\u003E\u003Cp\u003EAYRAN(70)\u003C\/p\u003E\u003Cp\u003EB\u0130SK\u00dcV\u0130 TATLISI(135)\u003C\/p\u003E\u003Cp\u003ESALATA(170)SANDV\u0130\u00c7 B\u00dcFES\u0130(370)\u003C\/p\u003E","lunch2_menu":"\u003Cp\u003EDOMATES \u00c7ORBASI(150)\u003C\/p\u003E\u003Cp\u003EV. SEBZEL\u0130 MEVS\u0130M T\u00dcRL\u00dc(120)\u003C\/p\u003E\u003Cp\u003ETEL \u015eEHR\u0130YEL\u0130 P\u0130R\u0130N\u00c7 P\u0130LAVI(190)\u003C\/p\u003E\u003Cp\u003EMEYVE SUYU(60)\u003C\/p\u003E\u003Cp\u003EMEVS\u0130M SALATA(35)\u003C\/p\u003E","dinner_menu":"\u003Cp\u003ESEBZE \u00c7ORBASI(110)\u003C\/p\u003E\u003Cp\u003ESOSLU IZGARA K\u00d6FTE(ELMA D\u0130L\u0130M PAT.)(310)\u003C\/p\u003E\u003Cp\u003ESADE MAKARNA(190)\u003C\/p\u003E\u003Cp\u003EAYRAN(70)\u003C\/p\u003E\u003Cp\u003EMEYVE(60)\u003C\/p\u003E\u003Cp\u003ESALATA(170)SANDV\u0130\u00c7 B\u00dcFES\u0130(370)\u003C\/p\u003E","dinner2_menu":"\u003Cp\u003ESEBZE \u00c7ORBASI(110)\u003C\/p\u003E\u003Cp\u003EV.PATATES YEME\u011e\u0130(180)\u003C\/p\u003E\u003Cp\u003ESADE MAKARNA(190)\u003C\/p\u003E\u003Cp\u003EMEYVE(60)\u003C\/p\u003E\u003Cp\u003EMEYVE SUYU(60)\u003C\/p\u003E"},{"title":"02.02.2024","date":"02-02-2024","date2":"2 \u015eubat 2024 Cuma","type":"","lunch_menu":"\u003Cp\u003EYAYLA \u00c7ORBASI(130)\u003C\/p\u003E\u003Cp\u003ETAVUK \u00c7\u00d6P \u015e\u0130\u015e(ISPANAKLI GARN\u0130)(220)\u003C\/p\u003E\u003Cp\u003EBULGUR P\u0130LAVI(140)\u003C\/p\u003E\u003Cp\u003EAYRAN(70)\u003C\/p\u003E\u003Cp\u003EKARI\u015eIK TUR\u015eU(80)\u003C\/p\u003E","lunch2_menu":"\u003Cp\u003EMERC\u0130MEK \u00c7ORBASI(140)\u003C\/p\u003E\u003Cp\u003EV.PIRASA YEME\u011e\u0130(90)\u003C\/p\u003E\u003Cp\u003EBULGUR P\u0130LAVI(140)\u003C\/p\u003E\u003Cp\u003EMEYVE(60)\u003C\/p\u003E\u003Cp\u003EKARI\u015eIK TUR\u015eU(65)\u003C\/p\u003E","dinner_menu":"\u003Cp\u003EMERC\u0130MEK \u00c7ORBASI(140)\u003C\/p\u003E\u003Cp\u003ESOSLU BURGER K\u00d6FTE( PATATES P\u00dcRE)(190)\u003C\/p\u003E\u003Cp\u003EER\u0130\u015eTE(190)\u003C\/p\u003E\u003Cp\u003EAYRAN(70)\u003C\/p\u003E\u003Cp\u003EHAVU\u00c7 TARATOR(65)\u003C\/p\u003E","dinner2_menu":"\u003Cp\u003EMERC\u0130MEK \u00c7ORBASI(140)\u003C\/p\u003E\u003Cp\u003EV.TAZE FASULYE(120)\u003C\/p\u003E\u003Cp\u003EP\u0130R\u0130N\u00c7 P\u0130LAVI(190)\u003C\/p\u003E\u003Cp\u003EMEYVE SUYU(60)\u003C\/p\u003E\u003Cp\u003EMEVS\u0130M SALATA(35)\u003C\/p\u003E"},
// etc
[{"title":"01.02.2024","date":"01-02-2024","date2":"1 \u015eubat 2024 Per\u015fembe","type":"","lunch_menu":"\u003Cp\u003EDOMATES \u00c7ORBASI(150)\u003C\/p\u003E\u003Cp\u003EELBASAN TAVA(220)\u003C\/p\u003E\u003Cp\u003ETEL \u015eEHR\u0130YEL\u0130 P\u0130R\u0130N\u00c7 P\u0130LAVI(190)\u003C\/p\u003E\u003Cp\u003EAYRAN(70)\u003C\/p\u003E\u003Cp\u003EB\u0130SK\u00dcV\u0130 TATLISI(135)\u003C\/p\u003E\u003Cp\u003ESALATA(170)SANDV\u0130\u00c7 B\u00dcFES\u0130(370)\u003C\/p\u003E","lunch2_menu":"\u003Cp\u003EDOMATES \u00c7ORBASI(150)\u003C\/p\u003E\u003Cp\u003EV. SEBZEL\u0130 MEVS\u0130M T\u00dcRL\u00dc(120)\u003C\/p\u003E\u003Cp\u003ETEL \u015eEHR\u0130YEL\u0130 P\u0130R\u0130N\u00c7 P\u0130LAVI(190)\u003C\/p\u003E\u003Cp\u003EMEYVE SUYU(60)\u003C\/p\u003E\u003Cp\u003EMEVS\u0130M SALATA(35)\u003C\/p\u003E","dinner_menu":"\u003Cp\u003ESEBZE \u00c7ORBASI(110)\u003C\/p\u003E\u003Cp\u003ESOSLU IZGARA K\u00d6FTE(ELMA D\u0130L\u0130M PAT.)(310)\u003C\/p\u003E\u003Cp\u003ESADE MAKARNA(190)\u003C\/p\u003E\u003Cp\u003EAYRAN(70)\u003C\/p\u003E\u003Cp\u003EMEYVE(60)\u003C\/p\u003E\u003Cp\u003ESALATA(170)SANDV\u0130\u00c7 B\u00dcFES\u0130(370)\u003C\/p\u003E","dinner2_menu":"\u003Cp\u003ESEBZE \u00c7ORBASI(110)\u003C\/p\u003E\u003Cp\u003EV.PATATES YEME\u011e\u0130(180)\u003C\/p\u003E\u003Cp\u003ESADE MAKARNA(190)\u003C\/p\u003E\u003Cp\u003EMEYVE(60)\u003C\/p\u003E\u003Cp\u003EMEYVE SUYU(60)\u003C\/p\u003E"},{"title":"02.02.2024","date":"02-02-2024","date2":"2 \u015eubat 2024 Cuma","type":"","lunch_menu":"\u003Cp\u003EYAYLA \u00c7ORBASI(130)\u003C\/p\u003E\u003Cp\u003ETAVUK \u00c7\u00d6P \u015e\u0130\u015e(ISPANAKLI GARN\u0130)(220)\u003C\/p\u003E\u003Cp\u003EBULGUR P\u0130LAVI(140)\u003C\/p\u003E\u003Cp\u003EAYRAN(70)\u003C\/p\u003E\u003Cp\u003EKARI\u015eIK TUR\u015eU(80)\u003C\/p\u003E","lunch2_menu":"\u003Cp\u003EMERC\u0130MEK \u00c7ORBASI(140)\u003C\/p\u003E\u003Cp\u003EV.PIRASA YEME\u011e\u0130(90)\u003C\/p\u003E\u003Cp\u003EBULGUR P\u0130LAVI(140)\u003C\/p\u003E\u003Cp\u003EMEYVE(60)\u003C\/p\u003E\u003Cp\u003EKARI\u015eIK TUR\u015eU(65)\u003C\/p\u003E","dinner_menu":"\u003Cp\u003EMERC\u0130MEK \u00c7ORBASI(140)\u003C\/p\u003E\u003Cp\u003ESOSLU BURGER K\u00d6FTE( PATATES P\u00dcRE)(190)\u003C\/p\u003E\u003Cp\u003EER\u0130\u015eTE(190)\u003C\/p\u003E\u003Cp\u003EAYRAN(70)\u003C\/p\u003E\u003Cp\u003EHAVU\u00c7 TARATOR(65)\u003C\/p\u003E","dinner2_menu":"\u003Cp\u003EMERC\u0130MEK \u00c7ORBASI(140)\u003C\/p\u003E\u003Cp\u003EV.TAZE FASULYE(120)\u003C\/p\u003E\u003Cp\u003EP\u0130R\u0130N\u00c7 P\u0130LAVI(190)\u003C\/p\u003E\u003Cp\u003EMEYVE SUYU(60)\u003C\/p\u003E\u003Cp\u003EMEVS\u0130M SALATA(35)\u003C\/p\u003E"},
// etc
Current Code:
public class Menu
{
public string title{ get; set; }
public DateTime date { get; set; }
public DateTime date2 { get; set; }
public string type { get; set; } = string.Empty;
public string lunch_menu { get; set; }
public string lunch2_menu { get; set; }
public string dinner_menu { get; set; }
public string dinner2_menu { get; set; }
}

public class Program
{
static void Main(string[] args)
{
var dateTimeNow = DateTime.Now;
var dateTime = dateTimeNow.DayOfWeek == DayOfWeek.Saturday || dateTimeNow.DayOfWeek == DayOfWeek.Sunday
? dateTimeNow.AddDays(((int)dateTimeNow.DayOfWeek - (int)DayOfWeek.Monday) % 7) : dateTimeNow;

string url = // URL

WebClient client = new WebClient();
string strPageCode = client.DownloadString(url);

dynamic parsed = JsonConvert.DeserializeObject(strPageCode);
public class Menu
{
public string title{ get; set; }
public DateTime date { get; set; }
public DateTime date2 { get; set; }
public string type { get; set; } = string.Empty;
public string lunch_menu { get; set; }
public string lunch2_menu { get; set; }
public string dinner_menu { get; set; }
public string dinner2_menu { get; set; }
}

public class Program
{
static void Main(string[] args)
{
var dateTimeNow = DateTime.Now;
var dateTime = dateTimeNow.DayOfWeek == DayOfWeek.Saturday || dateTimeNow.DayOfWeek == DayOfWeek.Sunday
? dateTimeNow.AddDays(((int)dateTimeNow.DayOfWeek - (int)DayOfWeek.Monday) % 7) : dateTimeNow;

string url = // URL

WebClient client = new WebClient();
string strPageCode = client.DownloadString(url);

dynamic parsed = JsonConvert.DeserializeObject(strPageCode);
leowest
leowest10mo ago
1) dont use dynamic you have a model use your model what u would normally do is, you create an object that represents the json then you deserialize to that object
leowest
leowest10mo ago
Instantly parse JSON in any language | quicktype
Whether you're using C#, Swift, TypeScript, Go, C++ or other languages, quicktype generates models and helper code for quickly and safely reading JSON in your apps. Customize online with advanced options, or download a command-line tool.
leowest
leowest10mo ago
this site helps you converting json sample data into a c# class but since u did not post the whole JSON I cannot use it to show an example so I Will show a simple example of deserializing
public class Menu
{
public string Name {get;set;}
public DateTime Created {get;set;}
public int Quantity {get;set;}
}
public class Menu
{
public string Name {get;set;}
public DateTime Created {get;set;}
public int Quantity {get;set;}
}
so this is my class for the example
Sticky
StickyOP10mo ago
i dont really know where to use dynamic anyways 😛
leowest
leowest10mo ago
that's good it would only dig your own grave with it
Sticky
StickyOP10mo ago
it gives output like this, but 22 of it
No description
MODiX
MODiX10mo ago
leowest
REPL Result: Success
List<Menu> items = new List<Menu>
{
new Menu { Name = "Pasta", Created = DateTime.Now, Quantity = 1 },
new Menu { Name = "Cheese", Created = DateTime.Now, Quantity = 2 },
new Menu { Name = "Pizza", Created = DateTime.Now, Quantity = 13},
};

var json = System.Text.Json.JsonSerializer.Serialize(items);
Console.WriteLine(json);

public class Menu
{
public string Name {get;set;}
public DateTime Created {get;set;}
public int Quantity {get;set;}
}
List<Menu> items = new List<Menu>
{
new Menu { Name = "Pasta", Created = DateTime.Now, Quantity = 1 },
new Menu { Name = "Cheese", Created = DateTime.Now, Quantity = 2 },
new Menu { Name = "Pizza", Created = DateTime.Now, Quantity = 13},
};

var json = System.Text.Json.JsonSerializer.Serialize(items);
Console.WriteLine(json);

public class Menu
{
public string Name {get;set;}
public DateTime Created {get;set;}
public int Quantity {get;set;}
}
Console Output
[{"Name":"Pasta","Created":"2024-02-18T18:42:19.9720409+00:00","Quantity":1},{"Name":"Cheese","Created":"2024-02-18T18:42:19.9720724+00:00","Quantity":2},{"Name":"Pizza","Created":"2024-02-18T18:42:19.9720727+00:00","Quantity":13}]
[{"Name":"Pasta","Created":"2024-02-18T18:42:19.9720409+00:00","Quantity":1},{"Name":"Cheese","Created":"2024-02-18T18:42:19.9720724+00:00","Quantity":2},{"Name":"Pizza","Created":"2024-02-18T18:42:19.9720727+00:00","Quantity":13}]
Compile: 493.649ms | Execution: 61.997ms | React with ❌ to remove this embed.
leowest
leowest10mo ago
so this is an example of serializing using the class and deserializing is just as easy
MODiX
MODiX10mo ago
leowest
REPL Result: Success
var json = "[{\"Name\":\"Pasta\",\"Created\":\"2024-02-18T18:42:19.9720409+00:00\",\"Quantity\":1},{\"Name\":\"Cheese\",\"Created\":\"2024-02-18T18:42:19.9720724+00:00\",\"Quantity\":2},{\"Name\":\"Pizza\",\"Created\":\"2024-02-18T18:42:19.9720727+00:00\",\"Quantity\":13}]";

var items = System.Text.Json.JsonSerializer.Deserialize<List<Menu>>(json);
foreach (var item in items)
Console.WriteLine(item.Name);

public class Menu
{
public string Name {get;set;}
public DateTime Created {get;set;}
public int Quantity {get;set;}
}
var json = "[{\"Name\":\"Pasta\",\"Created\":\"2024-02-18T18:42:19.9720409+00:00\",\"Quantity\":1},{\"Name\":\"Cheese\",\"Created\":\"2024-02-18T18:42:19.9720724+00:00\",\"Quantity\":2},{\"Name\":\"Pizza\",\"Created\":\"2024-02-18T18:42:19.9720727+00:00\",\"Quantity\":13}]";

var items = System.Text.Json.JsonSerializer.Deserialize<List<Menu>>(json);
foreach (var item in items)
Console.WriteLine(item.Name);

public class Menu
{
public string Name {get;set;}
public DateTime Created {get;set;}
public int Quantity {get;set;}
}
Console Output
Pasta
Cheese
Pizza
Pasta
Cheese
Pizza
Compile: 509.795ms | Execution: 70.369ms | React with ❌ to remove this embed.
leowest
leowest10mo ago
there we go so as u can see in the last code we map the json to List<Menu> and we can then access it normally as a natural object so you could filter anything u want after u deserialize u can even partially deserialize the data
Sticky
StickyOP10mo ago
is the items in the Menu class?
leowest
leowest10mo ago
items is just the name of the variable I use to create a List<Menu> to serialize it and to deserialize it to
Sticky
StickyOP10mo ago
ah
leowest
leowest10mo ago
my Menu class is
public class Menu
{
public string Name {get;set;}
public DateTime Created {get;set;}
public int Quantity {get;set;}
}
public class Menu
{
public string Name {get;set;}
public DateTime Created {get;set;}
public int Quantity {get;set;}
}
can you post the full JSON u are trying to deserialize to the site below? $paste
MODiX
MODiX10mo ago
If your code is too long, you can post to https://paste.mod.gg/ and copy the link into chat for others to see your shared code!
Sticky
StickyOP10mo ago
leowest
leowest10mo ago
so your class would look something like this
public class Menu
{
public string Title {get;set;}
public string Date {get;set;}
public string Date2 {get;set;}
public string Type {get;set;}
[JsonPropertyName("lunch_menu")]
public string LunchMenu {get;set;}
[JsonPropertyName("lunch2_menu")]
public string LunchMenu2 {get;set;}
[JsonPropertyName("dinner_menu")]
public string DinnerMenu {get;set;}
[JsonPropertyName("dinner2_menu")]
public string DinnerMenu2 {get;set;}
}
public class Menu
{
public string Title {get;set;}
public string Date {get;set;}
public string Date2 {get;set;}
public string Type {get;set;}
[JsonPropertyName("lunch_menu")]
public string LunchMenu {get;set;}
[JsonPropertyName("lunch2_menu")]
public string LunchMenu2 {get;set;}
[JsonPropertyName("dinner_menu")]
public string DinnerMenu {get;set;}
[JsonPropertyName("dinner2_menu")]
public string DinnerMenu2 {get;set;}
}
and in json [] means a list or array so you would deserialize it to List<Menu>
Sticky
StickyOP10mo ago
oooooooh didnt know that lol
leowest
leowest10mo ago
[JsonPropertyName("lunch_menu")]
[JsonPropertyName("lunch_menu")]
is used to map json weird names to keep the c# convention naming
Sticky
StickyOP10mo ago
yea, i forgot to do that before lol totally forgot
leowest
leowest10mo ago
No description
No description
leowest
leowest10mo ago
so yeah in a json viewer this is what ur data looks like
Sticky
StickyOP10mo ago
ye my main issue is how can i filter by the date
leowest
leowest10mo ago
well you can use Linq for example so after you deserialize the data
items.Where(x = x.Date == "this other date")
items.Where(x = x.Date == "this other date")
I wonder if u can use DateTime for the first date let me try
Sticky
StickyOP10mo ago
DateTime.Now.ToString("dd-MM-yyyy"); works
leowest
leowest10mo ago
yeah but if you can change the model from string to DateTime it is much easier to handle
Sticky
StickyOP10mo ago
oh fair
leowest
leowest10mo ago
yeah no it doesnt let me you can do that if u want the menu for today
Sticky
StickyOP10mo ago
ty for your help
leowest
leowest10mo ago
yw 😉 $close
MODiX
MODiX10mo ago
Use the /close command to mark a forum thread as answered
Want results from more Discord servers?
Add your server