LaVOZs

The World’s Largest Online Community for Developers

'; c# - Compare two JSONs, if required fill one with default data - LavOzs.Com

I have two JSON files. Lets say they're structured like this:

products.json:

[
    {
    "Id": "1",
    "Index": "ABC",
    "Stock": 16.0,
    "Name": "The One Product"
    },
    {
    "Id": "2",
    "Index": "XYZ",
    "Stock": 1.0,
    "Name": "Productivity Tool"
    },...
]

productsSettings.json:

[
    {
    "Id": "1",
    "IsCool": true,
    "WouldIBuyIt": false
    }
]

products.json and productsSettings.json reference the same products by using Id. Both JSONs are later used to create a Product object.

Product list will regularly change - it may increase or decrease. If products.json is updated, productsSettings.json will be accordingly modified with defaults. So afterwards it would look like this:

[
    {
    "Id": "1",
    "IsCool": true, <-- user input
    "WouldIBuyIt": false
    },
    {
    "Id": "2",
    "IsCool": false, <-- default
    "WouldIBuyIt": false
    }
]

I know it's mostly a theoretical question, I don't really know where to start with it. I hope to receive at least general advice with it, perhaps there's a library that already does that? Maybe I should try a different approach?

use Newtonsoft.Json library and create C# objects from JSON strings. (If you know the corresponding class, then generate an object of that class) Then use reflections to compare your objects

First of all: you can convert your json to a c# class.

Visual studio offers a very useful command to convert json string to c# class definitiont: paste special

I suggest the use of nuget package "Newtonsoft.Json" to serialize/deserialize json to the c# class.

Then having values inside the c# class, you can iterate over element of products and see if they exists or not in productsSettings.

As it was said before - you can use newtonsoft json.net. I'll give some examples so it could be clear what to do:

1) Create C# objects with properties you need in both classes:

public class Products
{
    public List<YourProduct> ArrayProperty{ get; set; }
}

public class YourProduct
{
    public string Id { get; set; }
    public string Index { get; set; }
    public float Stock { get; set; }
    public string Name { get; set; }
}

public class YourSettings
{
    public List<ProductSettings> ArrayProperty { get; set; }
}

public class ProductSettings
{
    public string Id { get; set; }
    public bool IsCool { get; set; }
    public bool WouldIBuyIt { get; set; }
}

2) install JSON.net using nuget package manager,

3) load your JSON from file to string (if your json file will be very big, better use JsonReader or other object to work with JSON. You always can read documentation for that) 4) deserialize your json to object with this method:

string reader = MethodToReadJsonFromFile();
JsonSerializer serializer = new JsonSerializer();
var result = serializer.Deserialize<Products>(reader);

6) use it to get two object list and then you can use any proper way to merge them in any way you need (for example you can use LINQ for it, i guess)

7) And if you need JSON as a result, don't forget to serialize your result object to json string

More about serialization/deserialization: Json .net introduction

Related
Comparing two byte arrays in .NET
Binary Data in JSON String. Something better than Base64
Posting a File and Associated Data to a RESTful WebService preferably as JSON
is there a require for json in node.js
How do I POST JSON data with cURL?
How to parse JSON in Python?
How do I write JSON data to a file?
Fetch: POST json data
Elastic Search - Raw JSON ( unstructured data ) handling with elastic C# API