❔ Convert nested JObjects into Dictionary<string, object>
What I want is like the title, to convert any json given by the user into a
Dictionary<string, object>
but keep the JSON structure. Especially if it's nested.
So I figured I'd start recursively, in a sense, but I am getting stuck on the point where I attack the child tokens, since they are JToken and not JObject. And I'm not sure if they will keep working if I use OfType<JObject>()
. So I could use some help converting a JObject that a user inputs to a Dictionary that keeps the JSON structure.
Here's what I've got so far:
but as you can see I'm not doing anything with childTokens at the moment because I'm not sure how to keep the original JSON structure
The reason I am trying to convert it, is because I have to do two of these mappings, and then map user-given keys of the one JSON object to the other (the values)
So this part:
Please halp with my recursive function to map any json into a dictionary
And afterwards, but this is optional for me, a query question23 Replies
Seems like this would be simpler and nicer to just parse into strongly typed class objects. Is there a reason you don't want to go that route?
because the user is the one that provides a JSON but I never know what the JSON might look like.
The idea is to map two JSON together.
JObj_A
has properties, which need to be mapped into JObj_B
and then the API returns JObj_B
with the new property values.
The user gives me 4 inputs
The PropsToMap is of typeJObj_A
JObj_B
QueryString
(optional, and not for now)PropsToMap
PropsToMap[]
I don't know ahead of time what JObj_A
or B look like, so I cannot make pre-declared classes and types for them
hence the conversion to DIctionariesah okay, makes sense
Do you have some idea in mind how this dictionary would look?
😅 I know, it's a bit messy, but it can be made clean if I do it right, but I am kind of stuck of recursively parsing JObjects into Dictionaries
(note,
JObj_A
and JObj_B
are both of type JObject
)
I know NewtonSoft.Json has this method JObj_A.ToObject<Dictionary<string, object>>()
but that does not add the nested objects, it just stays first layer deep
So this dictionary could look anything from: (coming up_
all the way up to:
The idea is that the JSON objects come from a seperate NoSQL database, or perhaps even an SQL database, adding all colums into JSON properties.
I don't care where the user gets their data for A and B from. All I am supposed to do is map the properties to the right properties and return the B object 😅just use the JObject
it has more info than a dictionary
it can be mapped easily
it avoids extra allocations ob the dictionaries
How do I get the keys (and thus the values of these keys) in a JObject?
can I just like Dicts use
JObjectItem[keyString]
?Properties
or something like thatWell what I mean is. let's say you have a parent in the dictionary. Then a child in the dictionary. How would each of those look? Something like:
you can index jobject
with the key
so like this:
? where
data
is the following class:
can I use a query-string on the JObject too?
because I can't really convert a SQL query string into Linq, or url querystring to linq 😅idk what you meab by query string, but the above will work fine with jobjects
if you want a filter system, you need to implement that yourself
Ah, right... it works, as long as my
prop.From
or prop.To
is single layer deep. As soon as I have to go deeper into an object, I have to index it by [indexKey][indexKey2]...
or use something like OData
well yeah, fix up the logic
introduce another while loop
but you can index with a nested key like "a.b.c" I think
introduce another while loop??? huh?
that might be easier
well if that property list contains property paths, aka a->b->c, then you need to follow the path
right
Either I ask the user to give a property key as if it's a path:
key1/key2/...
or I make a while loop to look for said key in the object, but then I have the chance that there are multiple instances of said property...
but even with path I have to create some new logic to get to said propertyyour From and To have to be either delimited by some character strings of keys, or lists of keys
either way, if you need your logic to be multilevel, you need to represent a complex path somehow
I think they will have to be delimited as a path, so with
/
. Because I expect the end-user to understand to path the property keys, and not really how to create a list of the properties leading up to the key they wantmultiple instances?
there's no such thing in json
or in dictionaries for that matter
any json content can just be deserialized directly to Dictionary<string,object> without any extra work. But if you want to be able to use it, you'll want Dictionary<string,JObject>, again just deserialize to it directly, no work required
you have to write custom logic to follow the path either way
Yea, alright, I'll see if I can figure out some logic to follow the path 😅 this already feels a little tricky lol
Thanks, we've come to another solution, and working from there now 😅 I didn't know I could just use the JObject the same way. Indexing with
[]
this seems to work! 😁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.