diff --git a/JUST.net/JsonTransformer.cs b/JUST.net/JsonTransformer.cs index e5e3265..71df3b2 100644 --- a/JUST.net/JsonTransformer.cs +++ b/JUST.net/JsonTransformer.cs @@ -141,8 +141,9 @@ private void RecursiveEvaluate(ref JToken parentToken, State state) bool isBulk = false; bool isScope = false; - foreach (JToken childToken in tokens) + for (int i = 0; i < tokens.Count(); i++) { + var childToken = tokens.ElementAt(i); ParseToken(ref parentToken, state, ref selectedTokens, ref tokensToReplace, ref tokensToDelete, ref loopProperties, ref condProps, ref arrayToForm, ref dictToForm, ref scopeProperties, ref scopeToForm, ref tokenToForm, ref tokensToAdd, ref isLoop, ref isBulk, ref isScope, childToken); } @@ -267,7 +268,21 @@ private void PostOperationsBuildUp(ref JToken parentToken, List tokenToF } else if (token is JArray arr && parentToken.Parent != null) { - (parentToken.Parent as JProperty).Value = arr; + switch (parentToken.Parent.Type) + { + case JTokenType.Array: + parentToken.Replace(arr); + break; + case JTokenType.Property: + (parentToken.Parent as JProperty).Value = arr; + break; + default: + if (Context.IsStrictMode()) + { + throw new Exception($"don't know what to do with {token} and {parentToken.Type} parent!"); + } + break; + } } else { diff --git a/UnitTests/ConditionalFunctionsTests.cs b/UnitTests/ConditionalFunctionsTests.cs index 7898c5a..af3ea95 100644 --- a/UnitTests/ConditionalFunctionsTests.cs +++ b/UnitTests/ConditionalFunctionsTests.cs @@ -256,5 +256,16 @@ public void ConstantEmptyArray() Assert.AreEqual("{\"result\":[]}", result); } + + [Test] + public void ConditionalGroupOnArrayItemWithLoopInside() + { + const string input = "{ \"additional_content\": [{ \"Cards_feed\": { \"cards_feed\": [ \"2\"] } }] }"; + const string transformer = "{ \"content\": [ { \"aaa\": \"bbb\" }, { \"#ifgroup(#exists($.additional_content[*].Cards_feed))\": { \"#loop($.additional_content[*].Cards_feed.cards_feed)\": { \"xxx\": \"yyy\" } } } ] }"; + + var result = new JsonTransformer(new JUSTContext { EvaluationMode = EvaluationMode.Strict }).Transform(transformer, input); + + Assert.AreEqual("{\"content\":[{\"aaa\":\"bbb\"},[{\"xxx\":\"yyy\"}]]}", result); + } } }