Home > Blockchain >  How to distinct json string in c# using LINQ without using a model class?
How to distinct json string in c# using LINQ without using a model class?

Time:01-18

I am looking for a generic solution for this json. so i can distinct this json with property name.

My client will provide the tag name "removeEmail" and property name in comma separated "WHAT,AMOUNT," etc. can be more property in it.

The result should be BEER 18.00 FOOD 12.00 CAR 20.00

JSON :

{
  "NUMBER": 3,
  "removeEmail": [
    {
      "WHAT": "Beer",
      "AMOUNT": 18.00
    },
    {
      "WHAT": "Food",
      "AMOUNT": 12.00
    },
    {
      "WHAT": "Food",
      "AMOUNT": 12.00
    },
    {
      "WHAT": "Car",
      "AMOUNT": 20.00
    }
  ]
}

CodePudding user response:

You can write a code something like this:

var arrayName = "removeEmail";
var props = "WHAT,AMOUNT";
var propArray = props.Split(",");

 var obj = JObject.Parse(json);
 var array = obj[arrayName] as JArray;

 var arrayResult = array.Children<JObject>()
                            .Select(item =>
                            {
                                var str = "";

                                foreach (var p in propArray)
                                {
                                    str  = $"{item[p]} ";
                                }
                                return str;
                            })
                            .Distinct()
                            .ToArray();


        var result = string.Join(" ", arrayResult);

You can add another properties to props.

CodePudding user response:

You can use Linq to Json (with Newtonsoft.Json) and then use Distinct().

var obj = JObject.Parse(json);
var items = (JArray)obj["removeEmail"];

var itemList = items.Children<JObject>()
                    .Select(item => $"{item["WHAT"]} {item["AMOUNT"]}")
                    .Distinct()
                    .ToList();

// output List<string>
Beer 18
Food 12
Car 20

If you want to retain the decimals then you can cast to decimal and format the string in the Select():

.Select(item => $"{item["WHAT"]} {((decimal)item["AMOUNT"]).ToString("0.00")}")

// output
Beer 18.00
Food 12.00
Car 20.00

Update

Since you said you want to access multiple properties then you could use string.Join to join the property values together in the Select:

var itemList = items.Children<JObject>()
             .Select(item => string.Join(" ", item.Properties().Select(p => p.Value)))
             .Distinct()
             .ToList();

Demo

CodePudding user response:

try this

var removeEmailParsed = (JArray) JObject.Parse(jsonOrig)["removeEmail"];  

List<string> list = removeEmailParsed.Select(ep => ConvertToItem(ep)).ToList();

public static string ConvertToItem(JToken jToken)
{
var item = jToken.Select(x=>x.Values().First()).ToArray(); 

return item[0].ToString()   "  "   Convert.ToDouble(item[1]).ToString("0.00");
}

result (in a json format)

[
  "Beer  18.00",
  "Food  12.00",
  "Food  12.00",
  "Car  20.00"
]

or in a string format

string items = string.Join(" ; ",list);

"Beer  18.00 ; Food  12.00 ; Food  12.00 ; Car  20.00"

  •  Tags:  
  • Related