Search code examples
c#azure-form-recognizer

How can I get the JSON response from a FormRecognizer document in C#?


I'm using FormRecognizer to analyse a document to get the values back like surname: SURNAME. But with my code I only get the string result without the recognized boxes like above with "surname:" SURNAME.

What I need is the raw json output like you see at the FormRecognizer Builder Website.

Code below:

    {
        Debug.Log("using");
        using (var stream = new MemoryStream())
        {
            List<string> list = new List<string>();
            var datei = File.OpenRead(filePath);
            datei.CopyTo(stream);
            stream.Seek(0, SeekOrigin.Begin);
            AnalyzeDocumentOperation operation = client.StartAnalyzeDocument(modelID, stream);
            operation.WaitForCompletion();
            var result = operation.Value;
            Debug.Log(result);
            string buchstaben = string.Empty;
            foreach(var document in result.Content)
            {
                buchstaben += document;
                
            }
            text.text = buchstaben;

        }
    } ```

Solution

  • It's not clear if you want to use the SDK to retrieve semantic document fields or raw JSON text, so I'll share a sample for both.

    Note that result.Content is a string containing the full text of the input document, so your loop is iterating over the char's of the document, not the recognized documents or their fields.

    Access document fields

    This is probably what you want if you are using the SDK to process documents.

    // client is DocumentAnalysisClient
    var operation = await client.StartAnalyzeDocumentAsync(modelId, input);
    var result = await operation.WaitForCompletionAsync();
    Console.WriteLine("Fields:");
    foreach (var document in result.Value.Documents)
    {
        Console.WriteLine(document.DocType);
        foreach (var (fieldName, fieldValue) in document.Fields)
        {
            // Use fieldValue.AsXyz() to access the strongly-typed value (e.g. AsString(), AsAddress(), etc.).
            Console.WriteLine($"  - {fieldName}: {fieldValue}");
        }
    }
    

    Access raw JSON string

    You probably only need this if you are storing results for downstream processing.

    // client is DocumentAnalysisClient
    var operation = await client.StartAnalyzeDocumentAsync(modelId, input);
    var result = await operation.WaitForCompletionAsync();
    var json = result.GetRawResponse().Content.ToString();
    Console.WriteLine(json);