@@ -87,7 +87,10 @@ defmodule JsonRemedy.Layer1.ContentCleaning do
8787 # Then try to extract from prose/text
8888 { result , prose_repairs } = extract_from_prose ( result )
8989
90- all_repairs = existing_repairs ++ html_repairs ++ prose_repairs
90+ # Finally, remove any trailing wrapper text after JSON
91+ { result , trailing_repairs } = remove_trailing_wrapper_text ( result )
92+
93+ all_repairs = existing_repairs ++ html_repairs ++ prose_repairs ++ trailing_repairs
9194 { result , all_repairs }
9295 end
9396
@@ -682,6 +685,66 @@ defmodule JsonRemedy.Layer1.ContentCleaning do
682685 find_balanced_end ( rest , open , close , pos + 1 , balance , in_string )
683686 end
684687
688+ # Remove trailing wrapper text after JSON
689+ defp remove_trailing_wrapper_text ( input ) do
690+ trimmed = String . trim ( input )
691+
692+ # Check if input starts with JSON structure
693+ cond do
694+ String . starts_with? ( trimmed , "{" ) ->
695+ check_and_remove_trailing_text ( input , "{" , "}" )
696+
697+ String . starts_with? ( trimmed , "[" ) ->
698+ check_and_remove_trailing_text ( input , "[" , "]" )
699+
700+ true ->
701+ { input , [ ] }
702+ end
703+ end
704+
705+ defp check_and_remove_trailing_text ( input , open_char , close_char ) do
706+ # Find where the JSON structure starts
707+ json_start =
708+ case String . split ( input , open_char , parts: 2 ) do
709+ [ prefix , _ ] -> String . length ( prefix )
710+ _ -> 0
711+ end
712+
713+ # Extract from the JSON start to find the balanced end
714+ substring_from_json = String . slice ( input , json_start , String . length ( input ) )
715+
716+ case find_balanced_end ( substring_from_json , open_char , close_char ) do
717+ nil ->
718+ # Could not find balanced end, return as is
719+ { input , [ ] }
720+
721+ end_pos ->
722+ # Calculate the absolute position where JSON ends
723+ json_end = json_start + end_pos + 1
724+
725+ # Check if there's non-whitespace content after JSON ends
726+ after_json = String . slice ( input , json_end , String . length ( input ) )
727+
728+ if String . trim ( after_json ) == "" do
729+ # No significant trailing content
730+ { input , [ ] }
731+ else
732+ # Extract only the JSON portion
733+ json_content = String . slice ( input , 0 , json_end )
734+
735+ repair = % {
736+ layer: :content_cleaning ,
737+ action: "removed trailing wrapper text" ,
738+ position: json_end ,
739+ original: input ,
740+ replacement: json_content
741+ }
742+
743+ { json_content , [ repair ] }
744+ end
745+ end
746+ end
747+
685748 # Helper functions for string detection using direct methods
686749
687750 # Fast check for long text that likely contains JSON content
0 commit comments