Pretty print JSON in LotusScript



'Add this LS function to your code

Private Function FormatJSON(json As String) As string

	Dim ret As String
	Dim jSession As New JavaSession
	Dim myClass As JavaClass
	Dim myObject As JavaObject
	Set myClass = jSession.GetClass("PrettyPrint")
	Set myObject = myClass.CreateObject
	ret = myObject.formatJson(json)
	FormatJSON = ret

End Function


'Add this java class to a java scriptlibrary and name it "JavaMethods" it uses gson which is a package that is already available in nsf so no need to add any extra library

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;

public class PrettyPrint {

	public String formatJson(String jsonString) {
	    JsonParser parser = new JsonParser();
	    JsonElement jsonElement = parser.parse(jsonString);
	    Gson gson = new GsonBuilder().setPrettyPrinting().create();
	    return gson.toJson(jsonElement);
	}
	
}

'Add these import statements to your LS code, 

Use "JavaMethods"
UseLSX "*javacon"


'Make the call

Dim prettyJson  As String
prettyJson = FormatJSON(your_json_unformatted_string)








All code submitted to OpenNTF XSnippets, whether submitted as a "Snippet" or in the body of a Comment, is provided under the Apache License Version 2.0. See Terms of Use for full details.
No comments yetLogin first to comment...