C# json object with empty json array - c#

I have a returned json object that contains an empty json array
{[]}
EDIT:
How do I check this in an if statement?
string arrayData = string.Empty;
if (response.Contains("\"data\":"))
{
JToken root = JObject.Parse(response);
JToken data = root["data"];
if (data != null)
{
arrayData = data.ToString();
}
}
else
{
arrayData = response;
}
var responseDatas = JsonConvert.DeserializeObject<dynamic>(arrayData);
Here, responseDatas is now
{[]}

First, that is invalid JSON. The array should have a name, like this:
{ list: [] }
Second, you can deserialize the JSON using JSON.NET and then test the result:
public class ClassWithList
{
public List<object> list { get; set; }
}
var o = JsonConvert.DeserializeObject<ClassWithList>(json);
if (o.list != null && o.list.Count > 0)
{ }

The json is invalid (in the original question and in the accepted answer). You should include double quotes ...
{
"list": []
}

Related

How would you add a JArray into a JObject without adding a new JObject key/name?

using .NET C#, I am trying to create a new JObject from a JArray. I have a FetchData JObject that I want to return a JObject of data for data driven testing. Here is what I have so far:
public static JObject FetchData(string testMethodName)
{
using (StreamReader r = new StreamReader("PathToJsonfile"))
{
string jsonstring = r.ReadToEnd();
JObject obj = JObject.Parse(jsonstring);
JArray jsonArray = JArray.Parse(obj[testMethodName].ToString());
JObject jObject = new JObject(new JProperty("test",jsonArray));
return jObject;
}
}
I want to return a JObject of test data that pertains to the testMethod which is being run. when I run this code, jObject returns:
"test": [
{
"loginId": "testuser1",
"userCase": "verify for user"
},
{
"loginId": "testuser2",
"userCase": "verify for user"
}
]
My issue is that I only want to return the following arrays within the JObject:
{"loginId":"testuser1","userCase":"verify for user"}
I have researched for a while and cannot find a solution without adding a key to the new JObject, in this case, key being "test".
Is this even possible in C#?
I have also tried adding the JArray directly to the JObject:
JObject jObject = new JObject(new JObject(jsonArray));
but get error : System.ArgumentException: 'Can not add Newtonsoft.Json.Linq.JArray to Newtonsoft.json.Linq.JObject
I have also tried adding the arrays to the JObject like this:
for (int i = 0; i < jsonArray.Count; i++)
{
jObject[i] = jsonArray[i];
}
but get error : System.ArgumentException : Set JObject values with invalid key value: 0. Object property name expected.
fwiw this is how I am doing this is Java and it works like a charm, but I cannot figure it out in C#. Java Code:
JSONObject[] jsonObject = new JSONObject[jsonArray.length()];
for (int i = 0; i < jsonArray.length(); i++) {
jsonObject[i] = jsonArray.getJSONObject(i);
}
To get first element of array you can do next:
var json = #"{
""test"": [
{
""loginId"": ""testuser1"",
""userCase"": ""verify for user""
},
{
""loginId"": ""testuser2"",
""userCase"": ""verify for user""
}]
}";
var testMethodName = "test";
var jObject = JObject.Parse(json);
var first = (JObject)jObject[testMethodName][0]; // note that this can throw for multiple reasons
Analog of your java code would be:
// parse jObject as earlier
var jArr = (JArray)jObject[testMethodName];
var jsonObjectArr = new JObject[jArr.Count];
for (int i = 0; i < jArr.Count; i++)
{
jsonObjectArr[i] = (JObject)jArr[i];
}
Or you can use LINQ to JSON:
// parse jObject as earlier
var jsonObjectArr = jObject[testMethodName]
.Children<JObject>()
.ToArray();
Why don't you return only the first object of your array like this:
public static JToken FetchOneDatum(string testMethodName)
{
string jsonString = GetFileContent("sampleTest.txt");
JObject obj = JObject.Parse(jsonString);
JArray jsonArray = JArray.Parse(obj[testMethodName].ToString());
return jsonArray[0];
}
You can find the whole visual Studio solution here:
solution on GitHub
C# approach ;)
public class Test
{
public string loginId { get; set; }
public string userCase { get; set; }
}
public class Scenario
{
public Test[] tests { get; set; }
}
// Usage
public static Test FetchData(string testMethodName)
{
using (var reader = new StreamReader("PathToJsonfile"))
{
var json = reader.ReadToEnd();
var scenario = JsonConvert.DeserializeObject<Scenario>(json);
return scenario.tests.First();
}
}

How do you promote a JSON object into an array?

Is it possible to "promote" a specific part of a json document into an array?
For example, the input would be something like:
{
"identifier": {"a":"awesome", "b":"back", "c":"cat"}
}
suppose I wanted to promote c, and in pseudocode, we'd do something like :
var payload = "{\"identifier\": {\"a\":\"awesome\", \"b\":\"back\", \"c\":\"cat\"}}";
var payloadWithPromotedC = Promoter.ToArray(payload, "identifier.c");
The result would be something like:
{
"identifier": {"a":"awesome", "b":"back", "c":["cat"]}
}
How do we declaritively promote a certain node inside of a json document into an array?
I'm not sure what you mean by "declaratively", but following your example you could do something like this using Json.Net:
public class Promoter
{
public static string ToArray(string json, string propertyPath)
{
JToken root = JToken.Parse(json);
JToken tokenToPromote = root.SelectToken(propertyPath);
if (tokenToPromote == null)
{
throw new JsonException(propertyPath + " was not found");
}
if (tokenToPromote.Type == JTokenType.Array)
{
return json; // value is already an array so return the original JSON
}
tokenToPromote.Replace(new JArray(tokenToPromote));
return root.ToString(Formatting.None);
}
}
Working demo: https://dotnetfiddle.net/bOUOWD

Adding a root element to json

I am generating a json string using NewtosoftJson using a table to format the the json. This is a simple key value pairs list and looks like:
public class items
{
private string key = String.Empty;
private string value = String.Empty;
public string Key
{
get
{
return key;
}
set
{
if (value != key)
{
key = value;
}
}
}
public string Value
{
get
{
return value;
}
set
{
if (value != this.value)
{
this.value = value;
}
}
}
}
When a list is populated and then serialised I get this JSON:
"Items": [
{
"Key":"FirstValue",
"Value":"One"
},
{
"Key":"SecondValue",
"Value":"Two"
},
{
"Key":"ThirdValue",
"Value":"Three"
}
]
What I need to get is:
"customData": {
"items": [
{
"Key":"FirstValue",
"Value":"One"
},
{
"Key":"SecondValue",
"Value":"Two"
},
{
"Key":"ThirdValue",
"Value":"Three"
}
]
}
I have tried creating a second class CustomData but can't see how to get the original JSON into the second class! Could you advice me on the correct way to construct the second class and method used to populate it please.
You can create an anonymous object and serialize that:
var objContainingItems = ... // your usual code
var customDataObj = new { customData = objContainingItems };
string json = JsonConvert.SerializeObject(customDataObj);
This is the most convenient solution if all you are interested in is serializing.
If you also want to be able to deserialize it, then you will need to use a class as specified in the answer by #William Moore.
Create a class customData and create a reference to the class items inside it. Then serialise your customData class using Newtonsoft.Json rather than your items class. So you will have:
public class CustomData
{
public items[] items; // I would rename the class items to item
}
Then you have an object of type customData, called customData which you pass into Newtonsoft.
You could then use the following to serialise/deserialise your data:
CustomData input = new CustomData();
input.items = []; // Whatever you have at the moment?
string json = JsonConvert.SerializeObject(account) //optionally set Formatting.Indented
CustomData deserialised = JsonConvert.DeserializeObject<CustomData>(json);

NewtonSoft json parsing

Can somebody help me to parse the json and get the details.
Lets say i have Top2 input parameter as Police and i want to know the respective Top3 and in that Top3 array i need to check whether Test1Child value is there or not.
I am using newtonsoft json + c# and so far i can get till DeviceTypes using below code
var json = File.ReadAllText(jsonFile); // below json is stored in file jsonFile
var jObject = JObject.Parse(json);
JArray MappingArray =(JArray)jObject["Top1"];
string strTop1 = ObjZone.Top1.ToString();
string strTop2 = ObjZone.Top2.ToString();
var JToken = MappingArray.Where(obj => obj["Top2"].Value<string>() == strTop2).ToList();
//Json
{
"Top1": [
{
"Top2": "Test1",
"Top3": [
"Test1Child"
]
},
{
"Top2": "Test2",
"Top3": [
"Test2Child"
]
}
]
}
I'd use http://json2csharp.com/ (or any other json to c# parser) and then use C# objects (it's just easier for me)
This would look like that for this case:
namespace jsonTests
{
public class DeviceTypeWithResponseTypeMapper
{
public string DeviceType { get; set; }
public List<string> ResponseTypes { get; set; }
}
public class RootObject
{
public List<DeviceTypeWithResponseTypeMapper> DeviceTypeWithResponseTypeMapper { get; set; }
}
}
and the use it like that in code:
var rootob = Newtonsoft.Json.JsonConvert.DeserializeObject<RootObject>(str);
var thoseThatHaveNotUsed = rootob.DeviceTypeWithResponseTypeMapper.Where(dtwrtm =>
dtwrtm.ResponseTypes.Any(rt => rt == "NotUsed"));
foreach (var one in thoseThatHaveNotUsed)
{
Console.WriteLine(one.DeviceType);
}
this code lists all the Device types that have "NotUsed" among the responses.
version 2 (extending your code) would look like that, i believe:
static void Main(string[] args)
{
var json = str; // below json is stored in file jsonFile
var jObject = JObject.Parse(json);
JArray ZoneMappingArray = (JArray)jObject["DeviceTypeWithResponseTypeMapper"];
string strDeviceType = "Police";
string strResponseType = "NotUsed";
var JToken = ZoneMappingArray.Where(obj => obj["DeviceType"].Value<string>() == strDeviceType).ToList();
var isrespTypeThere = JToken[0].Last().Values().Any(x => x.Value<string>() == strResponseType);
Console.WriteLine($"Does {strDeviceType} have response type with value {strResponseType}? {yesorno(isrespTypeThere)}");
}
private static object yesorno(bool isrespTypeThere)
{
if (isrespTypeThere)
{
return "yes!";
}
else
{
return "no :(";
}
}
result:
and if you'd like to list all devices that have response type equal to wanted you can use this code:
var allWithResponseType = ZoneMappingArray.Where(jt => jt.Last().Values().Any(x => x.Value<string>() == strResponseType));
foreach (var item in allWithResponseType)
{
Console.WriteLine(item["DeviceType"].Value<string>());
}

json properties to lower case c#

I'm getting from client json string:
{ "Client": { "Name": "John" } }
but for the further handling I need the following json:
{ "client": { "name": "John" } }
I tried something like that, but it didn't help:
public class LowerCaseNamingStrategy : NamingStrategy
{
protected override string ResolvePropertyName(string name)
{
return name.ToLower();
}
}
and
var settings = new JsonSerializerSettings();
settings.ContractResolver = new DefaultContractResolver { NamingStrategy = new LowerCaseNamingStrategy() };
var json = JsonConvert.DeserializeObject(input.DataJson, settings);
JSON is dynamic object, so I don't know properties are there.
How can I do that with c#? With using Newtonsoft.Json or may be with using Xml.
If I understood you correctly, you need to modify properties in your Json string, but not convert the Json into object.
In this case you can try to parse Json into JObject and replace properties in that object.
private static void ChangePropertiesToLowerCase(JObject jsonObject)
{
foreach (var property in jsonObject.Properties().ToList())
{
if(property.Value.Type == JTokenType.Object)// replace property names in child object
ChangePropertiesToLowerCase((JObject)property.Value);
property.Replace(new JProperty(property.Name.ToLower(),property.Value));// properties are read-only, so we have to replace them
}
}
sample:
var jsonString = #"{ ""Client"": { ""Name"": ""John"" } }";
var jobj = JObject.Parse(jsonString, new JsonLoadSettings());
ChangePropertiesToLowerCase(jobj);
var stringWithLowerCaseProperties = jobj.ToString(Formatting.None);
Try LowercaseContractResolver instead
var settings = new JsonSerializerSettings();
settings.ContractResolver = new LowercaseContractResolver();
var json = JsonConvert.DeserializeObject(input.DataJson, settings);
Extending Anton Semenov answer for cases when JSON can contain an Array of Objects:
private static void ChangePropertiesToLowerCase(JObject jsonObject)
{
foreach (var property in jsonObject.Properties().ToList())
{
if (property.Value.Type == JTokenType.Object) // replace property names in child object
ChangePropertiesToLowerCase((JObject)property.Value);
if (property.Value.Type == JTokenType.Array)
{
var arr = JArray.Parse(property.Value.ToString());
foreach (var pr in arr)
{
ChangePropertiesToLowerCase((JObject)pr);
}
property.Value = arr;
}
property.Replace(new JProperty(property.Name.ToLower(CultureInfo.InvariantCulture), property.Value)); // properties are read-only, so we have to replace them
}
}
All other solutions here modify the original object. Here's an immutable version which returns a new object with lowercase properties:
public static class JsonExtensions
{
public static JToken ToLowerRecursive(this JToken token)
{
if (token is JObject jobj)
return new JObject(jobj.Properties().Select(x => new JProperty(x.Name.ToLowerInvariant(), x.Value.ToLowerRecursive())));
if (token is JArray jarr)
return new JArray(jarr.Select(x => x.ToLowerRecursive()));
return token;
}
}
This is easy way without Regex. Replace every [{ "A] with [{ "a]
var json = "{ \"Client\": { \"Name\": \"John\" } }";
var newJson = string.Empty;
foreach (var w in json.Split(new[] { "{ \"" }, StringSplitOptions.RemoveEmptyEntries))
{
if (w[0] != null)
{
newJson += "{ \"" + (w[0].ToString().ToLower()) + w.Remove(0,1);
}
}
result:
"{ \"client\": { \"name\": \"John\" } }"

Categories

Resources