Here is the what I am trying to achieve here:
Get a list of file names and IDs from the database
Search for those files on a network path
Store any IDs of the files not found
Search for that ID in a second database
Search for those files on a network path
List all ID's where files are not found on either location.
The issue I an encountering is trying to use the file names from results I have collected.
When running the code, the raw JSON data collected from the database gets displayed, however when trying to list just the file names I get nothing (not even an error)
Any ideas on how to fix this and list the file names in a way that would also let me search for them later? Or is there a better way to do this?
Also note: due to the version of SQL server being used i have to use FOR XML as FOR JSON is not compatible.
EDIT: Using code provided by Prany I am now able to output just the Audio File Name, but it I try and also output the sCallId I get a duplication Issue (see below output):
Getting Calls
2016\03\30\300320161614210106361-00000934412405.asf--84377-3668343241-514513
2016\03\30\300320161614210106361-00000934412405.asf--84385-3668343557-255773
2016\03\30\300320161614210106361-00000934412405.asf--84392-3668344445-516453
2016\03\30\300320161614210106361-00000934412405.asf--85000-3668749568-733799
2016\03\30\300320161614210106361-00000934412405.asf--85604-3668872399-722313
2016\03\30\300320161620220106362-00000934052048.asf--84377-3668343241-514513
2016\03\30\300320161620220106362-00000934052048.asf--84385-3668343557-255773
2016\03\30\300320161620220106362-00000934052048.asf--84392-3668344445-516453
2016\03\30\300320161620220106362-00000934052048.asf--85000-3668749568-733799
2016\03\30\300320161620220106362-00000934052048.asf--85604-3668872399-722313
2016\03\30\300320161634220106363-00000933211384.asf--84377-3668343241-514513
2016\03\30\300320161634220106363-00000933211384.asf--84385-3668343557-255773
2016\03\30\300320161634220106363-00000933211384.asf--84392-3668344445-516453
2016\03\30\300320161634220106363-00000933211384.asf--85000-3668749568-733799
2016\03\30\300320161634220106363-00000933211384.asf--85604-3668872399-722313
2016\04\04\040420160908190106389-00000527974488.asf--84377-3668343241-514513
2016\04\04\040420160908190106389-00000527974488.asf--84385-3668343557-255773
2016\04\04\040420160908190106389-00000527974488.asf--84392-3668344445-516453
2016\04\04\040420160908190106389-00000527974488.asf--85000-3668749568-733799
2016\04\04\040420160908190106389-00000527974488.asf--85604-3668872399-722313
2016\04\05\050420161913220106406-00000405271715.asf--84377-3668343241-514513
2016\04\05\050420161913220106406-00000405271715.asf--84385-3668343557-255773
2016\04\05\050420161913220106406-00000405271715.asf--84392-3668344445-516453
2016\04\05\050420161913220106406-00000405271715.asf--85000-3668749568-733799
2016\04\05\050420161913220106406-00000405271715.asf--85604-3668872399-722313
Below Is the code I am currently using to try and do this.
//Run the SQL and wrap the output in results tags to fix Multiple Root Elements error.
string liveXML = "<results>" + cmd2.ExecuteScalar().ToString() + "</results>";
//Create new XML Document
XmlDocument LiveDoc = new XmlDocument();
LiveDoc.LoadXml(liveXML);
//Conver XML to JSON
sjsonLive = JsonConvert.SerializeXmlNode(LiveDoc);
//Output RAW JSON
txtOut.AppendText("\r\n" + sjsonLive);
//Parse JSON into an Array
var files = JObject.Parse(sjsonLive);
//We want to run this values in a files seach, but for now let's print it to txtOut
foreach (var f in files.SelectTokens("$..calls..#audioFileName"))
foreach (var c in files.SelectTokens("$..calls..#sCallID"))
{
txtOut.AppendText("\r\n" + f.ToString() + " - " + c.ToString());
//Conduct File Search Here...
}
Example JSON Data:
{
"results": {
"calls": [{
"#audioFileName": "2016\\03\\30\\300320161614210106361-00000934412405.asf",
"#sCallID": "84377-3668343241-514513"
}, {
"#audioFileName": "2016\\03\\30\\300320161620220106362-00000934052048.asf",
"#sCallID": "84385-3668343557-255773"
}, {
"#audioFileName": "2016\\03\\30\\300320161634220106363-00000933211384.asf",
"#sCallID": "84392-3668344445-516453"
}, {
"#audioFileName": "2016\\04\\04\\040420160908190106389-00000527974488.asf",
"#sCallID": "85000-3668749568-733799"
}, {
"#audioFileName": "2016\\04\\05\\050420161913220106406-00000405271715.asf",
"#sCallID": "85604-3668872399-722313"
}
]
}
}
Edit:
You can use below token selector
files.SelectTokens("$..calls..#audioFileName")
edit 2:
var calls = files.SelectTokens("$..calls..#sCallID").ToList();
var audiofiles = files.SelectTokens("$..calls..#audioFileName").ToList();
for (int i = 0; i <= audiofiles.Count; i++)
{
//Conduct File search
if (true)
{
//access by index like audiofiles[i] and append to the query
}
else
{
//access calls by index like calls[i] and append to the query
}
}
Related
I created an ASP.NET Application, where I have to parse a csv file with a json structure.
The csv file itself is structured like:
{"Instance":"abc","Date":"2019-06-03T00:00:02.056Z","Identification":"someFunction","Type":"DurationInMs","Value":"5","iserror":"False""}
I get the jsonCsvData as a string and tried to parse it. Then I want to save some of the elements of this json object into a db.
public IActionResult ReadJsonCsvData(string jsonCsvData)
{
Console.WriteLine("ReadJsonCsvData");
ChartData chartData = new ChartData();
var lines = jsonCsvData.Split("\n");
foreach (var line in lines)
{
var values = JObject.Parse(line);
var first = string.Concat(values["Instance"]); //Output for first: ""
}
}
The problem now is, that the variable first is an empty string. The result should be (like in the json structure example above) "abc".
Thank you in advance!
I don't know if it will help but here is my solution (remove one of " at the end of your Json).
I use the "Jobject" to parse Json as I want. Import this two reference.
using Newtonsoft.Json.Linq;
using Newtonsoft;
Then you have to create your JObject :
JObject o = JObject.Parse(myJsonString);
Then to retrieve specifics data, you just have to search in your object just like you do with a dictionary with key :
instanceFromJson = o["Instance"].ToString;
dateFromJson = o["Date"].ToString;
If you have a table in your "instance" json object you can retrieve all data from this list just like that :
foreach (var item in o["Instance"]["tabFromInstanceObject"])
{
MyList.Add(item);
}
I'm creating software where the users can create and load profiles to fill textboxes. The names and other information contained in the profile are stored in a JSON file. A profile name can contain any text that is entered by the user.
So for this, I'm trying to get each objects names of the JSON file (= each profile name) to display them in a treeview, but all I get is their contents.
I have a JSON file containing two objects:
[
{
"profile1": {
//Some informations 1
},
"profile2": {
//Some informations 2
}
}
]
For now, I have code that allows me to get the value of a given tag, but I can't find a way to get the name of each objects:
using (StreamReader r = File.OpenText(path))
{
string json = r.ReadToEnd();
dynamic array = JsonConvert.DeserializeObject(json);
foreach (var item in array)
{
debug_tb.Text += item.profile1; //Gives me each values of the "profile1 object"
}
}
So what I'm trying to get is to display "profile1" and "profile2" and "profile3" if it exists.
Your problem is that your JSON is an array with one object. So you can simplefy the JSON first:
{
"profile1": {
//Some informations 1
},
"profile2": {
//Some informations 2
}
}
Then you can easyly iterate over every item in the JSON and get the Name of it
dynamic array = JsonConvert.DeserializeObject("{ \"profile1\": { }, \"profile2\": { } }");
foreach (var item in array)
{
debug_tb.Text += item.Name; //Gives the name of the object
}
Console.WriteLine(text);
Console.ReadLine();
This is my first time ever looking at RDF, and after trying, I've no idea how to parse it. I'm looking at some RDF (in Turtle format) used in the AFF4 file system, here's a portion of it:
<aff4://0295fab8-94b7-4435-bdb3-932cf48e40bd>
a aff4:ImageStream ;
aff4:chunkSize "32768"^^xsd:int ;
aff4:chunksInSegment "2048"^^xsd:int ;
aff4:compressionMethod <http://code.google.com/p/snappy/> ;
aff4:imageStreamHash "82798a275176aa141a2993ca8931535b1303545a0954473f5c5e55b4d8d5a8e3ebdb9e9323e5ecfaf65f8d379a8e2b9150750f5cf44851cf4edb6a2e05372f42"^^aff4:SHA512 ;
aff4:imageStreamIndexHash "039eb2da046cfb8c8d40e6f9b42aae501fb36f9b09b5f29d660d3637f87c37c98c3ee3b995265adff1d2b971fa795317333bf50200e72fdfe9fa96acdb88b6d0"^^aff4:SHA512 ;
aff4:size "185335808"^^xsd:long ;
aff4:stored : ;
aff4:target <aff4://92015053-5f7b-4e5a-a1e7-901d8943cf1f> ;
aff4:version "1"^^xsd:int .
There's a lot of this stuff in the file, but I've no idea how to access any of it, thus far I've cobbled together:
private static void ParseInformationStream(Stream informationStream)
{
Console.WriteLine("Parsing information.turtle file: ");
informationStream.Position = 0;
TurtleParser turtleParser = new TurtleParser();
Graph graph = new Graph();
turtleParser.Load(graph, new StreamReader(informationStream));
foreach (var triple in graph.Triples)
{
Console.WriteLine(triple.Subject);
}
}
This prints out some of the data, but if for example, I wanted to access the aff4:compressionMethod (node?) specifically, how would I go about doing that? I've been reading about Sparql, but it all seems a bit overkill for what I need.
Any input or advice would be appreciated.
You can use the methods of the IGraph interface to access the contents of the parsed graph. For example the following will retrieve all image streams (in Turtle "a" is a shortcut for the rdf:type predicate) and print out the compression method for each stream:
// Get the node for rdf:type
var rdfType = graph.CreateUriNode(new Uri(RdfSpecsHelper.RdfType));
// Get the node for the aff4:ImageStream type
var imageStream = graph.GetUriNode("aff4:ImageStream");
// Get the node for the aff4:compressionMethod predicate
var compressionMethod = graph.CreateUriNode("aff4:compressionMethod");
// Get the streams (the subject of x a aff4:ImageStream in the Turtle)
var imageStreams = graph.GetTriplesWithPredicateObject(rdfType, imageStream).Select(t => t.Subject);
foreach (var streamInstance in imageStreams)
{
// Get the first compressionMethod value for the stream instance
var compression = graph.GetTriplesWithSubjectPredicate(streamInstance, compressionMethod)
.Select(t => t.Object).FirstOrDefault();
Console.WriteLine("Stream " + streamInstance + " uses compression method " + compression);
}
For more on accessing nodes an triples in a graph in dotNetRDF, please see https://github.com/dotnetrdf/dotnetrdf/wiki/UserGuide-Working-With-Graphs
I saved my file in the database, I want to get that file for sending mail If I wrote where the condition
public EMS_PROFILE_UPLOAD_MASTER GetHrUploadeProfile(string EnqId)
{
var x = from n in db.EMS_PROFILE_UPLOAD_MASTER
where n.ENQUIRY_CODE== EnqId
select n;
foreach(var fileData in x)
{
var _FilData = fileData.FILEDATA;
}
return x.FirstOrDefault();
}
I'm getting data but here I have multiple files in my database, how can I differentiate that file?
The fact that you have multiple files with the same ID - means you cannot use the ID as the selector. Inside your code example, it seems that there may be other meta-data from that fileData object - what is inside that to further filter or control your selection for the correct file?
I have written some codes to store image files in MongoDB. Now I want to filter and retrieve some images from the mongoDB. I want to filter out some images which has some set of characters on the image name.
For Ex: say I have stored aaaa_DEX.jpg, bbbb_DEX.jpg, cccc_BVX.jpg, dddd_OUI.jpg, eeee_DEX.jpg images in mongoDB and I want to get all the images which has the "DEX" on there names. Will it be possible with Query builder? How can I do this?
To upload I use:
public JsonResult UploadPrimaryImage(string hotelCode)
{
var db = _hoteldbObj.Instance();
var primaryImageBucket = new MongoGridFS(db, new MongoGridFSSettings() {Root = "HotelPrimaryImage"});
foreach (string httpFile in Request.Files)
{
var postedFile = Request.Files[httpFile];
if(postedFile == null)
throw new InvalidOperationException("Invalid file");
var bytes = ReadToEnd(postedFile.InputStream);
using (var c = primaryImageBucket.Create(hotelCode, new MongoGridFSCreateOptions() { ContentType = postedFile.ContentType }))
{
c.Write(bytes, 0, bytes.Length);
c.Flush();
c.Close();
}
}
return new JsonResult();
}
Thank You
Performing a .find("ABC") where ABC is your filename will handle this if querying on the full file name.
If you want to query on a substring within the file name, my suggestion would be to save the substring as part of the metadata object. See this post for an example of working with metadata.