2

I have JSON String which I am reading from file. I don't have the source of the JSON Object. So I can't call JsonConvert.DeserializeObject.

However I want check if the JSON String has specific structure, if yes, append some string or If not append the structure.

allmodules {
    feature: 'test-a'
}

submodules {
    //some data
}

Assume if there's not allmodules, I would like to append my structure

allmodules {
    feature: 'debug-a'
}

If it's already available, just append feature: 'debug-a'

And so on I have some custom work to do. Is there any efficient way to do this without breaking JSON format. Most of the questions regarding String to Object de-serialization, however as I mentioned I don't have original Object, and can't do that.

RaceBase
  • 18,428
  • 47
  • 141
  • 202
  • 1
    *I don't have the source of the JSON Object.* Then where is the JSON? – Yuval Itzchakov Oct 19 '15 at 06:47
  • @YuvalItzchakov JSON is coming from file. Think of like a tool to tune/append some data to JSON file at runtime – RaceBase Oct 19 '15 at 06:52
  • So input is any json and the output is a json that confirms to your json schema. – blogbydev Oct 19 '15 at 06:54
  • @singsuyash, output json which complies with source json structure. however I am appending some text which will not break the structure – RaceBase Oct 19 '15 at 06:55
  • Does the JSON have a known structure? Or will they be completely dynamic? – Yuval Itzchakov Oct 19 '15 at 06:55
  • @YuvalItzchakov, it has. however my tool shouldn't be checking against that. Think it like this, I am adding some JSON string which is compatible with source structure. – RaceBase Oct 19 '15 at 06:57
  • you want this json, just to test your functionality right? – blogbydev Oct 19 '15 at 06:58
  • @singsuyash, It's a tool to append some json string to source – RaceBase Oct 19 '15 at 07:00
  • @Reddy so you pretty much want to deserialize the JSON to a dynamic object and test if some properties are valid? [perhaps with a try/catch](http://stackoverflow.com/questions/2998954/test-if-a-property-is-available-on-a-dynamic-variable) or even reflection? – Prix Oct 19 '15 at 07:10

3 Answers3

2

You can do this using JObject and doing a little manual parsing. It could look something like this:

public string AppendAllModules(string json)
{
    var obj = JObject.Parse(json);
    JToken token;
    if (obj.TryGetValue("allmodules", out token))
        return json;

    obj.Add(new JProperty("allmodules", new JObject(new JProperty("feature", "test-a"))));
    return obj.ToString();
}

Given:

{
    "submodules": {
        "name": "yuval"
    }
}

Would yield:

{
  "submodules": {
    "name": "yuval"
  },
  "allmodules": {
    "feature": "test-a"
  }
}
Yuval Itzchakov
  • 146,575
  • 32
  • 257
  • 321
  • @singsuyash Your code doesn't meet the requirements of the OP. You simply append a new key value pair. I started writing this answer before you posted yours :) – Yuval Itzchakov Oct 19 '15 at 07:45
1

I don't have the source of the JSON Object.

No worries, you can simply construct a new C# object that it compatible with the JSON definition. There are a number of options listed at

How to auto-generate a C# class file from a JSON object string

Once you have a compatible C# class in your project, you can deserialize the JSON and manipulate it as an object, just as if you had the original object.

Community
  • 1
  • 1
Eric J.
  • 147,927
  • 63
  • 340
  • 553
  • J, what if the source JSON string changes. I don't have specific model that JSON will be like this. it might have some child attributes or may not be. The point is I don't have to understand the entire JSON of the same, just to go with append my customization and move on – RaceBase Oct 19 '15 at 06:54
  • @Reddy: If you deserialize to a type, type safety will protect you from incompatible changes that you may be ignorant to if you simply manipulate the JSON as a string. My preference would be to have my code break if a third-party dependency changes unexpectedly, rather than blindly processing that dependency. – Eric J. Oct 19 '15 at 07:06
  • I do agree with you however I don't have rights to import (call it as restriction) – RaceBase Oct 19 '15 at 07:09
0

use either JObject.FromObject or JObject.Parse to get your file json string into a JObject. Then the below example code may help. I am going via an If/else way because you mentioned you can not get the exact structure.

JObject obj = JObject.FromObject(
        new {Id = 5, Name = "Foo"}
    );
    JToken jtok = null;
    bool found = obj.TryGetValue("Bar",StringComparison.CurrentCultureIgnoreCase, out jtok);
    if (!found)
    {
        obj.Add("Bar","this is added");
    }
    else
    {
        Console.WriteLine(jtok);
    }

    Console.WriteLine(obj["Bar"]);

Of course after you are done editing your JObject you can use the JObject.ToString() method to get the string representation and send it over to the file.

blogbydev
  • 1,445
  • 2
  • 17
  • 29