Ankit
Member
- Joined
- Dec 16, 2020
- Messages
- 17
- Programming Experience
- 3-5
Below I have an array of json coming from different data sources. and my job is to get ids for each item of json and pass them to different container based on what source it is coming. the way to differentiate here is that items coming from Source A will have properties Id,Project and ProjectId only and items with source other than A will have more properties like Id, Name, Project, Class etc. below code returns me the id if it coming from source A. I would like to figure out a way where I can get the ids coming from sources other than source A and also list which id comes from which Source. I am considering only two sources here one is source A(meaning items with properties Id,Project and ProjectId) while all other items as Source B. How do I extend the existing function to achieve my result?
C#:
private List<string> GetFilteredDocIdsFromSBMessage(string sbMessage)
{
var data = JsonConvert.DeserializeObject<List<JObject>>( sbMessage );
List<string> filteredResult = new List<string>();
foreach(var recordingData in data )
{
string[] PropertiesFromSb = new string[3] { "id", "Project", "ProjectId" };
JProperty[] jProperties = recordingData.Properties().ToArray();
List<string> jPropertyName = new List<string>();
string id = string.Empty;
foreach( JProperty prop in jProperties )
{
jPropertyName.Add( prop.Name );
if(prop.Name == "Id" )
{
id = prop.Value.ToString();
}
}
var isSourceA = true;
foreach( string property in PropertiesFromSb )
{
if (!jPropertyName.Contains(property))
{
isSourceA = false;
}
}
if( isSourceA )
{
filteredResult.Add( id );
}
}
return filteredResult;
}