1

I have this object value that is being returned and I would like to convert it into a useful JSON object that I can inspect and manipulate. Ultimately, my goal is to validate the values of username and accessKey. But 2 things are throwing this off. Double {{ makes it invalid JSON and sauce:options can't be converted into a property in a class.

{{
  "browserName": "MicrosoftEdge",
  "browserVersion": "latest",
  "platformName": "Windows 10",
  "sauce:options": {
    "username": "test",
    "accessKey": "123"
  }
}}

Here is what I tried:

string output = JsonConvert.SerializeObject(SauceSession.Options.ConfiguredEdgeOptions);

This SauceSession.Options.ConfiguredEdgeOptions returns that object I mentioned above.

Got this back: Newtonsoft.Json.JsonSerializationException: 'Error getting value from 'BinaryLocation' on 'OpenQA.Selenium.Edge.EdgeOptions'.'

I also tried this as per suggestions:

var serialized = JsonConvert.SerializeObject(SauceSession.Options.ConfiguredEdgeOptions);

And got back this Newtonsoft.Json.JsonSerializationException: 'Error getting value from 'BinaryLocation' on 'OpenQA.Selenium.Edge.EdgeOptions'.'

Nikolay Advolodkin
  • 1,820
  • 2
  • 24
  • 28
  • 2
    This is...a JSON string? How are you "getting" this data? Are you looking to create a c# class to represent it? – gunr2171 Nov 25 '19 at 16:48
  • Are you getting stuck on making a property which has a `:` in the name? – gunr2171 Nov 25 '19 at 16:49
  • https://www.newtonsoft.com/json – Johnathan Barclay Nov 25 '19 at 16:50
  • There are myriad ways to do this. Please narrow your question. Including what you've tried so far can help do this. – Heretic Monkey Nov 25 '19 at 16:52
  • 2
    Does this answer your question? [Deserialize JSON with C#](https://stackoverflow.com/questions/7895105/deserialize-json-with-c-sharp) – Etheraex Nov 25 '19 at 16:53
  • Does this answer your question? [Prefix some characters to the property name - JSON Serialization](https://stackoverflow.com/questions/33654741/prefix-some-characters-to-the-property-name-json-serialization) – gunr2171 Nov 25 '19 at 16:53
  • If this is `json` it's not valid against RFC8259 or RFC7159. – Trevor Nov 25 '19 at 16:54
  • @Nikolay, look into Newtonsoft. This makes it VERY easy to convert JSON into an object. – Casey Crookston Nov 25 '19 at 17:09
  • You can deserialise the `x:y` property using `JsonProperty` but the double `{{` and `}}` mean this isn't valid JSON. – DavidG Nov 25 '19 at 17:28
  • @DavidG you're right that it's not valid JSON as jsonlint.com tells me. How do I handle this? – Nikolay Advolodkin Nov 25 '19 at 18:54
  • Fix the source of the JSON, or you will have to manually strip out these surrounding braces. – DavidG Nov 25 '19 at 19:05
  • @DavidG I can't fix the source as it's a 3rd party API. So you think to do something like this: var optionsString = SauceSession.Options.ConfiguredEdgeOptions.ToString().Substring(1, SauceSession.Options.ConfiguredEdgeOptions.ToString().Length - 2); The only challenge here is that now it's a string with new line characters and all that – Nikolay Advolodkin Nov 25 '19 at 19:20

1 Answers1

6

Since you cannot fix the source, you're going to have to apply a bodge to fix the JSON, for example this will work:

var fixedJson = sourceJson.Substring(1, Json.Length - 2);

Now you should have a couple of classes to hold your data, this way you can also cope with the unusual names:

public class Root
{
    public string BrowserName { get; set; }
    public string BrowserVersion { get; set; }
    public string PlatformName { get; set; }

    [JsonProperty("sauce:options")]
    public Options SauceOptions { get; set; }
}

public class Options
{
    public string Username { get; set; }
    public string AccessKey { get; set; }
}

And now you should be able to deserialise like this:

var root = JsonConvert.DeserializeObject<Root>(fixedJson);
DavidG
  • 113,891
  • 12
  • 217
  • 223
  • appreciate the solution. I got the following error: Newtonsoft.Json.JsonSerializationException: 'Error converting value "browserName" to type 'SimpleSauceTests.Root'. Path '', line 2, position 15.' – Nikolay Advolodkin Nov 25 '19 at 22:11
  • Then your source JSON doesn't have double braces. Just forget stripping them out. – DavidG Nov 25 '19 at 22:14