How we can check a json array is null or emty?
Json:
{
"productList": [
{
"id": 2440,
"serviceStatus": 1,
"listOfBillProductsExtras": [
{
"id": 2441,
"amount": 1,
"balance": 2,
}
],
"deskName": "Desk 1",
"onlyTime": "15:25"
},
{
"id": 2441,
"serviceStatus": 1,
"listOfBillProductsExtras": [ ],
"deskName": "Desk2",
"onlyTime": "15:27"
}
]
}
I try
JArray productList = JArray.Parse(content["productList"].ToString());
but it didn't work. (There was exp. Null Referance ) So, I want to check listOfBillProductsExtras array is null or empty. If not empty I will get the id, amount, balance.
Parse the Json object to jArray:
public ActionResult Method(object[] data)
{
var productList = Json.ParseJsonObjectToJArray(data, "productList");
if(jArray.Count > 0)
{
}
}
public class Json
{
public static JArray ParseJsonObjectToJArray(object[] data, string objectName)
{
dynamic jObject = JObject.Parse(data[0].ToString());
var info = jObject[objectName];
return info;
}
}
This should work
var found = JObject.Parse(json).SelectToken("productList[0].listOfBillProductsExtras[0].id");
where json is your input string.
found variable can be checked for null value.
Related
I am trying to update a value which is a parameter in an array of JToken type. But I am getting the error below:
Set JArray values with invalid key value: "tagCategoryId". Int32 array index expected.
Below is my method:
JObject _asset;
JToken _tagCategoryObject ;
public void Test1()
{
var invalidTagCategoryId = "invalidTagCategoryId";
while (invalidTagCategoryId.Length <= 255) invalidTagCategoryId += invalidTagCategoryId;
_asset.TryGetValue("enumeratedTagCategories", out _tagCategoryObject);
if (_tagCategoryObject != null)
{
_tagCategoryObject["tagCategoryId"] = invalidTagCategoryId;
}
.....
}
Below is the json:
"enumeratedTagCategories": [
{
"tagCategoryId": "TagCategoryId",
"tagCategoryDisplayName": "TagCategoryDisplayName",
"version": "Version",
"tagValueIdsList": [
{
"displayName": "DisplayName1"
},
{
"displayName": "DisplayName2"
}
]
}
]
How can I assign a value to tagCategoryId ?
_tagCategoryObject is array so try this
var _tagCategoryObject = JObject.Parse(json)["enumeratedTagCategories"];
if (_tagCategoryObject != null)
{
_tagCategoryObject[0]["tagCategoryId"] = invalidTagCategoryId;
}
and fix you json by wrapping in {}
{ "enumeratedTagCategories": [
{
"tagCategoryId": "TagCategoryId",
"tagCategoryDisplayName": "TagCategoryDisplayName",
"version": "Version",
"tagValueIdsList": [
{
"displayName": "DisplayName1"
},
{
"displayName": "DisplayName2"
}
]
}
]
}
I'm currently trying to parse a Json file in Unity using Json.NET and can parse 80% of the data at the moment. I've come across a section that is formatted like this.
{
"features": [
{
"coordinates": [
[
[ 1, 1 ],
[ 2, 2 ]
]
]
},
{
"coordinates": [
[ 1, 1 ],
[ 2, 2 ],
[ 3, 3 ]
]
},
{
"coordinates": [
[
[ 1, 2 ],
[ 1, 2 ]
]
]
},
{
"coordinates": [
[
[
[ 1, 2 ],
[ 1, 2 ]
]
]
]
}
]
}
The coordinates array may contain an unknown number of coordinates and it will be a jagged array with unknown dimensions as well. I'm unable to parse it as I'm not very well versed with Json deserialization.
Any help on how to approach this appreciated.
I'm not sure how much variety is in your data but here's a rough idea using recursion.
void ParseCoordinateJSON()
{
var coordinates = new List<int[]>();
var parsed = JObject.Parse(rawJson);
var featureArray = parsed.SelectToken("features");
var coordArray = featureArray.Children();
coordArray.ToList().ForEach(n => ExtractCoordinates(n.SelectToken("coordinates"), coordinates));
Debug.Log(string.Join("\n", coordinates.Select(c => $"({string.Join(", ", c)})")));
}
void ExtractCoordinates(JToken node, List<int[]> coordinates)
{
if (node == null)
{
return;
}
if (node.Children().Any(n => n.Type == JTokenType.Integer))
{
coordinates.Add(node.Children().Select(n => n.Value<int>()).ToArray());
return;
}
node.Children().Where(n => n.Type == JTokenType.Array).ToList().ForEach(n => ExtractCoordinates(n, coordinates));
}
Edit:
Here's it is without linq which might be easier to follow:
void ParseCoordinateJSONNoLinq()
{
var coordinates = new List<int[]>();
var parsed = JObject.Parse(rawJSON);
var featureArray = parsed.SelectToken("features");
// These will be the objects with a "coordinates" key and an array value.
var coordArray = featureArray.Children();
foreach(var node in coordArray)
{
ExtractCoordinatesNoLinq(node.SelectToken("coordinates"), coordinates);
}
Console.WriteLine(string.Join("\n", coordinates.Select(c => $"({string.Join(", ", c)})")));
}
void ExtractCoordinatesNoLinq(JToken node, List<int[]> coordinates)
{
var intValues = new List<int>();
// Step through each child of this node and do something based on its node type.
foreach(var child in node.Children())
{
// If the child is an array, call this method recursively.
if (child.Type == JTokenType.Array)
{
// Changes to the coordinates list in the recursive call will persist.
ExtractCoordinatesNoLinq(child, coordinates);
// The child type is an integer, add it to the int values.
} else if (child.Type == JTokenType.Integer)
{
intValues.Add(child.Value<int>());
}
}
// Since we found int values at this level, add them to the shared coordinates list.
if (intValues.Count > 0)
{
coordinates.Add(intValues.ToArray());
}
}
If the rest of your data is reliable, I would use typical data objects and de-serialize to them then add something like the above as a Custom JSON Converter for an object representing the jagged array.
public class MyDataObject {
public string SomeField {get; set;}
public Vector2 Position {get; set;}
[JsonProperty("features")]
public JaggedFeatures {get; set;}
}
public class JaggedFeatures {
public List<int[]> Coordinates {get; set;}
}
//...
JsonConvert.Deserialize<MyDataObject>(rawJSON, new JaggedFeaturesConverter())
I am serializing an anonymous object to use as a request message for an HTTP post request. The problem is that one of the JSON keys contains a dot in its name. VS throws out ''invalid anonymous type member declarator'' error.
return JsonConvert.SerializeObject(new
{
query = "something",
firstname.keyword = "xyz"
});
What can I do to solve this issue?
Edit: the real json request looks something like this, so I don't think I can use a dictionary:
{
"query": {
"bool": {
"must": [
{
"term": {
"firstname.keyword": ""
}
}
],
"must_not": [ ],
"should": [ ]
}
},
"from": 0,
"size": 10,
"sort": [ ],
"aggs": { }
}
Json can generally be represented using arrays, dictionaries and anonymous objects.
The first part of your Json can be generated as follows:
return JsonConvert.SerializeObject(new
{
query = new
{
#bool = new
{
must = new[]
{
new
{
term = new Dictionary<string, object>
{
["firstname.keyword"] = string.Empty,
}
}
}
}
}
});
This question already has answers here:
Merge two Json.NET arrays by concatenating contained elements
(3 answers)
Closed 7 years ago.
I have a first json:
{
"data": [{
"id": "id1",
"field": "field1"
}],
"paging": {
"prev": "link1",
}
}
and a second one:
{
"data": [{
"id": "id2",
"field": "field2"
}],
"paging": {
"prev": "link2",
}
}
and I want to merge/union the two Data array, such as:
{
"data": [{
"id": "id1",
"field": "field1"
},
{
"id": "id2",
"field": "field2"
}]
}
(I don't care about about paging right now).
How can I do it quick and easy? This is my try:
var final = JsonConvert.SerializeObject(new { data = json1["data"].Union(json2["data"]) }, Newtonsoft.Json.Formatting.Indented).ToString();
but an Exception is raised: 'Newtonsoft.Json.Linq.JArray' does not contains a definition of 'Union'
Newtonsoft.Json now supports merging objects (old link):
var dataObject1 = JObject.Parse(#"{
""data"": [{
""id"": ""id1"",
""field"": ""field1""
}],
""paging"": {
""prev"": ""link1"",
}
}");
var dataObject2 = JObject.Parse(#"{
""data"": [{
""id"": ""id2"",
""field"": ""field2""
}],
""paging"": {
""prev"": ""link2"",
}
}");
var mergeSettings = new JsonMergeSettings
{
MergeArrayHandling = MergeArrayHandling.Union
};
// method 1
(dataObject1.SelectToken("data") as JArray).Merge(dataObject2.SelectToken("data"), mergeSettings);
// method 2
//dataObject1.Merge(dataObject2, mergeSettings);
var mergedArray = dataObject1.SelectToken("data") as JArray;
Console.WriteLine(mergedArray.ToString(Formatting.None));
(checked with brain-compiler ;) )
JArray dataOfJson1=json1.SelectToken("data");
JArray dataofJson2=json2.SelectToken("data");
foreach(JObject innerData in dataofJson2)
{
dataOfJson1.Add(innerData);
}
For those that (like me) cannot use the new JSON.net library.
The following method is what I use.
public static JObject mergeJsonObjects(List<JObject> objects) {
JObject json = new JObject();
foreach(JObject JSONObject in objects) {
foreach(var property in JSONObject) {
string name = property.Key;
JToken value = property.Value;
json.Add(property.Key, property.Value);
}
}
return json;
}
The method takes an list of JObjects and returns a single JObject, simpel and effective.
A possible solution could be:
class Container
{
public List<IdField> data{get;set;}
}
class IdField
{
public string id{get;set;}
public string field{get;set;}
}
string s1 = "{ \"data\": [{ \"id\": \"id1\", \"field\": \"field1\" }], \"paging\": { \"prev\": \"link1\", } }";
string s2 = "{ \"data\": [{ \"id\": \"id2\", \"field\": \"field2\" }], \"paging\": { \"prev\": \"link2\", } }";
var d1 = JsonConvert.DeserializeObject<Container>(s1);
var d2 = JsonConvert.DeserializeObject<Container>(s2);
d1.data.AddRange(d2.data);
var result = JsonConvert.SerializeObject(d1);
I use the DataContractJsonSerializer to parse a json string into a object hierarchie.
The json string looks like this:
{
"groups": [
{
"attributes": [
{
"sortOrder": "1",
"value": "A"
},
{
"sortOrder": "2",
"value": "B"
}
]
},
{
"attributes": {
"sortOrder": "1",
"value": "C"
}
}
]
}
As you can see the sub value of "attributes" can be an array or a single item.
I found the code part where the problem occures:
[DataContract]
public class ItemGroup
{
[DataMember(Name="attributes")]
public List<DetailItem> Items { get; set; }
}
This works for the first one but fails on the second one.
Has anyone an answer for this?
Thx
If you control how the JSON is created then make sure that attributes is an array even if it only contains one element. Then the second element will look like this and parse fine.
{
"attributes": [{
"sortOrder": "1",
"value": "C"
}]
}
As Daniel said, if you can control the creation of Json, it is better to continue that way.
But if you can't, then you can use Json.Net library & the JsonObject class from
this link to write some code like:
JObject o = (JObject)JsonConvert.DeserializeObject(input);
dynamic json = new JsonObject(o);
foreach (var x in json.groups)
{
var attrs = x.attributes;
if (attrs is JArray)
{
foreach (var y in attrs)
{
Console.WriteLine(y.value);
}
}
else
{
Console.WriteLine(attrs.value);
}
}
I tried to get this working with DataContractJsonSerializer, JavaScriptSerializer, and JSON.Net and none would deserialize directly to an object successfully in all cases. I used a similar approach as L.B, using JSON.Net, although without the use of dynamics and the extra JsonObject class. Adapting my code to your scenario would look something like the following:
private List<ItemGroup> ParseItemGroupList(string input)
{
JObject json = JObject.Parse(input);
List<ItemGroup> groups = new List<ItemGroup>();
JArray gArray = json["groups"] as JArray;
foreach (var gToken in gArray)
{
ItemGroup newGroup = new ItemGroup();
JToken attrToken = gToken["attributes"] as JToken;
if (attrToken is JArray)
{
newGroup.Items = attrToken.Children().Select(MapDetailItem()).ToList();
}
else
{
newGroup.Items = new List<DetailItem>() { MapDetailItem().Invoke(attrToken) };
}
groups.Add(newGroup);
}
return groups;
}
private static Func<JToken, DetailItem> MapDetailItem()
{
return json => new DetailItem
{
SortOrder = (string)json["sortOrder"],
Value = (string)json["value"]
};
}
Hopefully, someone will add a setting for JSON.Net to allow it to force deserialization to a collection with a single item rather than throwing an exception. It's a shame that you have to do all of the parsing manually when there is only one small portion of the JSON that doesn't parse correctly automatically.