How to trim and extract JSON string via C# RegEx? - c#

I have a json string that I will like to grab only the array in the data node. Do I trim using RegEx or what is the best way to extract that portion of the json string?
Here is a sample:
{
"Data":[
{
"Title":"Test Item 1",
"Icon":"pdf",
"PublicationDate":"2013-05-08T18:23:18.037Z"
},
{
"Title":"Test Item 2",
"Icon":"pdf",
"PublicationDate":"2013-05-08T18:23:38.177Z"
}
],
"Count":67
}
Below is what I want to end up with:
[
{
"Title":"Test Item 1",
"Icon":"pdf",
"PublicationDate":"2013-05-08T18:23:18.037Z"
},
{
"Title":"Test Item 2",
"Icon":"pdf",
"PublicationDate":"2013-05-08T18:23:38.177Z"
}
]
How can I do this properly? Sometimes the json string already comes as I need with only the array in the data node, so the logic has to be smart enough to ignore it if it already comes in that format. The reason is because I am feeding both cases to JsonConvert.DeserializeObject<List<dynamic>>(json). Thanks for any help!

After reading your comments, it seems that this is what you need:
var jToken=JToken.Parse(data);
JArray arr;
switch(jToken.Type)
{
case JTokenType.Array:
arr=(JArray)jToken;
break;
case JTokenType.Object:
arr=(JArray)((JObject)jToken)["Data"];
break;
default:
throw new Exception();
}
var output=arr.ToString();

Related

How do I loop over this JObject?

I am able to read the json file and store it in a JObject variable. I need to check for null values and ignore any segment that has null values. I was thinking of looping through the JObject but I don't know how i can access every segment/ key in the json below.
I think we can loop like this based on another question i saw on here:
foreach (var x in example) //example is the JObject in which json file is stored
{
string name = x.Key;
JToken value = x.Value;
}
But my json doesnt have keys. How do i go in every segment and check if name, description values are not null?
My json is :
{
"lorem ipsum Segment Groups":[
{
"name":"lorem ipsum",
"description":"lorem ipsum",
"segments":[
"Not Enrolled",
"Started Enrollment – Zipcode Lookup",
"Started Enrollment – Passed Flow Step 1",
"Started Enrollment – Passed Flow Step 2"
]
},
{
"name":"lorem ipsum",
"description":"Status Description",
"segments":[
"Moving in next 30 days",
"Moving in next 90 days",
"Not Moving"
]
},
{
"name":"lorem ipsum",
"description":"Interest description",
"segments":[
"Interested - Lots of Time Reading Many Pages",
"Interested - Lots of Time Comparing Products/Prices"
]
}
]
}
Thanks
Here is an example of how to loop through your JSON:
JObject obj = JObject.Parse(json);
JArray segmentGroups = (JArray)obj.Properties().FirstOrDefault()?.Value;
if (segmentGroups != null)
{
foreach (JObject group in segmentGroups.Children<JObject>())
{
string name = (string)group["name"];
string desc = (string)group["description"];
string[] segments = group["segments"]?.ToObject<string[]>();
Console.WriteLine("name: " + (name ?? "(null)"));
Console.WriteLine("description: " + (desc ?? "(null)"));
Console.WriteLine("segments: " + (segments != null ? "\n " + string.Join("\n ", segments) : "(null)"));
Console.WriteLine();
}
}
Fiddle: https://dotnetfiddle.net/kOJzaZ
By knowing the JSON structure, you can use various types with name formatted as JXxx to read the values you want. However here there is one convenient way of selecting the tokens you want by using a json path. That way you can conveniently target the desired set of tokens and continue processing. I'm not so sure about the performance hit with that approach but you may take its trade-off (and if performance does matter, just try benchmarking it first).
The code can be simple like this:
//suppose your example is the loaded JObject
foreach (var group in example.SelectTokens("['lorem ipsum Segment Groups'][*]"))
{
var name = group["name"].Value<string>();
var desc = group["description"].Value<string>();
//check if they are null here ...
}
Note the way we escape keys or paths containing spaces by wrapping it inside ['']. If your key does not need to be escaped, you can use it directly in the path.

Extract specific data from a json file using C# [duplicate]

This question already has answers here:
Deserialize JSON with C#
(10 answers)
Closed 2 years ago.
I'm trying to extract specific data, in this case "Name" from a JSON file so that I can use it for the rest of my code later.
My current code where I load JSON:
public static void LoadJson()
{
using (StreamReader r = new StreamReader(#"C:\Users\Work\Documents\MyJson.json"))
{
string json = r.ReadToEnd();
var o = JsonConvert.DeserializeObject<JObject>(json);
var h = o.Value<JObject>("Data")
.Value<JArray>("Accounts");
Console.WriteLine(h[0]);
}
}
JSON example:
{
"Data": {
"Accounts": [
{
"Name": "owner",
"Address": "123456"
},
{
"Name": "Lerris",
"Address": "179672"
}
]
}
}
The output I get from my code right now:
JSON length is much longer than the one I posted above, but it was just to make an example.
Question:
How do I obtain and define both "Name" and "Address" value so that I can use them for the rest of my code?
I hope you guys will get what I mean. If I weren't specific enough please just say and I'll try to explain again.
See this section:
Use JsonDocument for access to data

How can I convert json format sentence to another one by using C# regex for elasticsearch?

I have very strange json problem . I need to convert UrlQueryString to another standard for elasticsearch . when you look at below you will see that 2 json . I want to change :
FROM "UrlQueryString": "checkinDate: 2020-07-01,checkoutDate: 2020-07-05,room: Y,Y,Y",
TO
"UrlQueryString": {"checkinDate":"2020-07-01","checkoutDate":"2020-07-05","room": "Y,Y,Y"}
How can I do that? I think that I need regex or another options. also I tried many times for this by using replacing and removing.
I have below string json:
{
"LogLevel": "Information",
"CorrelationId": "44160536-a1e9-4a5b-a19d-9e2323ebfa7a",
"UrlMethod": "GET",
"Data": "",
"UrlQueryString": "checkinDate: 2020-07-01,checkoutDate: 2020-07-05,room: Y,Y,Y",
"UrlPath": /xxxx",
"LogSource": "source1",
"LogDate": "2019-12-28T07:39:24.2434156Z",
"Environment": "xxxx"
}
My desired result:
{
"LogLevel": "Information",
"CorrelationId": "44160536-a1e9-4a5b-a19d-9e2323ebfa7a",
"UrlMethod": "GET",
"Data": "",
"UrlQueryString": {"checkinDate":"2020-07-01","checkoutDate":"2020-07-05","room": "Y,Y,Y"},
"UrlPath": "/xxxx",
"LogSource": "source1",
"LogDate": "2019-12-28T07:39:24.2434156Z",
"Environment": "xxxx"
}
important NOTE: Don't forget something. UrlQueryString is not static. It's exactly dynamic.
at the end of the day;
"UrlQueryString": {"Parameter2": "bird"," anyvalue": "fly"}
or
"UrlQueryString": {"Parameter2": "bird"," anyvalue": "fly","Parameter3": "bird"," anyvalue2": "fly"}
{
"LogLevel": "Information",
"CorrelationId": "44160536-a1e9-4a5b-a19d-9e2323ebfa7a",
"UrlMethod": "GET",
"Data": "",
"UrlQueryString": {"Parameter1": "KuÅŸ"," test1": "1577518755769"},
"UrlPath": "/Json/AutoCompleteHotelCallBack",
"LogSource": "xxxrwr",
"LogDate": "2019-12-28T07:39:24.2434156Z",
"Environment": "xx-1"
}
You're probably thinking too complicated.
You could easily get away with just a normal string replacement. And there would be no risk either, unless one of the values could somehow get a value that's the same as a key. Doesn't seem possible though.
Maybe not the most professional solution, but I doubt that's what you're after. Or if you are, my apologies.
So just a few string replacements on that particular row and it's good to be parsed as a json.
Examples:
.Replace("\"checkinDate: ", "{\"checkinDate:\"")
.Replace(",checkoutDate: ", "\",\"checkoutDate\":\"")
UrlQueryString value is not a valid json or something that can be converted to json with standard processes
Issues:
Its a string without curly braces {}.
Its a string with key and value representation without quotes {"key", "value"}
You can try doing Splits on , and : but that would not work as you have , in your values with possibility of an object (JArray or JObject) as a value. {"key": "val,ue"} or {"key": { "key2": "value" } }
ONLY IFF you can be sure that you will have only Keys and string literals for value in that string, you can use the following method to convert that string to a json. {"key": "value", "key2": "val,ue2"}
Convert string to an array containing keys and values in order.
Convert array to Dictionary<string,string> so JsonConvert can convert to object.
public static object ConvertStringToJson (string convertThisToJson)
{
if (string.IsNullOrEmpty(convertThisToJson))
return JsonConvert.DeserializeObject<object>(JsonConvert.SerializeObject("{}"));
string[] splitStr = convertThisToJson.Split(':');
Dictionary<string, string> dict = new Dictionary<string, string>();
string key, value;
// First element will always be Key
key = splitStr[0];
// Everything in the middle will be Value/NextKey pair (not key/Value)
for (int i = 1; i < splitStr.Count() - 1; i++)
{
// You suggested your value could have commas ... thus LastIndexOf
int indexSplit = splitStr[i].LastIndexOf(",");
value = splitStr[i].Substring(0, indexSplit);
dict.Add(key, value.Trim());
key = splitStr[i].Substring(indexSplit + 1);
}
// Last element in its entirety will be value.
value = splitStr[splitStr.Length - 1];
dict.Add(key, value.Trim());
// You can choose to do either of these.
//return JsonConvert.SerializeObject(dict); // Do change the return type if you decide to go with this.
return JsonConvert.DeserializeObject<object>(JsonConvert.SerializeObject(dict));
}
and use it like this
string str = "checkinDate: 2020-07-01,checkoutDate: 2020-07-05,room: Y,Y,Y";
object jsonObj = ConvertStringToJson(str);
Console.WriteLine(jsonObj.ToString()); // Print Json Object
Console.WriteLine(JsonConvert.SerializeObject(jsonObj)); // Print String
Output
// Json Object.
{
"checkinDate": "2020-07-01",
"checkoutDate": "2020-07-05",
"room": "Y,Y,Y"
}
// String
{"checkinDate":"2020-07-01","checkoutDate":"2020-07-05","room":"Y,Y,Y"}

To parse JSON data from text file using c# winforms

I have numbers of text files containing Json data, i want to parse all required data from all those files. I am creating C# windows app to do this task.
please help me for same, thanx a lot in advance
Here is my text file data sample:
Name: sample testname
Username: sampleXYZ
Time zone: SampleTimezone
Language: EN
Json: {
"id": 600723423551234234234,
"id_str": "600723423551234234234",
"name": "sample testname",
"screen_name": "sampleXYZ",
"location": "sample Location",
"description": "sampleDescritpin",
"url": null,
"entities": { "description": {
"urls": []
}
},
//some unwanted data in between
}
First you need to extract the JSON from your file; to do this you could do something like this:
static string ExtractJSON(string path)
{
var file = File.ReadAllText(path);
var brackets = 0;
var json = "";
foreach (var c in file)
{
if (c == '{') // if { encountered, go in a level
brackets++;
else if (c == '}') // if } encountered go out a level
{
brackets--;
if (brackets == 0)
json += c.ToString(); // get the last bracket
}
if (brackets > 0) // ignore everything that isn't within the brackets
json += c.ToString();
}
return json;
}
Then once you have your json data, use a parser like the one by NewtonSoft to parse the json

Invalid Json Primitives

Could you help me for resolving this issue. I have one asp.net application, in this i am using Javascript serializer for serializing a dataset followed by convertion to the list. That code is shown below.
JavaScriptSerializer json = new JavaScriptSerializer();
strJson = json.Serialize(aclDoc);
But, at the time of deserializing i got one ArguementException like Invalid Json Primitives with my Json value. My json value is
[{"Id":"F79BA508-F208-4C37-9904-DBB1DEDE67DB","App_Id":"ScriptFlow","Name":"New form","FriendlyName":"","Read":"Revoke","ReadRule":"a353776f-cbdc-48b7-a15b-4a2316d19b05","Update":"Grant","UpdateRule":"be30c34e-33ec-4c0a-9f09-4fd483f5f1b9","Create":"Revoke","CreateRule":"898dce4d-4709-45b6-8942-d7efb07cbd86","Delete":"Revoke","DeleteRule":"aa14d435-dec8-4ade-ad9b-830ae5ee15d0"}][{"Id":"1","Doc_Id":"858E013C-5775-4FDF-AA1E-2C84053EE39F","Name":"TextBox1","FriendlyName":"TextBox1","Read":"Grant","ReadRule":"0a2e3c0e-ad8f-4f75-9160-cfd9827ac894","Update":"Grant","UpdateRule":"ecad3cf4-104f-44dc-b815-de039f3a0396"},{"Id":"2","Doc_Id":"858E013C-5775-4FDF-AA1E-2C84053EE39F","Name":"TextBox2","FriendlyName":"TextBox2","Read":"Grant","ReadRule":"81e0e9ef-09f7-4c25-a58e-d5fdfbd4c2ba","Update":"Grant","UpdateRule":"2047f662-c881-413b-a1f9-69f15bf667fc"}]
The code for deserializing is:
JavaScriptSerializer json = new JavaScriptSerializer();
lstDoc = json.Deserialize<List<ACLDocument>>(value);
return lstDoc;
where lstDoc is a List Collection of type of my class
I got the exception like this:
Invalid JSON primitive:
{"Id":"1","Doc_Id":"858E013C-5775-4FDF-AA1E-2C84053EE39F","Name":"TextBox1","FriendlyName":"TextBox1","Read":"Grant","ReadRule":"0a2e3c0e-ad8f-4f75-9160-cfd9827ac894","Update":"Grant","UpdateRule":"ecad3cf4-104f-44dc-b815-de039f3a0396"},{"Id":"2","Doc_Id":"858E013C-5775-4FDF-AA1E-2C84053EE39F","Name":"TextBox2","FriendlyName":"TextBox2","Read":"Grant","ReadRule":"81e0e9ef-09f7-4c25-a58e-d5fdfbd4c2ba","Update":"Grant","UpdateRule":"2047f662-c881-413b-a1f9-69f15bf667fc"}].
Please help me for resolving this issue. Thanks in advance
Your input string is really a wrong JSON string. You input consist from two correct JSON strings:
[
{
"Id": "F79BA508-F208-4C37-9904-DBB1DEDE67DB",
"App_Id": "ScriptFlow",
"Name": "New form",
"FriendlyName": "",
"Read": "Revoke",
"ReadRule": "a353776f-cbdc-48b7-a15b-4a2316d19b05",
"Update": "Grant",
"UpdateRule": "be30c34e-33ec-4c0a-9f09-4fd483f5f1b9",
"Create": "Revoke",
"CreateRule": "898dce4d-4709-45b6-8942-d7efb07cbd86",
"Delete": "Revoke",
"DeleteRule": "aa14d435-dec8-4ade-ad9b-830ae5ee15d0"
}
]
and
[
{
"Id": "1",
"Doc_Id": "858E013C-5775-4FDF-AA1E-2C84053EE39F",
"Name": "TextBox1",
"FriendlyName": "TextBox1",
"Read": "Grant",
"ReadRule": "0a2e3c0e-ad8f-4f75-9160-cfd9827ac894",
"Update": "Grant",
"UpdateRule": "ecad3cf4-104f-44dc-b815-de039f3a0396"
},
{
"Id": "2",
"Doc_Id": "858E013C-5775-4FDF-AA1E-2C84053EE39F",
"Name": "TextBox2",
"FriendlyName": "TextBox2",
"Read": "Grant",
"ReadRule": "81e0e9ef-09f7-4c25-a58e-d5fdfbd4c2ba",
"Update": "Grant",
"UpdateRule": "2047f662-c881-413b-a1f9-69f15bf667fc"
}
]
but you can not concatenate two JSON strings. To say exactly what you receive after such concatenating in not more a JSON string.
I recommend you to verify JSON strings in http://www.jsonlint.com/. Just cut and paste the data which you need to verify and click "Validate" button.
To answer the question directly, since everyone thinks this is a Microsoft forum and not answering directly.
The string is sent as a 2 element array. You forgot the '[' in the beginning of the string which denotes that the containing values are an array structure.
Insert the '[' in the beginning of the string and the error should go away.
This is a useful little tool for examining your JSON objects:
http://jsonviewer.codeplex.com/
See if you have any // or commented lines in project.json
Removing this has solved the same problem for me

Categories

Resources