Created json formatter and validator. (#82)
Co-authored-by: mikolaj widla <mikolaj.widla@gmail.com> Co-authored-by: Adam Bem <adam.bem@zoho.eu> Co-authored-by: Adam Bem <bema@noreply.example.com> Co-authored-by: Artur Kołecki <koleckiartur@icloud.com> Reviewed-on: R11/release11-tools-web#82
This commit is contained in:
		
							
								
								
									
										29
									
								
								Frontend/assets/scripts/tools/json.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										29
									
								
								Frontend/assets/scripts/tools/json.js
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,29 @@
 | 
			
		||||
function formatAndValidateJson(errorElement) {
 | 
			
		||||
  const input = document.querySelector('#jsonBlock');
 | 
			
		||||
  const errorOutput = document.getElementById(errorElement);
 | 
			
		||||
 | 
			
		||||
  try {
 | 
			
		||||
    const obj = JSON.parse(input.textContent);
 | 
			
		||||
    input.innerHTML = JSON.stringify(obj, null, 2);
 | 
			
		||||
    errorOutput.innerText = "";
 | 
			
		||||
    hljs.highlightElement(input);
 | 
			
		||||
  } catch (error) {
 | 
			
		||||
    errorOutput.innerText = error;
 | 
			
		||||
    console.error("Error: ", error)
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function minimizeJson(errorElement) {
 | 
			
		||||
  const input = document.querySelector('#jsonBlock');
 | 
			
		||||
  const errorOutput = document.getElementById(errorElement);
 | 
			
		||||
 | 
			
		||||
  try {
 | 
			
		||||
    const obj = JSON.parse(input.textContent);
 | 
			
		||||
    input.innerHTML = JSON.stringify(obj);
 | 
			
		||||
    errorOutput.innerText = "";
 | 
			
		||||
    hljs.highlightElement(input);
 | 
			
		||||
  } catch (error) {
 | 
			
		||||
    errorOutput.innerText = error;
 | 
			
		||||
    console.error("Error: ", error)
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user