1

I have a json file, where i have to validate a json attribute element value based on another json element attribute value. But if there json elements with the same name. It always takes the last value always instead of parsing the json data fully. Please guide me.

Below the sample json file

    {
   "PLMXML":{
      "language":"en-us",
      "author":"Developer",
      "date":"2020-05-22",
      "traverseRootRefs":"#id6",
      "Operation":{
         "id":"id21",
         "subType":"BS4_BaOP",
         "catalogueId":"70700000209604"
         },
      "Operation":{
         "id":"id28",
         "subType":"BS4_BaOP",
         "catalogueId":"70700000209603"
         },
      "OperationRevision":{
         "id":"id6",
         "subType":"BS4_BaOPRevision",
         "masterRef":"#id21",
         "revision":"A1"
         }
      }
}

And below the code which im trying to use

public void Readjsonfile(string jsondata)
{   
    var message = JsonConvert.DeserializeObject<plmxmldatamodel>(jsondata);      

    if (String.Equals(message.PLMXML.traverseRootRefs.Substring(1), message.PLMXML.OperationRevision.id))
    {   
        Console.WriteLine("Condtion1");         
        if (String.Equals(message.PLMXML.OperationRevision.masterRef.Substring(1), message.PLMXML.Operation.id))
        {
            Console.WriteLine("Condition_2");
            //Do something based on the condtion
        }               
    }
}
public class Operation
{
    public string id { get; set; }
    public string subType { get; set; }
    public string catalogueId { get; set; }
}
public class OperationRevision
{
    public string id { get; set; }
    public string subType { get; set; }
    public string masterRef { get; set; }
}
public class PLMXML
{
    public string language { get; set; }
    public string author { get; set; }
    public string date { get; set; }
    public string traverseRootRefs { get; set; }            
    public Operation Operation { get; set; }
    public OperationRevision OperationRevision { get; set; }
}
public class plmxmldatamodel
{
    public PLMXML PLMXML { get; set; }
}

When i try to dedug this in the second if condtion, the value for message.PLMXML.Operation.id is always id28 , because of which second if condition fails. While the first if condition is passed as there is only one message.PLMXML.OperationRevision.id. i wanted behaviour where it would check complete json data and check if message.PLMXML.Operation.id with value id21 is present or not , So my data gets passed. Please kindly guide me here.I am very new to C# here.

1 Answers1

1

From my observation you have couple of issues.

What happen you have double keys, and your parser taking the last value not the first one.

First of all your json should be corrected. I assume you have access to change your json and operation should be an array like follow:

{
   "PLMXML":{
      "language":"en-us",
      "author":"Developer",
      "date":"2020-05-22",
      "traverseRootRefs":"#id6",
      "Operations":[
         {
            "id":"id21",
            "subType":"BS4_BaOP",
            "catalogueId":"70700000209604"
         },
         {
            "id":"id28",
            "subType":"BS4_BaOP",
            "catalogueId":"70700000209603"
         }
      ],
      "OperationRevision":{
         "id":"id6",
         "subType":"BS4_BaOPRevision",
         "masterRef":"#id21",
         "revision":"A1"
      }
   }
}

When array in place than use an online tool like to validate your json and use this tool to create a model.

Your model will be like this:

public partial class PlmxmlDataModel
{
    [JsonProperty("PLMXML")]
    public Plmxml Plmxml { get; set; }
}

public partial class Plmxml
{
    [JsonProperty("language")]
    public string Language { get; set; }

    [JsonProperty("author")]
    public string Author { get; set; }

    [JsonProperty("date")]
    public DateTimeOffset Date { get; set; }

    [JsonProperty("traverseRootRefs")]
    public string TraverseRootRefs { get; set; }

    [JsonProperty("Operations")]
    public Operation[] Operations { get; set; }

    [JsonProperty("OperationRevision")]
    public OperationRevision OperationRevision { get; set; }
}

public partial class OperationRevision
{
    [JsonProperty("id")]
    public string Id { get; set; }

    [JsonProperty("subType")]
    public string SubType { get; set; }

    [JsonProperty("masterRef")]
    public string MasterRef { get; set; }

    [JsonProperty("revision")]
    public string Revision { get; set; }
}

public partial class Operation
{
    [JsonProperty("id")]
    public string Id { get; set; }

    [JsonProperty("subType")]
    public string SubType { get; set; }

    [JsonProperty("catalogueId")]
    public string CatalogueId { get; set; }
}

And your method like this:

public void Readjsonfile(string jsondata)
{
    var message = JsonConvert.DeserializeObject<PlmxmlDataModel>(jsondata);

    if (String.Equals(message.Plmxml.TraverseRootRefs.Substring(1), message.Plmxml.OperationRevision.Id))
    {
        Console.WriteLine("Condtion1");
        if (String.Equals(message.Plmxml.OperationRevision.MasterRef.Substring(1), message.Plmxml.Operations[0].Id))
        {
            Console.WriteLine("Condition_2");
            //Do something based on the condtion
        }
    }
}

Now in your method I am looking for array index 0 with contain id 28, but if you are look for id 28 in any of the array then you can do some thing like:

if (message.Plmxml.Operations.Any(e => e.Id == message.Plmxml.OperationRevision.MasterRef.Substring(1)))
Maytham Fahmi
  • 31,138
  • 14
  • 118
  • 137
  • Hi Sir, I have corrected the json, which i missed while removing more atrributes for the sample data,Thanks for your valuable time,I have gone through your post, currently the json file is coming from a different system, also the json file is generated from a xml before i receive the actual json file. So i am getting the file , the way i posted it.How do you suggest me in this case . – user1539205 Jun 03 '20 at 20:03
  • 1
    as far as I know this is not possible, you can not solve inconsistent datastructure with magic. either your xml is wrong or it is converted to json in wrong way. the only solution is to find out the pattern of your differetn json and write a customzer parser to solve what you are looking for. look at this https://stackoverflow.com/questions/42384565/return-json-object-with-duplicate-keys-using-c-sharp and this https://stackoverflow.com/questions/20714160/how-to-deserialize-json-with-duplicate-property-names-in-the-same-object – Maytham Fahmi Jun 04 '20 at 01:23