1

I have a JSON data like bellow

[{
    "CurrencyDenomination_JSON": "[{\"PK_MasterCurrencyDenomID\":1,\"CurrencyDenomination\":2000,\"NoofCurrency\":2,\"Total\":\"4000.00\",\"IsNote\":true},{\"PK_MasterCurrencyDenomID\":2,\"CurrencyDenomination\":500,\"NoofCurrency\":2,\"Total\":\"1000.00\",\"IsNote\":true}]"
}, {
    "CurrencyDenomination_JSON": "[{\"PK_MasterCurrencyDenomID\":1,\"CurrencyDenomination\":2000,\"NoofCurrency\":5,\"Total\":\"10000.00\",\"IsNote\":true},{\"PK_MasterCurrencyDenomID\":2,\"CurrencyDenomination\":500,\"NoofCurrency\":2,\"Total\":\"1000.00\",\"IsNote\":true}]"
}, {
    "CurrencyDenomination_JSON": "[{\"PK_MasterCurrencyDenomID\":1,\"CurrencyDenomination\":2000,\"NoofCurrency\":5,\"Total\":\"10000.00\",\"IsNote\":true},{\"PK_MasterCurrencyDenomID\":2,\"CurrencyDenomination\":500,\"NoofCurrency\":5,\"Total\":\"2500.00\",\"IsNote\":true}]"
}]

and I want to extract value from it and expect bellow data

[{
    "PK_MasterCurrencyDenomID": 1,
    "CurrencyDenomination": 2000,
    "NoofCurrency": 2,
    "Total": "4000.00",
    "IsNote": true
}, {
    "PK_MasterCurrencyDenomID": 2,
    "CurrencyDenomination": 500,
    "NoofCurrency": 2,
    "Total": "1000.00",
    "IsNote": true
}, {
    "PK_MasterCurrencyDenomID": 3,
    "CurrencyDenomination": 200,
    "NoofCurrency": 2,
    "Total": "400.00",
    "IsNote": true
}, {
    "PK_MasterCurrencyDenomID": 4,
    "CurrencyDenomination": 100,
    "NoofCurrency": 2,
    "Total": "200.00",
    "IsNote": true
}, {
    "PK_MasterCurrencyDenomID": 5,
    "CurrencyDenomination": 50,
    "NoofCurrency": 2,
    "Total": "100.00",
    "IsNote": true
}, {
    "PK_MasterCurrencyDenomID": 6,
    "CurrencyDenomination": 20,
    "NoofCurrency": 2,
    "Total": "40.00",
    "IsNote": true
}]

to do that I write bellow code ,and think this is not right way to do that there must be some smart way to do that .Please suggest me a better alternative.

JArray jsonArray = JArray.Parse(json);
List<MasterCurrencyDenomination> d = new List<MasterCurrencyDenomination>();
string strjson = string.Empty;
foreach (var item in jsonArray.Children())
{

strjson+= item["CurrencyDenomination_JSON"].ToString().Replace("[", "").Replace("]", ",");

}

d = JsonConvert.DeserializeObject<List<MasterCurrencyDenomination>>("["+strjson+"]");

2 Answers 2

2

If you observe the Json, it is evident that it is an Array of Type which contains a single String Property CurrencyDenomination_JSON. The Value of CurrencyDenomination_JSON is a JSON string.

What you need to do is, fetch these JSON strings (represented as IEnumerable<string>), retrieve JObject from them by parsing each of them and Serialize the collection.

var currencyArray = JArray.Parse(json).Children<JObject>()
                     .SelectMany(x=>x.Properties().Select(c=>c.Value.Value<string>()));

var result = JsonConvert.SerializeObject(currencyArray.SelectMany(x=>JArray.Parse(x)));
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks ,works fine .if you don't mind can you please describe the logic for en-reaching knowledge.
@kuntal I have updated the answer with description, please do let me know if your need further explanation
0

I think You can use the following code to do this.

   List<MasterCurrencyDenomination> items = new List<MasterCurrencyDenomination>();

        JToken.Parse(myJson).ToList().ForEach(x =>
        {
            items.AddRange(JsonConvert.DeserializeObject<List<MasterCurrencyDenomination>>(x.SelectToken("CurrencyDenomination_JSON").Value<string>()));
        });

OR:

        List<MasterCurrencyDenomination> items2 = new List<MasterCurrencyDenomination>();

        foreach (var test in JToken.Parse(myJson))
        {
            items2.AddRange(JsonConvert.DeserializeObject<List<MasterCurrencyDenomination>>(test.SelectToken("CurrencyDenomination_JSON").Value<string>()));
        }

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.