Click here to Skip to main content
15,887,294 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
I have JSON data coming from an application in the below format:
[{ "name":"First",
   "Custnumber":"123SC"
 },
 {"name":"Second",
   "Custnumber":"67BC"
 },
 {"name":"Third",
   "Custnumber":"99ABC"
 },
 {"name":"Fourth",
   "Custnumber":"123SC"
 }]

Now, I have to split this based on Custnumber with which should be like this:
[{ "name":"First",
   "Custnumber":"123SC"
 },
 {"name":"Fourth",
   "Custnumber":"123SC"
 }]

[{"name":"Second",
  "Custnumber":"67BC"
}]

[{"name":"Third",
  "Custnumber":"99ABC"
}]


What I have tried:

Please help me solving this. I am stuck here from 2 days.

Thanks in advance.
Posted
Updated 19-Jan-20 8:37am
v2
Comments
Christian Amado 19-Oct-17 19:10pm    
What did you tried?

Assuming the grouping will happen on the client side? Here is an example : Group json - JSFiddle[^]

The JSON will look like below after the grouping
HTML
{
   "123SC":[
      {
         "name":"First",
         "Custnumber":"123SC"
      },
      {
         "name":"Fourth",
         "Custnumber":"123SC"
      }
   ],
   "67BC":[
      {
         "name":"Second",
         "Custnumber":"67BC"
      }
   ],
   "99ABC":[
      {
         "name":"Third",
         "Custnumber":"99ABC"
      }
   ]
}


Here the output by looping the object

Custnumber 123SC has 2 customers :
---->1. First.
---->2. Fourth.
Custnumber 67BC has 1 customers :
---->1. Second.
Custnumber 99ABC has 1 customers :
---->1. Third. 


Reference:
javascript - Grouping JSON by values - Stack Overflow[^]
 
Share this answer
 
Just deserialise the json to objects, select the data from want from those objects then re-serialise.

[DataContract]
public class Data
{
    [DataMember(Name = "name")]
    public string Name { get; set; }
    [DataMember]
    public string Custnumber { get; set; }
}


string json = "[{\"name\":\"First\", \"Custnumber\":\"123SC\" }, { \"name\":\"Second\", \"Custnumber\":\"67BC\" }, { \"name\":\"Third\", \"Custnumber\":\"99ABC\" }, { \"name\":\"Fourth\", \"Custnumber\":\"123SC\" }]";

List<Data> data = null;

// System.Runtime.Serialization.Json.DataContractJsonSerializer is just one way to
// serialise JSON, use whatever way you want to do this bit
MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(json));
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(List<Data>));
data = ser.ReadObject(ms) as List<Data>;
ms.Close();

// get just the person you're interested in

data = data.Where(d => d.Custnumber == "123SC").ToList();

// serialize back to JSON

ms = new MemoryStream();

ser.WriteObject(ms, data);
ms.Position = 0;
json = Encoding.UTF8.GetString(ms.ToArray());
ms.Close();


you'll need these usings for that JSON serialiser, but as I said use whatever you normally use.

using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900