Azure / Azure/azure-functions-host

JSONLine format output cause Newtonsoft.Json.JsonReaderException

Open
#9,587 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
C#
Stars
2k
Forks
482
Avg merge
2d 12h
Merged PRs (30d)
38

Description

Investigative information
This issue is reproducible locally.

Timestamp: N/A
Function App version: 4.21.1.20667
Function App name: N/A
Function name(s) (as appropriate): N/A
Invocation ID: N/A
Region: N/A

#### Repro steps

Create Queue output binding function with JavaScript, then set [jsonlines](https://jsonlines.org/) style output to binding.

```javascript
module.exports = async function (context, req) {
context.log('JavaScript HTTP trigger function processed a request.');

const s =`{"key1": "value1a", "key2": "value2a"}
{"key1": "value1b", "key2": "value2b"}
{"key1": "value1c", "key2": "value2c"}`

context.bindings.outItem = s;
context.res = {
// status: 200, /* Defaults to 200 */
body: s
};
}
```

```
[2023-10-04T09:57:04.525Z] Executing 'Functions.HttpTrigger1' (Reason='This function was programmatically called via the host APIs.', Id=986db933-fa06-4d6d-968a-c805af6f71ef)
[2023-10-04T09:57:04.646Z] JavaScript HTTP trigger function processed a request.
[2023-10-04T09:57:04.848Z] Executed 'Functions.HttpTrigger1' (Failed, Id=986db933-fa06-4d6d-968a-c805af6f71ef, Duration=360ms)
[2023-10-04T09:57:04.848Z] System.Private.CoreLib: Exception while executing function: Functions.HttpTrigger1. Newtonsoft.Json: Additional text encountered after finished reading JSON content: {. Path '', line 2, position 0.
```

#### Expected behavior

JSONLines string correctly saved to queue storage

#### Actual behavior

Function failed.

```
fail: Function.HttpTrigger1[3]
Executed 'Functions.HttpTrigger1' (Failed, Id=0cd6ffd9-529a-4929-8c2f-9df2c1d1732f, Duration=119425ms)
Microsoft.Azure.WebJobs.Host.FunctionInvocationException: Exception while executing function: Functions.HttpTrigger1
---> Newtonsoft.Json.JsonReaderException: Additional text encountered after finished reading JSON content: {. Path '', line 2, position 0.
at Newtonsoft.Json.JsonTextReader.Read()
at Newtonsoft.Json.Linq.JToken.Parse(String json, JsonLoadSettings settings)
at Microsoft.Azure.WebJobs.Script.Binding.FunctionBinding.ReadAsEnumerable(Object value) in /home/toshida/Projects/LearningDay/FunctionsHostRunLocal/azure-functions-host/src/WebJobs.Script/Binding/FunctionBinding.cs:line 119
```

#### Known workarounds

Provide a description of any known workarounds.
N/A

#### Related information

Provide any related information

This exception is thrown from below code.
https://github.com/Azure/azure-functions-host/blob/dev/src/WebJobs.Script/Binding/FunctionBinding.cs#L114-L119

[`Utility.IsJson`](https://github.com/Azure/azure-functions-host/blob/dev/src/WebJobs.Script/Utility.cs#L419C28-L419C34) returns true for JSONLines, but `JToken.Parse` does not accept JSONLines.

There is similar issue about JSON handling for output binding
https://github.com/Azure/azure-functions-host/issues/9581
```[tasklist]
### Tasks
```

Contributor guide

Open the contributing guide

Research direction

Start with src/WebJobs.Script/Binding/FunctionBinding.cs around lines 114-119 and inspect Utility.IsJson in src/WebJobs.Script/Utility.cs around line 419. Reproduce the JavaScript queue output binding with the JSONLines string, then verify that the output is accepted and saved to queue storage without the Newtonsoft.Json.JsonReaderException.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.