Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/jmespath.net/Functions/MergeFunction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,15 @@ public override void Validate(params JmesPathFunctionArgument[] args)
public override JToken Execute(params JmesPathFunctionArgument[] args)
{
var result = new JObject();
var jsonMergeSettings = new JsonMergeSettings
{
MergeNullValueHandling = MergeNullValueHandling.Merge
};

foreach (var argument in args)
{
var token = (JObject)argument.Token;
result.Merge(token);
result.Merge(token, jsonMergeSettings);
}

return result;
Expand Down
48 changes: 48 additions & 0 deletions tests/jmespathnet.tests/Functions/MergeFunctionTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using jmespath.net.tests.Expressions;
using jmespath.net.tests.Parser;
using Newtonsoft.Json.Linq;

namespace jmespath.net.tests.Functions
{
using FactAttribute = Xunit.FactAttribute;

public class MergeFunctionTest : ParserTestBase
{
[Fact]
public void MergeOverwriteValue()
{
const string json = "{\"bar\":2}";
const string expression = "merge(@, { bar: `3` } )";
const string expected = "{\"bar\":3}";

Assert(expression, json, expected);
}

[Fact]
public void MergeNewValue() {
const string json = "{\"bar\":2}";
const string expression = "merge(@, { foo: `3` } )";
const string expected = "{\"bar\":2,\"foo\":3}";

Assert(expression, json, expected);
}

[Fact]
public void MergeNewNullValue() {
const string json = "{\"bar\":2}";
const string expression = "merge(@, { foo: `null` } )";
const string expected = "{\"bar\":2,\"foo\":null}";

Assert(expression, json, expected);
}

[Fact]
public void MergeOverwriteWithNull() {
const string json = "{\"bar\":2}";
const string expression = "merge(@, { bar: `null` } )";
const string expected = "{\"bar\":null}";

Assert(expression, json, expected);
}
}
}