-2

I'm getting unquoted json string with dynamic properties from API and I need to check if specific property exist, and if it does I need to get it's value. For example I'm getting this string:

"{overridedCondition:used,makerAndModelMissing:true}"

I need to check for 'overridedCondition', if it exist I need to get it's value. I tried something like this but I'm getting exception like 'Error parsing undefined value. Path 'overridedCondition'':

var json = "{overridedCondition:used,makerAndModelMissing:true}";

var result = JsonConvert.DeserializeObject<dynamic>(json);

I also tried parsing it with

JObject.Parse(json);

but that didn't worked either.

Does anyone have some suggestion on how to parse this string so I'm able to access it's values.

I'm using .net 6 for this project. Thanks

kajdzo
  • 62
  • 5
  • 1
    Are all your striings like this one or you can have some more complicated? – Serge Jun 17 '23 at 18:57
  • 5
    The problem is that your "json string" is not actually JSON, it's malformed. You can't parse a malformed JSON string with a JSON parser, you'll get an error. In order to parse that, you need to know the actual grammar. For instance, how are string values that include colons represented e.g. ISO 8601 time strings like `18:18:24Z`? Strings with embedded commas? How are arrays represented? Nested objects? – dbc Jun 17 '23 at 18:58
  • 2
    If your inputs are simple key/value pairs with no nesting and no embedded quotes, you might just parse them manually with a string split rather than with a serializer. Or use a regex to fix the input, see e.g. [How to Fix JSON Key Values without double-quotes?](https://stackoverflow.com/q/50947760). – dbc Jun 17 '23 at 19:01
  • 2
    Correct JSON: `{ "overridedCondition": "used", "makerAndModelMissing": true }`. See: [JSON](https://www.json.org/json-en.html). Shouldn't the first property be named `overriddenCondition`? "overrided" does not sound English. – Olivier Jacot-Descombes Jun 17 '23 at 19:39
  • @OlivierJacot-Descombes yes, you're right regarding the name, it is a typo. – kajdzo Jun 17 '23 at 20:12
  • @dbc That is the issue it is a dynamic string, I never know what I'm going to get, it is a peace of some scrape data. I ended up fixing string via regex, with help of the question you suggested, to add quotes and than I was able to do JObject.Parse(json). From there it is straight forward to achieve the logic I need. Thanks everyone! – kajdzo Jun 17 '23 at 21:07

1 Answers1

1

if all your string are like the one you posted, you can try this code

   var s = "{overridedCondition:used,makerAndModelMissing:true}";

    var jObj = new JObject(s.Substring(1, s.Length - 2)
                            .Split(",", StringSplitOptions.RemoveEmptyEntries)
                            .Select(x => x.Split(":", StringSplitOptions.TrimEntries))
                            .Select(x => new JProperty(x[0], x[1])));

    string overridedCondition = (string)jObj["overridedCondition"];

if you need a valid json string

    string json = jObj.ToString();

UPDATE

You can try to fix a json string as you want and parse it to a JObject, but IMHO it is not a reliable way

var jObj = JObject.Parse(s.Replace("{", "{\"")
                          .Replace("}", "\"}")
                          .Replace(":", "\":\"")
                          .Replace(",", "\",\""));
Serge
  • 40,935
  • 4
  • 18
  • 45
  • The string is the result of scrape data so I never know what I'm going to get or even if I'm going to get anything so I ended up fixing string to proper JSON format and parsing it. – kajdzo Jun 17 '23 at 21:17
  • What do you have it is not an invalid json string, it is just a string. IMHO it is not the best way try to convert a string to a json string and after this parse it to JObject. It is much more relailable to create a JObject from a string with delimeters. You have much more ways to validate an original string. – Serge Jun 17 '23 at 21:26