I'm fairly new to C# and a lot of the libraries, but I'm trying to deserialize a json file that has a bunch of non-dynamic fields followed by a bunch of dynamically named structures with more dynamically sized/named structures underneath them.
Example of json:
"Header": {
"date": "Aug 30 2020",
"time": "14:00:00"
},
...
Other fields that will remain static
...
// Start of fields that can grow/shrink and be added to/deleted from
"Birthday": {
"MessageIndex": 0,
"Info Array Size": 3,
"Info": {
"Date": 30,
"Month": 8,
"Year": 2020
},
},
"Time of Day": {
"MessageIndex": 1,
"Info Array Size": 3,
"Info": {
"Hour": 2,
"Minute": 0,
"Second": 0
},
},
"Height": {
"MessageIndex": 2,
"Info Array Size": 2,
"Info": {
"Feet": 6,
"Inches": 2
},
},
// Continues on for any number of messages
I'm looking to create a tool to be able to parse and document what is in this information without having to add a JsonConverter for each new thing that gets added. Instead I'm looking for something that can
parse any number of items (like "Birthday", "Time of Day", "Height") that have an unknown name and a field named "Info" underneath it with an unknown number of fields and each of those fields name is unknown at the time of running the tool.
I was looking to use JSON.net (or any other available library really), but if that doesn't work I'm fine parsing the file manually. I'd just rather avoid that if possible. Thanks!
Related
I have large number of documents (~2 million). I have to query for highest num for a given dictionary Key.
Expected output from below two documents would be '3' for given key '85642768'.
Doc1:
{
"id": "fb41ecd9-2761-41f4-87ca-aa4bf59f9d83",
"Key": "Key123",
"Dict": {
"85642766": {
"num": 1,
"str": "str1"
},
"85642767": {
"num": 1,
"str": "str2"
},
"85642768": {
"num": 2,
"str": "str3"
}
}
}
Doc2:
{
"id": "821cf017-421a-422a-b082-45e77228dca3",
"Key": "Key456",
"Dict": {
"85642766": {
"num": 1,
"str": "str1"
},
"85642767": {
"num": 2,
"str": "str2"
},
"85642768": {
"num": 3,
"str": "str3"
}
}
}
Currently I have 250K unique documents. I am downloading list of documents based on list of Key in batches of 8000. It takes 14 mins with 10000 RUs in same location.
I have an index based on Key also. Is there any better way to reduce search time?
As comment above says,you can use sql do this thing.
Please try something like this sql:
SELECT value max(c.Dict["85642768"].num) from c
Hope this can help you.
After a bunch of research I haven't been able to find any good ways to read a json file, store their values, then append a new object/array to it.
The JSON looks like
{
"Skywars": [
{
"Solo Normal": [
{
"000001": [
{
"Kills": 213,
"Deaths": 117
}
]
}
],
"Solo Insane": [
{
"000001": [
{
"Kills": 10790,
"Deaths": 7184
}
]
}
]
}
],"Bedwars": [
{
"Solo": [
{
"000001": [
{
"Kills": 0,
"Deaths": 0
}
]
}
],
"Duos": [
{
"000001": [
{
"Kills": 0,
"Deaths": 0
}
]
}
]
}
]
}
As an example, i am intending for it to go "Skywars.Solo Normal", "Skywars.Solo Insane", "Bedwars.Solo", "Bedwars.Duos" then append "000002" with new kills and deaths values.
For some reason, even after hours of searching, I can't find out how to read the kills and deaths (I've gotten close, using public Skywars[] Skywars { get;set; }. Problem is most of the examples are using JSON files that look like {"user":[{"id":1,"logins":0}]} with very little arrays & sub arrays.
To anyone who is kind enough to answer, please don't spoonfeed me code, explain how it would be done (would I need to create my own parser, etc), or if there are already any posts/links that answer my question (even though I failed to find how).
Notes -
"000001" and "000002" will be dynamic, so each time you start the program those values will be different. I just want to append after the last instance of stats saved.
Also sorry, I am still learning C# but know most of the basics and some more complex concepts, I've just never been good at storing data and using JSON. If you need anything to help else just add a comment and I'll add it.
Please see Json.NET's Modifying JSON.
This sample loads JSON, modifies JObject and JArray instances and then writes the JSON back out again.
Sample
string json = #"{
'channel': {
'title': 'Star Wars',
'link': 'http://www.starwars.com',
'description': 'Star Wars blog.',
'obsolete': 'Obsolete value',
'item': []
}
}";
JObject rss = JObject.Parse(json);
JObject channel = (JObject)rss["channel"];
channel["title"] = ((string)channel["title"]).ToUpper();
channel["description"] = ((string)channel["description"]).ToUpper();
channel.Property("obsolete").Remove();
channel.Property("description").AddAfterSelf(new JProperty("new", "New value"));
JArray item = (JArray)channel["item"];
item.Add("Item 1");
item.Add("Item 2");
Console.WriteLine(rss.ToString());
// {
// "channel": {
// "title": "STAR WARS",
// "link": "http://www.starwars.com",
// "description": "STAR WARS BLOG.",
// "new": "New value",
// "item": [
// "Item 1",
// "Item 2"
// ]
// }
// }
Sorry for the messy title, I'm not sure how I should explain it better.
The JSON string I have:
{
"Name that varies here": {
"id": 01,
"ArrayOfType": [{
"name": "a name",
"id": 01
},
{
"name": "a name",
"id": 02
}]
},
"Another name": {
"id": 02,
"ArrayOfSameType": [{
"name": "a name",
"id": 03
},
....
I dont know how I should deserialize it, especially because the Object-name varies. Basically what I'd want is a List where someObject has String name, int ID and a List.
How should I create the object and deserialize it? So far I have only deserialized arrays/objects that are all called the same. I could make a class for each name (Name that varies here.cs, Another name.cs) with the variables and then deserialize it into that class.
I have a json object and I want to get the value of :
entities > media > sizes > large > h
Is there a way to get it like XML -> Xpath method?
This is extra lines that is irrelevant to question just because of ...
{
"created_at": "Sun, 01 Jan 2012 17:05:32 +0000",
"entities": {
"user_mentions": [
{
"screen_name": "nurdanterbiyik",
"name": "nurdan",
"id": 264782080,
"id_str": "264782080",
"indices": [
0,
15
]
}
],
"media": [
{
"id": 153522253777219584,
"id_str": "153522253777219584",
"indices": [
44,
64
],
"media_url": "http://p.twimg.com/AiFrrSmCMAAdEID.jpg",
"media_url_https": "https://p.twimg.com/AiFrrSmCMAAdEID.jpg",
"url": "http://t.co/ZwHN9gvO",
"display_url": "pic.twitter.com/ZwHN9gvO",
"expanded_url": "http://twitter.com/emelkiraac/status/153522253773025280/photo/1",
"type": "photo",
"sizes": {
"large": {
"w": 536,
"h": 800,
"resize": "fit"
},
Using JSON.NET you have several ways to read data without having to deserialize your JSON text into objects. Here is a simplified example:
string json = #" {
""created_at"": ""Sun, 01 Jan 2012 17:05:32 +0000"",
""entities"": {
""media"": [{
""type"": ""photo"",
""sizes"": {
""large"": {
""w"": 536,
""h"": 800,
""resize"": ""fit""
}
}
}]
}
}
";
JObject o = JObject.Parse(json);
int h = (int)o["entities"]["media"][0]["sizes"]["large"]["h"];
int h2 = (int)o.SelectToken("entities.media[0].sizes.large.h");
I prefer to think of JSON purely as serialized objects--the first and last thing I want to do with it is to de-serialize it into the objects that I then work with.
I can't tell you the best way to do it in c#, but I've had good luck using JSON.NET
JSON.NET will also let you (quote from website) "LINQ to JSON is good for situations where you are only interested in getting values from JSON, you don't have a class to serialize or deserialize to, or the JSON is radically different from your class and you need to manually read and write from your objects. LINQ to JSON allows you to easily read, create and modify JSON in .NET.", which sounds like what you are trying to do. I've never tried it, but if all you're trying to do is get one piece of data out of that long string, then it sounds like a good alternative.
I have a Json string that comes from another project to my C# project.
I want to convert this object to Dataset (list of Datatables).
I got this error:
Additional text found in JSON string after finishing deserializing object.
This is my sample json:
{
"list":[
[
256,
181940,
635346,
0,
1,
5696109,
18350,
0,
0,
"232",
1
],
[
"id",
"Factorid",
"classid",
"hostid",
"ownerid",
"orderid",
"userid",
"LastOrder",
"NextOrder",
"note",
"used"
]
],
"list2":[
[
"ID",
"MID",
"OwnerID",
"Ord",
"Enabled",
"schemaID",
"LastUpdate",
"createDate",
"parentid",
"General"
],
[
1207054,
20,
1,
1000,
1,
40,
"2015-01-11 03:18:24.143000",
"2015-01-08 22:55:17.930000",
0,
0
],
[
1207126,
20,
1,
1000,
1,
40,
"2015-01-12 02:59:15.227000",
"2015-01-12 01:37:10.317000",
0,
0
]
]
}
It's ok and will be converted to JObject with this method:
JObject jo = JObject.Parse(jsonString);
I've searched stackoverflow but didn't find any answer that was solved.
Everybody says first define a class that maps to json object, but json comes to my project,
may have different structures which in many situations won't map to class created for it.
The question is how can I dynamically convert every json to Dataset which comes to my project?