Extract Values from JObject - c#

I'm trying to extract some values from a Json but I have problems with the data between [ ]
{
attrib1: ""es-BO"",
attrib2: 2,
Segment: [
{
inAttrib1: ""value1"",
inAttrib2: ""value2"",
inAttrib3: ""value3""
}]
}
for the first values I'm using:
string attrib1 = request.GetValue("attrib1").Value<string>();
.
.
.
but when I'm trying to do:
string inAttrib1 = request.GetValue("inAttrib1").Value<string>();
doesn't work...what can I do?, or exists another way to do the same

The data between (and including) [] is called an array. Before moving on it might be helpful to look at JSON's home page, specifically at the different data types available.
You need to navigate down to the Segment array, then get the first element, then that element's inAttrib1 property:
string attrib1Value = request["Segment"][0]["inAttrib1"].Value<string>();
Or alternatively:
string attrib1Value = request.SelectToken(#"Segment[0].inAttrib1").Value<string>()

Related

Passing JSON as a string in the body of the POST request

I need a small help, because I don't know how to solve the below problem.
The requirement is simple, I have to sent the JSON to the server as a string parameter. The server basing on the key finds the mapping, and generically parses the JSON to some objects. That means, that the payload can have a different values and structures, each key has its own mapping - different data structure, number of parameters and so on. So the payload shouldn't be parsed outside the endpoint logic.
I know, that the Swagger sees the payload as a JSON, not as a string, and it tries to parse the data. How can I send the JSON as a string parameter to the endpooint without parsing the parameter? I have to parse it inside of the application, because of the mentioned mappings.
Example JSON:
{
"key": "test",
"payload": "[{"IDNew":1,"NameNew":"t1","DescriptionNew":"t1d", "IntegerValueNew":1, "DecimalValueNew":123.3}]"
}
When I'm trying to send the data in Swagger, I'm getting the below results:
curl -X POST "http://localhost:5110/api/InboundData" -H "accept: */*" -H "Content-Type: application/json-patch+json" -d "{ \"key\": \"test\", \"payload\": \"[{\"IDNew\":1,\"NameNew\":\"t1\",\"DescriptionNew\":\"t1d\", \"IntegerValueNew\":1, \"DecimalValueNew\":123.3}]\"}"
{
"errors": {
"payload": [
"After parsing a value an unexpected character was encountered: I. Path 'payload', line 3, position 17."
]
},
"type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
"title": "One or more validation errors occurred.",
"status": 400,
"traceId": "|d952c89f-4e25126d8cdf3697."
}
Data model:
[Required]
[JsonProperty(Required = Required.DisallowNull)]
[MaxLength(100)]
public string Key { get; set; }
[Required]
[JsonProperty(Required = Required.DisallowNull)]
public string Payload { get; set; }
The error clearly suggests that your JSON is not correct. If we analyze the payload property:
{
"key": "test",
"payload": "[{"IDNew":1,"NameNew":"t1","DescriptionNew":"t1d", "IntegerValueNew":1, "DecimalValueNew":123.3}]"
}
It seems you are creating a string object which further contains a JSON as a string. Generally, when you pass an array, you would pass it like this.
{
"key": "test",
"payload": [
{
"IDNew": 1,
"NameNew": "t1",
"DescriptionNew": "t1d",
"IntegerValueNew": 1,
"DecimalValueNew": 123.3
}
]
}
But, since value of the payload property is not properly escaped, which is why it is not properly able to parse it as it has unexpected characters for a string value.
If you strictly want to pass a JSON Array as a string object, you need to properly escape it in order to get it working. For example below is a JSON that contains JSON as a string with properly escaped properties:
{
"key": "test",
"payload": "[{\"IDNew\":1,\"NameNew\":\"t1\",\"DescriptionNew\":\"t1d\", \"IntegerValueNew\":1, \"DecimalValueNew\":123.3}]"
}
This is how you would escape your JSON if you strictly want to pass a JSON object that further contains JSON as string.
Or, perhaps, use single quote (') instead for the nested JSON. For example below is a JSON that contains JSON as a string with a single quotes for the properties:
{
"key": "test",
"payload": "[{'IDNew':1,'NameNew':'t1','DescriptionNew':'t1d', 'IntegerValueNew':1, 'DecimalValueNew':123.3}]"
}
UPDATE
I just wanted to add a suggestion that would be less confusing and would generate an accurate output for the scenario.
It would be nice if you generate the models for your intended JSON string and serialize the model to get a JSON string then do the assignment to payload property.
var payload = new List<payloadSample1>();
payload.Add(new payloadSample1{ IDNew = 1, NameNew = "t1", DescriptionNew = "t1d" });
var payloadStr = JsonConvert.SerializeObject(payload);
// payloadStr would contain your JSON as a string.
In C#, you can also generate dynamic type objects. Use those if your JSON is constantly varying and you find it hectic to create many models for many api requests.
var payload = new List<dynamic>();
payload.Add(new { IDNew = 1, NameNew = "t1", DescriptionNew = "t1d" });
var payloadStr = JsonConvert.SerializeObject(payload);
// And even then, if you have a further JSON object to send:
var payloadParent = new { key = "test", payload = payloadStr };
// send payloadParent as your json.
This is not the cleanest approach because of many reasons one out of those would be, when there is a change in your model, you will have to manually analyze all your dynamic objects and change all the references where you are using it. But, certainly, it will reduce the confusion behind escaping and maintaining the strings.
If you are using JavaScript make the api call, then generate a proper JSON object and then stringify it to get a string.

C# How to turn String with Carriage Return symbols in to an array

I am calling some external method that returns a string like this.
"[\r\n \"0\",\r\n \"1\",\r\n \"2\"\r\n]"
How do I turn this in to an Array with the values 1,2,3 ?
Should I be trying to split/substring methods to do this, or is there some kind of built in .net method that can do this?
I have tried,
string theStringResult = Class.ExternalMethod();
theStringResult.ToArray()
The returned string appears to be a JSON array made up of strings.
The line breaks are part of a pretty print version of the JSON string which, when not escaped, would look like this...
[
"0",
"1",
"2"
]
You can use Newtonsoft's Json.Net to parse and deserialize the returned string into a strongly typed object.
string theStringResult = Class.ExternalMethod();
string[] array = JsonConver.DeserializeObject<string[]>(theStringResult);
The above should produce the desired result
Try this.
string Originalstring = "[\r\n \"0\",\r\n \"1\",\r\n \"2\"\r\n]";
string[] result = Originalstring.Split(new String[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
This should split a string that has a carriage return into an array and remove any empty entries
The method you're looking for is String.Split. It takes a single character as a parameter and returns an array of pieces split wherever that character is found.
In your case you have two characters ("\r\n") so either you split and post-process the array, or you replace the pair with a single character and then split.
In code it looks like this:
string source = "\r\n0\r\n1\r\n2\r\n";
string parts = source.Replace("\r\n", "\n").Split('\n');
The resultant array is ["0", "1", "2"].
Oh, that's a literal JSON string... that wasn't entirely clear on first viewing.
If the array is fairly consistent you could do string manipulation, but it's not ideal.
Add the NewtonSoft.Json NuGet package to your project and use the following:
string source = "[\r\n \"0\",\r\n \"1\",\r\n \"2\"\r\n]";
string[] parts = Newtonsoft.Json.JsonConvert.DeserializeObject<string[]>(source);
Same output as above.

Append data in a json file in C#

How would i keep appending data?
I have this:
{
"13232": [
"2012952"
]
}
And i want to add an another object to it, example:
{
"13232": [
"2012952"
],
"19213": [
"2016086"
]
}
This is the code i use:
JArray array = new JArray();
array.Add(Itemid);
JObject o = new JObject();
o[Userid] = array;
string json = o.ToString(Formatting.Indented);
//i know this keeps appending text but how would i append it inside the { and }?
File.AppendAllText("E:/media.json", json);
I literally have no idea how to keep adding it, but maybe someone else has?
You won't be able to use file append operations to do this. File append operations can only add text to the end, they can't insert text at some point in the middle. This makes it impossible to use file-append to keep the JSON valid.
You have two choices that I can think of:
Read the entire file into an object, add your object, and then
rewrite the entire file (poor performance)
Open the file read/write, parse through
until you get to the closing curly brace, then write the remaining
data, then write the close curly brace (not trivial)
The safest approach is read-update-rewrite (applies to JSON and XML format as they don't support appending).
Next option if you can live with invalid JSON is to simply concatenate JSON fragments with code you have and than use SupportMultipleContent in JsonReader to read fragments Read Multiple Fragments With JsonReader
If those approaches don't work and your format is fixed - find position of last ] in the file, seek stream there and write new array elements and append ]}.
I recommand you use Newtonsoft Json lib, available as a nuget package.
You can the make a model class to represent on of the json node then you can de-serialize you Json to that model and build an array containing the new element at the end to then re-serialize it to json after.
Look at this MSDN page about it: https://msdn.microsoft.com/en-us/library/bb412179(v=vs.110).aspx
Edit: Actual NewtonSoft Documentation
In steps: 1 Deserialize the collection
2: And a new class instance with listName.Add(className);
3: Reserialize the collection
My approach was to add an additional step to my string handling.
public void WriteObjectToFile(object objectToInsert)
{
var stringToSaveToFile = JsonConvert.SerializeObject(objectToInsert) + ",";
File.AppendAllText(this.path, stringToSaveToFile);
}
public List<objects> GetListFromFile()
{
var notQuiteJSONstring = File.ReadAllTest(this.path);
var treatment1 = notQuiteJSONstring.Substring(0, notQuiteJSONstring.Length -1); //remove the trailing comma
var treatment2 = "[" + treatment1 + "]"; // wrap string with brackets to deserialize as list
var list = JsonConvert.DeserializeObject<List<object>>(treatment2);
return list;
}

Smartly replace strings

I am working with JSON API. As c# doesn't accept characters like - (minus) or . (point), I had to replace each character by _ (underscore). The replacement happens when the JSON response is received as a string so that every attribute name containing a - or a . will have it replaced by a _ , then every attribute name will be the same as the attributes names in the class it will be deserialized into.
To make it clearer, here are some examples:
I recieve the following JSON : { "id": 1, "result": [ { "data": [ { "adm-pass": ""}]}
In the class I want to deserialize into I have this attribute : public String adm_pass {get; set;}
So I replace the minus with an underscore so that the NewtonSoft parser can deserialize it accordingly.
My problem is that I sometimes I get some negative integers in my JSON. So if I do the string replacement in: {"beta" : -1}, I get a parsing exception since the -1 (integer here) becomes _1 and cannot be deserialized properly and raises an exception.
Is there a way to replace the string smartly so I can avoid this error?
For example if - is followed by an int it's not replaced.
If this way does not exist, is there a solution for this kind of problems?
Newtonsoft allows you to specify the exact name of the JSON property, which it will use to serialize/deserialize.
So you should be able to do this
[JsonProperty("adm-pass")]
public String adm_pass { get; set; }
This way you are not restricted to name your properties exactly as the JSON property names. And in your case, you won't need to do a string replace.
Hope this helps.
You'll have to check that you are replacing the key and not the value, maybe by using a regex like http://regexr.com/3d471
Regex could work as wlalele suggests.
But I would create a new object like this:
Create a new object:
var sharpObj = {};
loop through the objects as properties as described here:
Iterate through object properties
for (var property in object) {
if (object.hasOwnProperty(property)) {
// do stuff
}
}
In the // do stuff section, create a property on sharpObj with the desired string replacements and set the property to the same value.
var cleanProperty = cleanPropertyName(property);
sharpObj[cleanProperty] = orginalObject[property];
Note: I assume you can figure out the cleanPropertyName() method or similar.
Stringify the object
var string = JSON.stringify(sharpObj);
You can substring to check whether the next character is an integer, this can adapt into your code easily as you already find a character, as such you could do
int a;
if(int.TryParse(adm_pass.Substring(adm_pass.IndexOf("-") + 1,1),out a))
{
//Code if next character is an int
}
else
{
adm_pass = adm_pass.Replace("-","_");
}
This kind of code can be looped until there are no remaining hyphens/minuses

using strings in c#.net

Hi How do I retrieve the number from the following string,
{"number":100,"data":[test]}
The number could be of any length.
I used the following code. but it gives and error message
strValue.Substring((strValue.IndexOf(":")+1), (strValue.IndexOf("data")));
the output comes like
100,"data":[
Thanks,
It looks like your input string is JSON. Is it? If so, you should use a proper JSON parser library like JSON.NET
As noted by Jon, your input string seems to be a JSON string which needs to be deserialized. You can write your own deserializer, or use an existing library, such as Json.NET. Here is an example:
string json = #"[
{
""Name"": ""Product 1"",
""ExpiryDate"": ""\/Date(978048000000)\/"",
""Price"": 99.95,
""Sizes"": null
},
{
""Name"": ""Product 2"",
""ExpiryDate"": ""\/Date(1248998400000)\/"",
""Price"": 12.50,
""Sizes"": null
}
]";
List<Product> products = JsonConvert.DeserializeObject<List<Product>>(json);
Your attempt is close. There are two (possibly three issues) I found.
Your string has a comma after the number you're looking for. Since your code is searching for the index of "data", your end index will end up one character too far.
The second paramter of String.Substring(int, int) is actually a length, not an end index.
Strings are immutable in C#. Because of this, none of the member functions on a string actually modify its value. Instead, they return a new value. I don't know if your code example is complete, so you may be assigning the return value of SubString to something, but if you're not, the end result is that strValue remains unchanged.
Overall, the result of your current call to string.Substring is returning 100,"data":[tes. (and as far as I can see, it's not storing the result).
Try the following code:
string justTheNumber = null;
// Make sure we get the correct ':'
int startIndex = strValue.IndexOf("\"number\":") + 9;
// Search for the ',' that comes after "number":
int endIndex = strValue.IndexOf(',', startIndex);
int length = endIndex - startIndex;
// Note, we could potentially get an ArguementOutOfRangeException here.
// You'll want to handle cases where startPosition < 0 or length < 0.
string justTheNumber = strValue.Substring(startIndex, length);
Note: This solution does not handle if "number": is the last entry in the list inside your string, but it should handle every other placement in it.
If your strings get more complex, you could try using Regular Expressions to perform your searches.
Parsing JSON spring in that way is very bad practice as everything is hardcoded. Have you though of using 3rd party library for parsing JSON strings, like Newtonsoft JSON.
I guess you needed to use IndexOf(",") istead of IndexOf("data")

Categories

Resources