1
"firstChainPrices":{
"Prices":[
{
"minPrice":"29250.00",
"currency":"KZT",
"brand":"DV.CITDMB.Y.3.OPT",
"minPriceAvailable":4,
"artificialPriceForChain":"29250.00"
},
{
"minPrice":"24950.00",
"currency":"KZT",
"brand":"DV.CITDMB.Y.1.LIGHT",
"minPriceAvailable":4,
"artificialPriceForChain":"24950.00"
},
{
"minPrice":"37350.00",
"currency":"KZT",
"brand":"DV.CITDMB.Y.5.FLEX",
"minPriceAvailable":4,
"artificialPriceForChain":"37350.00"
}
],
"Prices":[
{
"minPrice":"39118.00",
"currency":"KZT",
"brand":"DV.CITDMB.Y.3.OPT",
"minPriceAvailable":4,
"artificialPriceForChain":"39118.00"
},
{
"minPrice":"28148.00",
"currency":"KZT",
"brand":"DV.CITDMB.Y.1.LIGHT",
"minPriceAvailable":4,
"artificialPriceForChain":"28148.00"
},
{
"minPrice":"53643.00",
"currency":"KZT",
"brand":"DV.CITDMB.Y.5.FLEX",
"minPriceAvailable":4,
"artificialPriceForChain":"53643.00"
}
]

} I want to deserialize this json to C# object. But this json contains different Prices array (2 in this case) but when I am deserializing I am getting only one Price array. This is not a complete json I have given here (I have removed some parts because json is too long)

Puneet Pant
  • 918
  • 12
  • 37
  • Does this answer your question? [How to Convert JSON object to Custom C# object?](https://stackoverflow.com/questions/2246694/how-to-convert-json-object-to-custom-c-sharp-object) – Vivek Nuna Jun 08 '20 at 07:22
  • 1
    that is very unusual JSON, and most serializers will preserve just the last value - see https://stackoverflow.com/questions/5306741/do-json-keys-need-to-be-unique for more detailed discussion - but: can you change the JSON layout at all? this choice is going to make life very hard for you – Marc Gravell Jun 08 '20 at 07:22
  • @viveknuna it is more complex than that - it is an atypical situation; it isn't very visible, but the JSON is `{ "Prices": [ ... ], "Prices": [...] }` – Marc Gravell Jun 08 '20 at 07:23
  • How is it deserialized? There is also a few curly braces missing here – Pavel Anikhouski Jun 08 '20 at 07:24
  • @MarcGravell *"most serializers will preserve just the last value"* do you mean some serializers are able to keep duplicated keys ? – Cid Jun 08 '20 at 07:29
  • @Cid I'm not ruling it out; however, even if you deserialize as `JObject` (Json.NET) - it erases the earlier keys *before* handing it back to you; I suspect you'd need to use `JsonReader` here, and do all the work yourself – Marc Gravell Jun 08 '20 at 07:33
  • 5
    In some ways the best solution to this issue would be to go back to the person who generated this JSON and ask them to output it in a sensible format without the duplicated keys – ADyson Jun 08 '20 at 07:48
  • This is not a valid JSON string – adiga Jun 08 '20 at 07:53
  • 1
    @adiga it is a *fragment*; the intent is perfectly clear – Marc Gravell Jun 08 '20 at 07:56

1 Answers1

0

while it is not the most elegant solution this can solve your problem. And with a bit of work it can be generalized if you need.

    private string fix(string Json, string JsonKey)
    {
        string Key = "\"" + JsonKey + "\":";
        Regex r = new Regex(Key);
        int i = 0;
        while (Json.Contains(Key)) {
            string NewKey = Key.Replace("\":", i.ToString().Trim() + "\":");
            Json = r.Replace(Json, NewKey, 1);
            i++;
        }
        return Json;
    }

Similarly, you could consider merging all lists under the given key. To have only 1 price list.

Cheers

ps: Generalizing this to correct any arbitrary duplicate key at the same section, requires that you keep track of the sections... Which would be a rather painful procedure. So if it is only Prices that causes you the problem and it doesnt appear elsewhere as a key this is a an easy simple fix ;-)

ps2: Ohh in case it was not clear... I replace each "Key" to "Key0", "Key1" etc... When you deserialize you should make a code that checks all entries and probably in your case merge them.

  • For your specific example and assuming you are always getting the Prices the one after the other (could be a safe assumption for your jsons) it is easy to merge them in one. – Theofrastos Mantadelis Jun 08 '20 at 07:56
  • 1
    Strongly agree with ADyson in the OP comments above. Data with multiple keys of the same name cannot be deserialized into a C# object. The result of this answer cannot be serialzed into a C# object either, because the number of Price sections is unknown in advance. You don't know, how many PriceX items will appear in the repaired result and would be required in the mapping object.. – Goodies Jun 08 '20 at 08:28