C
C#17mo ago
HappyTeaKid

❔ Get value out of deserialized json object.

I need to get value out of meta, from the "object". As for now I also use this: Dictionary<string, object> msg = (Dictionary<string, object>)result; But since its generic Im not sure how to access the field value. Any help? These dont work: var result = data["meta"]["object"].Value<string>(); var result = data.SelectToken("meta.object").ToString(); var result = data.Descendants() .OfType<JProperty>() .FirstOrDefault(x => x.Name == "object") ?.Value; And here is the json:
{
"v":1,
"matches_filters":{
"current":[
]
},
"meta":{
"action":"updated",
"id":1,
"is_bulk_update":false,
"matches_filters":{
"current":[

]
},
"object":"organization",
"permitted_user_ids":[
125421,
12312,
123123
],
"pipedrive_service_name":false,
"timestamp":1231415,
},
"current":{
"address_route":"Mama",
"related_closed_deals_count":0
},
"event":"updated",
"retry":0
}
{
"v":1,
"matches_filters":{
"current":[
]
},
"meta":{
"action":"updated",
"id":1,
"is_bulk_update":false,
"matches_filters":{
"current":[

]
},
"object":"organization",
"permitted_user_ids":[
125421,
12312,
123123
],
"pipedrive_service_name":false,
"timestamp":1231415,
},
"current":{
"address_route":"Mama",
"related_closed_deals_count":0
},
"event":"updated",
"retry":0
}
10 Replies
MODiX
MODiX17mo ago
Ero#1111
REPL Result: Success
using System.Text.Json;
using System.Text.Json.Serialization;

public record Root(
[property: JsonPropertyName("meta")] Meta Meta);

public record Meta(
[property: JsonPropertyName("object")] string Object);

var json = """
{
"v": 1,
"matches_filters": {
"current": []
},
"meta": {
"action": "updated",
"id": 1,
"is_bulk_update": false,
"matches_filters": {
"current": []
},
"object": "organization",
"permitted_user_ids": [
125421,
12312,
123123
],
"pipedrive_service_name": false,
"timestamp": 1231415
},
"current": {
"address_route": "Mama",
"related_closed_deals_count": 0
},
"event": "updated",
"retry": 0
}
""";

var root = JsonSerializer.Deserialize<Root>(json);

root.Meta.Object
using System.Text.Json;
using System.Text.Json.Serialization;

public record Root(
[property: JsonPropertyName("meta")] Meta Meta);

public record Meta(
[property: JsonPropertyName("object")] string Object);

var json = """
{
"v": 1,
"matches_filters": {
"current": []
},
"meta": {
"action": "updated",
"id": 1,
"is_bulk_update": false,
"matches_filters": {
"current": []
},
"object": "organization",
"permitted_user_ids": [
125421,
12312,
123123
],
"pipedrive_service_name": false,
"timestamp": 1231415
},
"current": {
"address_route": "Mama",
"related_closed_deals_count": 0
},
"event": "updated",
"retry": 0
}
""";

var root = JsonSerializer.Deserialize<Root>(json);

root.Meta.Object
Result: string
organization
organization
Compile: 624.654ms | Execution: 91.993ms | React with ❌ to remove this embed.
ero
ero17mo ago
yay
Esa
Esa17mo ago
Hi, there are several ways you can go about this. System.Text.Json can be used if you want to deserialize it into a strongly typed class, but that requires you to create the class for it first. Luckily there are tools to help convert json to .cs objects. This can probably be a bit bloated if you worry about that, but it'll be consise and very readable and generally is the preferred way if the payload won't change. Another approach is to use the more generic JsonElement (also from STJ) I think they're called, where you instead deserialize this into something more generic where you can access it like you would a dictionary.
json["meta"]?["object"]
json["meta"]?["object"]
I prefer @Eros approach here, that solution is how I'd do it too
HappyTeaKid
HappyTeaKid17mo ago
Yes, it might vary depending on the request thats why it is generic. Im looking trough the other solution that have been posted here
Esa
Esa17mo ago
How may it vary? May the content of the json change, or just the values of the contents? Because if you can keep a strongly typed class I'd prefer that
ero
ero17mo ago
the content of object changes types depending on the request, then it's difficult if you know the type beforehand, because you know what it returns based on the request, a generic parameter might work
HappyTeaKid
HappyTeaKid17mo ago
got another solution string meta = (new JavaScriptSerializer()).Serialize(msg["meta"]); string obj = meta.Substring(meta.IndexOf("object")+9);
Angius
Angius17mo ago
1. Generate classes from this JSON 2. Deserialize to those classes 3. Hurray, now you're using the benefits of C#'s type system in full and you can get rid of magic strings!
Anton
Anton17mo ago
+9?
Accord
Accord17mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server
More Posts
❔ I'm confused how "x = false" can cause a NullReferenceExceptiondeclaration of _isUpdatingPeakValue is inside a public sealed class: ```C private volatile bool _is❔ Azure Speech Service wont shut upI am using azure in a WPF app, where I have two buttons, Read, and Stop The problem is, that when I❔ The remote certificate is invalid because of error in the certificate chain: partial chainWhen I debug it from VS, the request responds successfully, but when the service is deployed to anot❔ ✅ How does .NET MAUI Blazor relate to Blazor Server or WebAssembly?Or am I conflating those ideas? I see the .NET MAUI Blazor project template `index.html` has the li✅ Output not correctIm doing a Client/server program the program will give out in output if the user is a minor or not t❔ How to create an event handler for a variableI am currently developing a HTML editor that has tabs and a page that shows the output. The variable❔ Looking to turn this GUI integer calculator into a Fraction calculator using the provided classFinal project for school, need to create a GUI fraction calculator. I have a GUI integer calculator ❔ How do I publish a WebAssembly app to a singlefile executableHow do I publish a web assembly app to a selfhosted single executable file? Using these settings juJSON C#Can someone help with this code: using System; using System.Collections.Generic; using System.Compon❔ Help to close a forms from another forms C#I created 2 scripts with a timer with intervals of 1000 ms, both one is to open a new form when a ce