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.

9
  • 1
    I don't have the source of the JSON Object. Then where is the JSON? Commented Oct 19, 2015 at 6:47
  • @YuvalItzchakov JSON is coming from file. Think of like a tool to tune/append some data to JSON file at runtime Commented Oct 19, 2015 at 6:52
  • So input is any json and the output is a json that confirms to your json schema. Commented Oct 19, 2015 at 6:54
  • @singsuyash, output json which complies with source json structure. however I am appending some text which will not break the structure Commented Oct 19, 2015 at 6:55
  • Does the JSON have a known structure? Or will they be completely dynamic? Commented Oct 19, 2015 at 6:55

3 Answers 3

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"
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

@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 :)
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.

3 Comments

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
@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.
I do agree with you however I don't have rights to import (call it as restriction)
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.