so I want to be able to extract and ID based on whether that object has a particular property. I NEED this to be done via Regex. Here is an example of the JSON I am working with:
{
"workspaceid": ws01,
"data": {
"workspacetitle": "My Workspace"
},
"collections": {
"projects": [{
"id": 01,
"data": {
"title": "My Project 01",
"enddateperiod": "2020-02-20T23:59:59",
"profilecomplete": true,
"synced": false
},
"lists": {
"projectcode": [{
"id": pcodered,
"data": {
"code": "myproject123",
"name": "OffshoreProject"
}
}]
}
}, {
"id": 02,
"data": {
"title": "My Project 02",
"enddateperiod": "2020-02-20T23:59:59",
"profilecomplete": false,
"synced": false
},
"lists": {
"projectcode": [{
"id": pcodered,
"data": {
"code": "myproject123",
"name": "OffshoreProject"
}
}]
}
}]
}}
So what I want to extract is the ID of the project whose profile is not complete ("profilecomplete":false). So in this case, I want to select Project 2's id (which is 02).
How can I do this via Regex? I've managed to remove all of the whitespace and new lines as well so the JSON is essentially all one long line. Would it be easier to extract the Regex like this? Either way, I could use some help on how to get this ID.
NOTE: The format of the JSON cannot change.
This one works
/"id": ([^,]*?)(?=,[^{]*{[^}]*"profilecomplete": false)/
Explanations :
Read all these chars first "id":[space]
Then read in a group chars that aren't ","
And then a lookahead : you expect "," then chars that aren't "{", then "{"; and finally, before matching the closing "}", you want to read "profilecomplete": false
But I agree that a JSON parser would have been my preferred option!
Related
Have tried several methods described but not winning. Here is the value ofthe var valuesEntity (this is from Luis integration into bot framework).
I have not found a way to interrogate this var so that I get a variable with the value of Venue - in this case Bakery.
{{
"$instance": {
"Venue": [
{
"startIndex": 13,
"endIndex": 19,
"text": "bakery",
"type": "Venue"
}
]
},
"Venue": [
[
"BAKERY"
]
]
}}
"Venue" is an string[][], Venue[0][0], should return "BAKERY".
This would be a valid json:
{
"$instance": {
"Venue": [{
"startIndex": 13,
"endIndex": 19,
"text": "bakery",
"type": "Venue"
}]
},
"Venue": [
[
"BAKERY"
]
]
}
You can check for valid json on https://jsonlint.com/
The key was to deserialize the Jason object to a Jason string.
jsonData is
{
"S_Client.packet": [
{
"DESC": "2",
"NO": 2,
"SEND": [
{
"PARAM": "1",
"TYPE": null,
"VALUE": "11"
}
],
"RECEIVE": []
}
]
}
I want get data set using method( JsonConvert.DeserializeObject(jsonData) ) but "RECEIVE":[] < last character run Exception..
different types was well done..
and not first data(DataTable in DataSet and blank array) was also well done..
I think that was bug to report
This question already has answers here:
Order of serialized fields using JSON.NET
(15 answers)
JSON order mixed up
(17 answers)
Closed 6 years ago.
I tryed to read a json file, edit it and then save it again, everything works fine except the order of the values in the new json file is wrong.
Here a part of the Original Json file:
"files": [
{
"name": "Game.cfg",
"sections": [
{
"name": "Chat",
"settings": [
{
"name": "ChatX",
"value": "44"
},
{
"name": "ChatY",
"value": "74"
},
{
"name": "Transparency",
"value": "0.0000"
}
]
},
and here the the same part in the new json file i create:
"files": [
{
"sections": [
{
"settings": [
{
"name": "ChatX",
"value": "44"
},
{
"name": "ChatY",
"value": "74"
},
{
"name": "Transparency",
"value": "0.0000"
}
],
"name": "Chat"
},
and here the code if needed:
string filename = filesource;
var res = JsonConvert.DeserializeObject<PersistentSettings>(File.ReadAllText(filename));
List<PersistentSettings> pers = new List<PersistentSettings>();
pers.Add(res);
string json = JsonConvert.SerializeObject(pers, Formatting.Indented);
//write string to file
File.WriteAllText("test.json", json);
like i said it works just not as it should, anyone got an idea why?
i try to slit a text and put it into a dictionary , the problem i my text doesn't have a a clear structure :
text :
{
"about": "where I'm meant to be...",
"bio": "Visit my official blog at:\n\nhttp://ABC.com/ \n\nAdd me on Twitter:\n\nhttp://www.ABC.com/ABC",
"category": "Public figure",
"is_published": true,
"location": {
"street": "",
"city": "Los Angeles",
"state": "CA",
"country": "United States",
"zip": ""
},
"talking_about_count": 254637,
"username": "ABC",
"website": "http://kimkardashian.celebuzz.com/\nhttp://www.twitter.com/kimkardashian\n",
"were_here_count": 0,
"id": "114696805612",
"name": "ABC",
"link": "http://www.ABC.com/ABC",
"likes": 0,
"cover": {
"cover_id": "000000000",
"source": "http://ABC.jpg",
"offset_y": 0,
"offset_x": 200
}
}
As you see i have the "," as a delimiter , the problem is that there some composed objects like the :
"location": {
"street": "",
"city": "Los Angeles",
"state": "CA",
"country": "United States",
"zip": ""
},
that's why I can't use the string.Split(' ');
i heard about the regular expressions but I don't know how to use them
Is there any solution to get those information separated into a dictionary or any other structure
Your data is in a standard format (JSON) and there are parsers already written for it. You can download Json.NET easy through NuGet in Visual Studio.
Regular expressions are a powerful tool that makes pattern matching a lot simpler. For me that's as far as they go. They can be used to create parsers and all sorts of other things, but it's complicated.
So you could create your own JSON parser using regular expressions, but it'll take a lot of time. It would be like building a lockpick when there is a key available.
JavaScriptSerializer may satisfy your needs
using System.Web.Script.Serialization;
var jss = new JavaScriptSerializer();
var dict = jss.Deserialize<Dictionary<string,string>>(jsonText);
Console.WriteLine(dict["some_number"]);
See: http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.aspx
I'm using Json.NET in a .NET 4.0 application in order to convert a JSON RESTful response into XML. I am running into issues converting JSON into XML if a JSON child key has a space.
So far, I am able to convert most JSON responses.
Here are example responses along with the code which I am using to generate the XML.
{
num_reviews: "2",
page_id: "17816",
merchant_id: 7165
}
And here is the response which is causing an error:
[
{
headline: "ant bully",
created_date: "2010/06/12",
merchant_group_id: 10126,
profile_id: 0,
provider_id: 10000,
locale: "en_US",
helpful_score: 1314,
locale_id: 1,
variant: "",
bottomline: "Yes",
name: "Jessie",
page_id: "17816",
review_tags: [
{
Pros: [
"Easy to Learn",
"Engaging Story Line",
"Graphics",
"Good Audio",
"Multiplayer",
"Gameplay"
]
},
{
Describe Yourself: [
"Casual Gamer"
]
},
{
Best Uses: [
"Multiple Players"
]
},
{
Primary use: [
"Personal"
]
}
],
rating: 4,
merchant_id: 7165,
reviewer_type: "Verified Reviewer",
comments: "fun to play"
},
{
headline: "Ok game, but great price!",
created_date: "2010/02/28",
merchant_group_id: 10126,
profile_id: 0,
provider_id: 10000,
locale: "en_US",
helpful_score: 1918,
locale_id: 1,
variant: "",
bottomline: "Yes",
name: "Alleycatsandconmen",
page_id: "17816",
review_tags: [
{
Pros: [
"Easy to Learn",
"Engaging Story Line"
]
},
{
Describe Yourself: [
"Frequent Player"
]
},
{
Primary use: [
"Personal"
]
},
{
Best Uses: [
"Kids"
]
}
],
rating: 3,
merchant_id: 7165,
reviewer_type: "Verified Reviewer",
comments: "This is a cute game for the kids and at a great price. Just don't expect a whole lot."
}
]
So far, I have been considering on creating a mapping of the JSON data to a C# object and generating XML for that class. However, is there a way to keep this dynamic? Or is there a way to treat spaces as %20 encodings?
This question is same as how to validate JSON string before converting to XML in C#
If you have any further queries, please let me know.
You can call XmlConvert.EncodeName, which will escape any invalid characters using _s.
For example, a space would become _x0020_.
You cannot have an XMLElement Name with a space in it. You would need to replace the space with an Underscore or anyother element. If that is not feasible for you, try putting that value as an attribute for that Node.
I hope this makes sense.