I have a simple JSON example like this :
{
"items": {
"element": ["item 1","item 2"]
},
"name": "James"
}
and a JSON Schema like this :
{
'type': 'object',
'properties': {
'name': {'type':'string'},
'items': {
'type': 'object',
'properties':{
'element':{
'type':'array',
'items':[{'type':'string', 'type':'string'}]
}
}
}
},
'additionalProperties': false
}
Calling "IsValid()" method in JSON.NET using the given Schema and Data will return VALID.
Question :
How do I traverse and edit the elements inside JSON ?
My objective is to look-up values of node "element" in database and then replace it with a generalized value if they exists e.g "item 1" exists in database and will replaced with "general value A". However, "item 2" doesn't exist in database and should throw some kind of error message when IsValid() method is called.
Note that this is a desktop application using .NET 4.5 and JSON.NET library, and it will be used as a data-cleansing tool. I'm open to any kind of alternative libraries as long as they are compatible with .NET 4.5 though..
Not fully understand the part of your question with IsValid() issue, but if talk about traversing through json elements and editing, you can use 2 methods.
Install Newtonsoft.Json at first in Package-Manager in VS: Install-package newtonsoft.json.
Create class JsonItems, which describes your input JSON hierarchy
class JsonItems
{
public Items items;
public string name;
}
class Items
{
public List<string> elements { get; set; }
}
Now you can legally deserialize your json and edit result as any .net object.
string jsonItems = #"{
'items': {
'element': ['item 1','item 2']
},
'name': 'James'
}";
var result = JsonConvert.DeserializeObject<JsonItems>(jsonItems);
result.items = new Items
{
elements = new List<string> {"itemd 3", "item 4"}
};
2. The second way is more quick (in implementation), but less secuarble in runtime. With help of dynamic.
dynamic d = JObject.Parse(jsonItems);
d.items.element[1] = "item 3";
Related
I have a business case where I need to take any incoming JSON payload (so the JSON object will have to be dynamic, not predefined by a C# class) and prepend a given namespace to all its containing keys.
For example if the following payload comes in:
{
"property1": "value1",
"property2": 2,
"property3": true,
"property4": {
"myArray": [
{
"arrayProperty1": "im the first object in array",
"arrayProperty2": "some value"
},
{
"arrayProperty1": "im the second object in array",
"arrayProperty2": "some value"
}
]
}
}
Then it needs to result in the following output:
{
"mynamespace.property1": "value1",
"mynamespace.property2": 2,
"mynamespace.property3": true,
"mynamespace.subObj": {
"mynamespace.myArray": [
{
"mynamespace.arrayProperty1": "im the first object in array",
"mynamespace.arrayProperty2": "some value"
},
{
"mynamespace.arrayProperty1": "im the second object in array",
"mynamespace.arrayProperty2": "some value"
}
]
}
}
Is this possible using C#? I tried searching for any similar question here on stackoverflow but this is the closest I got (they're using javascript): Prepending namespace to all of a JSON object's Keys
You can make a short helper method using Json.Net's LINQ-to-JSON API (JTokens) to accomplish this:
public static string AddPrefixToAllKeys(string json, string prefix)
{
JContainer token = (JContainer)JToken.Parse(json);
// Note: We need to process the descendants in reverse order here
// to ensure we replace child properties before their respective parents
foreach (JProperty prop in token.Descendants().OfType<JProperty>().Reverse().ToList())
{
prop.Replace(new JProperty(prefix + prop.Name, prop.Value));
}
return token.ToString();
}
Then use it like this:
string modifiedJson = AddPrefixToAllKeys(originalJson, "mynamespace.");
Working demo here: https://dotnetfiddle.net/AdkAO7
I have tried numerous times and also tried to consult the documentation provided by Newtonsoft website but there doesn't seem to be any answer regarding my issue. I have also searched Google for no avail, wasted around 4 hours with no answer but just to continue without it.
My issue is that can't add a JObject into a JArray with a Key. I understand it seems rather simple task but whatever I tried ended up in an Exception being thrown out.
I am trying to write a simple file with the following layout:
{
"items" : [
"item 1" : { },
"item 2" : { },
]
}
// the actual layout that I have is
{
"items" : [
{ },
{ },
]
}
I can successfully add the first items key for JArray through jobj["items"] = jarray but I cant seem to use the same technique with JArray. I need to add items in JArray through JArray.Add() and it doesn't let me provide a key. I'm actually lost on this. Can someone please explain as how can I achieve the above layout? Thank you.
As #dbc mentioned, the format which you require is not valid. The nearest valid format you could achieve is as following.
{
"items":
{
"item 1":{},
"item 2":{}
}
}
You could achieve it using a Data structure containing a Dictionary. For Example
var data = new DataStructure{
items = new Dictionary<string,Item>()
{
["Item 1"] = new Item{},
["Item 2"] = new Item{}
}};
Where DataStructure and Item is defined as
public class DataStructure
{
public Dictionary<string,Item> items{get;set;}
}
public class Item
{
}
If you do not want to create concrete class, you can achieve the same using anonymous types
var data = new {items = new Dictionary<string,Item>()
{
["Item 1"] = new Item{},
["Item 2"] = new Item{}
}};
or, if you want to avoid creation of Item Class as well
var data = new {items = new Dictionary<string,object>()
{
["Item 1"] = new {},
["Item 2"] = new {}
}};
Output
{
"items": {
"Item 1": {},
"Item 2": {}
}
}
As it has been suggested in the comment, my desired layout is not a right one. As it appears json needs an anonymous object within the array to hold a keyed object.
So the layout should be:
{
"items" : [
{
"item1" : { }
},
.......
]
}
I have a little utility where we extract values from JSON using JObject.SelectToken(path). We need to determine the paths at run-time. Works perfectly.
What I now need to do is to write back into the JSON (JObject or other) using the same path string. I've hunted and searched and I can't quite find if there is anything that does this quite as cleanly as SelectToken does for reading.
(I'm also stuck in 3.5 CF)
For example, something like:
... JObject read in already ...
var theJToken = theJObject.SelectToken("animals.cat[3].name");
theTJoken.SetValue("Bob"); // Of course this doesn't exist
... serialize it ...
JToken.SelectToken actually returns a JToken which can be modified using JToken.Replace. You can use that to replace the node within your JSON object, mutating the original object.
JObject o = JObject.Parse(#"{ 'cats': [
{ 'name': 'cat 1' },
{ 'name': 'cat 2' },
{ 'name': 'cat 3' } ] }");
// get the token
JToken secondCatName = o.SelectToken("cats[1].name");
// replace the name
secondCatName.Replace("meow");
// and the original object has changed
Console.WriteLine(o.ToString());
// { "cats": [ { "name": "cat 1" }, { "name": "meow" }, { "name": "cat 3" } ] }
My application accesses a mongodb using the .net c# driver.
My data structure looks like this:
{
"_id" : ObjectId("53d97351e37f520a342e152a"),
"Name" : "full question test 2",
"keywords" : ["personality", "extraversion", "agreeableness"],
"Questions" : [{
"type" : "likert",
"text" : "question 1",
},
{
"type" : "likert",
"text" : "question 2",
}]
}
What I want to do is to select only the type column from the questions array.
Here is my linq code now:
from e in collection.AsQueryable<scales>()
where e.Name == "full question test 2"
select new { e.Id, e.Name, e.Questions}
This returns all the question properties (both type and text). I want just the type. I can do this by saying something like e.Questions[0].text,e.Questions[1].text.
But the number of questions varies from file to file, so I'd love a solution that doesnt require this manual coding.
Open to ideas!
The standard query methods that are wrapped here have a form of "projection" available for field selection, but this is however not capable of doing things such as selecting specific fields within an array element. At least for multiple fields anyway.
But single fields should be possible just using the dot notation form to access the element:
from e in collection.AsQueryable<scales>()
where e.Name == "full question test 2"
select new { e.Id, e.Name, e.Questions.type }
In order to do anything more you need the form of projection that is available to the aggregation framework where your "query" and "projection" are represented as BSON documents using the $match and $project operators for the pipeline. In the shell form it looks like this:
db.collection.aggregate([
{ "$match": {
"Name": "fullquestion test 2"
}},
{ "$project": {
"Name": 1,
"Questions.type": 1
}}
])
Or with constructing the BSON documents for C#:
var match = new BsonDocument {
{ "$match",new BsonDocument {
{
"Name", "full question test 2"
}
}
}
};
var project = new BsonDocument {
{ "$project", new BsonDocument {
{ "Name", 1 },
{ "Questions.type": 1 }
}
}
};
var pipeline = new [] { match, project };
var result = collection.aggregate(pipeline);
Essentially, the $project stage of the aggregation pipeline can do a lot more things than just select fields. The additional support here allows for things such as "changing" the structure of the document inside the array.
Support for aggregation pipeline mapping to Linq is a work in progress. You can monitor the issues here: CSHARP-601
I'm trying to generate JSON using C# and DataContractJsonSerializer in .Net 3.5. The problem is that I can't figure out how to build the structure correct for the result I need.
I've tried to reproduce PHP's associative arrays using both hashtables, list objects and arraylists but can't figure out how to generate my result in the best way using DataContractJsonSerializer without having to create my own recursive loop for building JSON.
The closest approach is using the a Dictionary < string, Dictionary < string, string>> aproach but the result is too big as I can't "name" the keys.
This is what I get:
[
{
"Key":"status",
"Value":[
{
"Key":"value",
"Value":"ok"
}
]
},
{
"Key":"1",
"Value":[
{
"Key":"name",
"Value":"Result1"
},
{
"Key":"details",
"Value":"Result 1 details"
}
]
},
{
"Key":"2",
"Value":[
{
"Key":"name",
"Value":"Result2"
},
{
"Key":"details",
"Value":"Result 2 details"
}
]
},
{
"Key":"caller",
"Value":[
{
"Key":"value",
"Value":"1135345"
}
]
}
]
This is what I want:
{
"status":"ok",
"response":[
{
"id":1,
"name":"Result1"
"details":"Result 1 details"
},
{
"id":2,
"name":"Result2"
"details":"Result 2 details"
},
{
"id":3,
"name":"Result3"
"details":"Result 3 details"
],
"caller":"1135345"
}
Does anybody have a clue how I can generate this piece of JSON using C# without having to load the entire Json.NET framework? I need this to be generated as fast as possible as this project aims to become an site search engine.
You should look at using the JavaScriptSerializer. It's part of the .NET framework (distributed in System.Web.Extensions). To get the result you want you can do this:
var results = new[]
{
new{id=1,name="Result 1"},
new{id=2,name="Result 2"},
new{id=3,name="Result 3"}
};
var js = new JavaScriptSerializer();
var result = js.Serialize(new
{
status = "ok",
response = results,
caller = 1135345
});
You can either use anonymous classes, or any existing ones. Works perfectly fine :)
The return value of this call would be:
{"status":"ok","response":[{"id":1,"name":"Result 1"},{"id":2,"name":"Result 2"},{"id":3,"name":"Result 3"}],"caller":1135345}
Using the JavaScriptSerializer rather than DataContractJsonSerializer on a Dictionary will remove the Key/Value json attributes and make them into a "keyed" array.
http://msdn.microsoft.com/en-us/library/bb412168.aspx
Have you attributed your classes with the [DataContract] and [DataMember] decorators?
http://msdn.microsoft.com/en-us/library/bb412179.aspx