Transform nested JSON arrays into an array of JSON objects - c#

I have the following set of variable JSON data where the first row is the list of element names and the other rows represent the list of element values:
[
[
"category",
"author",
"title",
"price"
],
[
"reference",
"Nigel Rees",
"Sayings of the Century",
"8.95"
],
[
"fiction",
"Evelyn Waugh",
"Sword of Honour",
"12.99"
],
[
"fiction",
"Herman Melville",
"Moby Dick",
"8.99"
],
[
"fiction",
"J. R. R. Tolkien",
"The Lord of the Rings",
"22.99"
]
]
I want to build the following type of JSON object from the data:
{
"book": [
{
"category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
},
{
"category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
},
{
"category": "fiction",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99
},
{
"category": "fiction",
"author": "J. R. R. Tolkien",
"title": "The Lord of the Rings",
"isbn": "0-395-19395-8",
"price": 22.99
}
]
}
How can I build my JSON object which should be identical to the above. I don't see any direct support for doing this using JSON.NET APIs.
Any help is greatly appreciated.

This is just an idea to get you on the right track.
JObject obj = new JObject();
var child = new JArray();
var child2 = new JObject();
child2.Add("category", "references");
child.Add(child2);
obj.Add("book", child);
var result = obj.ToString();
I won't do the string parsing for you, i think you should be able to handle that.
The piece of code I've given produces the following JSON:
{
"book": [
{
"category": "references"
}
]
}
Hope this helps.

You can do this transformation using Json.Net's LINQ-to-JSON API:
JArray array = JArray.Parse(origJson);
string[] keys = array.First().Select(t => t.ToString()).ToArray();
JArray array2 =
new JArray(array.Skip(1).Select(a =>
new JObject(a.Select((t, i) =>
new JProperty(keys[i], t)
))
));
JObject obj = new JObject(new JProperty("book", array2));
string finalJson = obj.ToString();
Fiddle: https://dotnetfiddle.net/kMyHOe

Related

C# Add values to a JSON file without changing the initial JSON format

What I want:
To add a property in JSON file in a specific place (for this case I want to add {"name2":"Tom} after {"name":"Andrew"} ) without changing its format (empty new lines)
Current JSON
{
"Profiles": [
{
"Properties": {
"color": "blue",
"shape": "circle",
"fruit": "apple",
"name": "Andrew",
"username": "ad11",
"Password": "pass",
"country": "France",
"region": "Europe"
}
}
]
}
Current code
var json = File.ReadAllText(path);
var Jobect = JObject.Parse(json);
Jobect["Profiles"][0]["Properties"]["name2"] = "Tom";
File.WriteAllText(path, Jobect.ToString());
Resulted JSON
{
"Profiles": [
{
"Properties": {
"color": "blue",
"shape": "circle",
"fruit": "apple",
"name": "Andrew",
"username": "ad11",
"Password": "pass",
"country": "France",
"region": "Europe",
"name2": "Tom"
}
}
]
}
Desired JSON
{
"Profiles": [
{
"Properties": {
"color": "blue",
"shape": "circle",
"fruit": "apple",
"name": "Andrew",
"name2": "Tom,
"username": "ad11",
"Password": "pass",
"country": "France",
"region": "Europe"
}
}
]
}
In conclusion. How can I add a property in JSON file in a specific place and without change its format?

How to upload/index a GeoJson file in Elasticsearch using NEST(C#)

I have GeoJson File with me which i want to index on Elastic Search through NEST
But Due to lack of expertise I am having trouble in indexing the document
I have created a class which represent the Mapping on ElasticSearch:
public class GeoDocument
{
[Nest.Keyword(Name = "DocId")]
public string DocId { get; set; }
[Nest.GeoShape(Name = "GeoField")]
public object GeoField { get; set; }
}
But when i use this Mapping to index a Document
var polygon = "{\"type\":\"Polygon\",\"coordinates\":[[[5.856956,51.002753],[5.856928,51.002771],[5.856687,51.002853],[5.856956,51.002753]]]}";
var geoDocument = new GeoJson
{
DocId = "1",
GeoField = JsonConvert.DeserializeObject<object>(polygon)
};
var IndexResponse = client.IndexDocument(geoDocument);
I get a Result Something like This
"_source": {
"DocId": "1",
"GeoField": [
[
[]
],
[
[
[
[
[],
[]
],
[
[],
[]
],
[
[],
[]
],
[
[],
[]
]
]
]
]
]
}
}
In order to make that JObject saved correctly, you have to tell the ElasticClient to use NewtonSoft .Net serializer.
Install NEST.JsonNetSerializer package
Reference the JsonNetSerializer in your ConnectionSettings
If you get 400 after changing the settings, you might need to create a new Index.
Sample code
using Nest;
using Elasticsearch.Net;
using Nest.JsonNetSerializer;
SingleNodeConnectionPool node = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
ConnectionSettings settings = new ConnectionSettings(
node,
JsonNetSerializer.Default
);
settings.DefaultMappingFor<GeoDocument>(m => m.IndexName("project2"));
ElasticClient client = new ElasticClient(settings);
// This is Supposed to be GeoDocument as per your question.
GeoDocument geoDocument = new GeoDocument
{
DocId = "1",
GeoField = JObject.Parse(polygon)
// GeoField = JsonConvert.DeserializeObject<object>(polygon) // <-- Works too.
};
IndexResponse IndexResponse = client.IndexDocument(geoDocument);
Response
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 1.0,
"hits": [
{
"_index": "project2",
"_type": "_doc",
"_id": "COQRXW8BNG2RJmIOyoO0",
"_score": 1.0,
"_source": {
"DocId": "1",
"GeoField": {
"type": "Polygon",
"coordinates": [
[
[
5.856956,
51.002753
],
[
5.856928,
51.002771
],
[
5.856687,
51.002853
],
[
5.856956,
51.002753
]
]
]
}
}
}
]
}
}

json object data accessing via index

I have following json object of array structure.I am trying to retrieve certain elements from array of objects
{
"data": [
{
"_id": "5b62dc6ebef986403db8aafd",
"name": "smitha vijaya",
"designation": "account management",
"projects": {
"project1": "description1",
"project2": "description2"
},
"age": "27"
},
{
"_id": "5b62dd17bef986403db8ab90",
"name": "JIKKU VARGHESE",
"designation": "SUPERVISING OPERATIONS MANAGER",
"projects": {
"project1": "description1",
"project2": "description2"
},
"age": "27"
},
{
"_id": "5b62dd76bef986403db8abe3",
"name": "SUJEETH NAIR",
"designation": "MENA AMS",
"projects": {
"project1": "description1",
"project2": "description2"
},
"age": "30"
},
{
"_id": "5b62ddb1bef986403db8ac13",
"name": "GIRISH KN",
"designation": "MENA AMS",
"projects": {
"project1": "description1",
"project2": "description2"
},
"age": "27"
}
]
}
I am using following c# code to extract ist name (smitha)
JsonData jsonvale = JsonMapper.ToObject( jsonString);
Name = jsonvale["data"][0]["name"].ToString();
print (name);
how can i acces other elements like name jikku and so on?
You could iterate using a for loop:
for(int i = 0; i < jsonvale.length; i++) {
JsonData jsonvale = JsonMapper.ToObject(jsonString);
Name = jsonvale["data"][i]["name"].ToString();
print(name);
}
You can iterate it over your json array like following:
JsonData jsonvale = JsonMapper.ToObject( jsonString);
for (var i=0; i<jsonvale.length; i++){
print(jsonvale["data"][i]["name"].ToString());
}
Just deserialize this json in List() and then you can access all data from this list without loops, and when you do not need this list anymore, you can destroy it. See this example https://github.com/IonCojucovschi/JsonDeserializeGenericForm

c# can't build json hierarchy correctly using newtonsoft

I looked at this SO question but it doesn't address my needs: Build JSON Hierarchy from Structured Data
The problem: When trying to build the json, the nesting of children isn't done correctly. The children property should contain 1 or more name array items like this:
"children": [{
"name": "XXX - Level XXX",
...instead it is generated as:
"children": []
Here's a dotnet fiddle with more code details:
I'm trying to build the tree by using json.net .Last to grab the last child and then add a JObject to that child but that isn't working out too well for me.
The main data structure used to build the json :
Dictionary<int, Industry>();
The desired json structure should be:
{
"name": "XX Level XX",
"children": [{
"name": "XXX - Level XXX",
"children": [{
"name": "XXXX - Level XXXX",
"children": [{
"name": "XXXXX - Level XXXXX",
"children": [{
"name": "XXXXXX - Level XXXXXX"
}]
}]
}]
}]
}
The actual output is:
{
"name": "XX-Level XX",
"children": [{
"name": "XXX-Level XXX",
"children": []
}, {
"name": "XXXX-Level XXXX",
"children": []
}, {
"name": "XXXXX-Level XXXXX",
"children": []
}, {
"name": "XXXXXX-Level XXXXXX",
"children": []
}
]
}
Here's the code that builds the json:
dynamic relationship = new JObject();
relationship.name = relationships[0].Industries[1].Name;
relationship.children = new JArray();
var lftIndustries = relationships[0].Industries.Where(k => k.Key > 1);
foreach (var item in lftIndustries)
{
//not good enough, need to get deepest, empty "children" node
// and add "industry" to it
var node = ((JContainer)relationship).Last;
var childArray = (JArray)((JContainer) node).Last;
var industry = new JObject(new JProperty("name", item.Value.Name), new JProperty("children", new JArray()));
childArray.Add(industry);
}
json = relationship.ToString();
I thought that using json.net .Last and .Next would be the answer.
try this in your foreach loop and tell me if it is whats youre looking for:
JToken currentContainer = null;
foreach (var item in lftIndustries)
{
//not good enough, need to get deepest, empty "children" node
// and add "industry" to it
if (currentContainer != null)
{
var node = ((JContainer)currentContainer).Last;
var childArray = (JArray)((JContainer) node).Last;
var industry = new JObject(new JProperty("name", item.Value.Name), new JProperty("children", new JArray()));
childArray.Add(industry);
currentContainer = industry;
}
else
{
var node = ((JContainer)relationship).Last;
var childArray = (JArray)((JContainer) node).Last;
var industry = new JObject(new JProperty("name", item.Value.Name), new JProperty("children", new JArray()));
childArray.Add(industry);
currentContainer = industry;
}
}
The result is this:
{
"name": "XX-Level XX",
"children": [
{
"name": "XXX-Level XXX",
"children": [
{
"name": "XXXX-Level XXXX",
"children": [
{
"name": "XXXXX-Level XXXXX",
"children": [
{
"name": "XXXXXX-Level XXXXXX",
"children": []
}
]
}
]
}
]
}
]
}
Heres the fiddle: https://dotnetfiddle.net/1yzTGU

Sort properties json without serializing to a model

I have two json strings I want to compare.
The problem is that when the properties are the same but in a different order.
Simple example:
{
"Number": "123",
"Name": "My name",
"FirstName": "My First name",
"Prop1": false,
"Prop2": [],
"Plans": [],
"SomeList": [
{
"Code": "118",
"Period": {
"From": "2000-01-27T00:00:00.0000000",
"Until": "2003-12-31T00:00:00.0000000"
}
},
{
"Code": "120",
"Period": {
"From": "2004-01-01T00:00:00.0000000",
"Until": "2004-12-31T00:00:00.0000000"
}
}
]
}
and
{
"Number": "123",
"FirstName": "My First name",
"Name": "My name",
"Prop1": false,
"Prop2": [],
"Plans": [],
"SomeList": [
{
"Code": "120",
"Period": {
"From": "2004-01-01T00:00:00.0000000",
"Until": "2004-12-31T00:00:00.0000000"
}
},
{
"Code": "118",
"Period": {
"From": "2000-01-27T00:00:00.0000000",
"Until": "2003-12-31T00:00:00.0000000"
}
}
]
}
These two json's are equal.
The compare function JToken.DeepEquals says it's not equal.
And I don't want to serialize to an object XYZ to compare both jsons.
I want to sort the properties of the json so the JToken.DeepEquals works fine.

Categories

Resources