Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
Former Member

Intro


In this guide we will see how to parse through JSON using Java so that the data can be loaded into Lumira via its Data Access Extensions. If you are only interested in getting all of the JSON data into Lumira without excluding or manipulating the data, then this guide may not be what you need. You could just use a more generic parser that converts all of the JSON into a CSV file that you can then use in Lumira. Thus saving you time and effort. However if you are interested in extracting some parts of the JSON, manipulating the data, using the data to create more data, or getting JSON into its Java counterpart then this guide will certainly be of use.

What you’ll need:


  1. Eclipse
  2. GSON – a JSON parsing library
  3. Jsmooth – free software to wrap .jar as .exe (or use your own method to run a jar as an exe)
  4. Lumira software and license or trial version
  5. Basic Understanding of Data Access Extensions (DAE)

Resources

                        

Trevor Dubinsky’s personal guide to SAP Lumira DAE

GSON User Guide

Download GSON 2.2.4

Download Jsmooth

Process


First you will need to download and import the GSON library into your eclipse project. In your main method you will need to reach your JSON data service URL and store the JSON as a String.  Instantiate the GSON Object by calling

Gson gson = new Gson();

You will also need to instantiate the JSON Parser

JsonParser jsonParser = new JsonParser();

That’s it for main for now. To convert all of the JSON data into Java variables you will need to create a class for each object in the JSON you want. Remember in JSON curly brackets are the start and end of an object { }.  Inside these classes you should create the class attributes or variables for all of the JSON data that you want to keep as you parse through. Be sure to match data types, if the JSON value is a String then it should be so in the class, if a number it should be a double or int. If you come across square brackets [ ] that is an array. Be sure to match what type of elements the array will be holding. It could be objects, or mixed data types. So you will need an array of that specific Object or a Java Collection for the mixed data types. If you come across more { } objects, and you want that data, you will need to create a class for it and repeat the above process. When finished you should have a hierarchy of objects, basically objects containing objects, possibly arrays, and all the other variable types depending on how deep and complex your JSON data was. Note if your JSON starts with an unnamed curly bracket { you can create a wrapper class to hold everything inside. Likewise if your JSON starts with a square bracket [ then it is an array and you just need to start with a class variable that is an array of the type for whatever elements it is holding.

To finish up your classes you will need to create the constructors. This part is important because when you call the GSON parser it is going to call the constructor for any object it finds in the JSON and attempt to pass in all of the values. Note the class attribute names should match that of the JSON, if you don’t want them to or they can’t due to spacing or special characters then you will need to map them using @SerializedName(“JSON Name”) in front of the class attribute. Here is your chance to either catch and store the value, ignore it, manipulate it, or create more data from it.  For example if you simply don’t want one of the values then don’t put its name in the constructor or class attributes. But if you do want to copy the value then be sure it is passed into the constructor and from inside the constructor you do this.value = value. And you have successfully stored it. Another cool example is manipulating the data. For example if you only want the first name of someone from a full name key. You could simply parse the string up to the space to get the first name and then store it as an attribute. Date formatting is also useful here. Last example, since you now have all the data you want in Java you can now create new data values. Like counting how many objects there were, or even just averaging all of the numbers you parsed.

Now back to your main method you will need a JsonObject or JsonArray. This depends on whether your JSON starts with a { or [.  Create it using

JsonObject jsonObject = jsonParser.parse(JSON_string_var).getAsJsonObject();

Or

JsonArray jsonArray = jsonParser.pars(JSON_string_var).getAsJsonArray();

And now create the parser

Parser your_wrapper_var = gson.fromJson(jsonObject, YourClass.class);

Or

Parser your_array_var = gson.fromJson(jsonArray, YourClass[ ].class);

You can test the parser by outputting to console in csv format now, and you will have to for Lumira to be able to read in the csv data.  Depending on your data this can be simple or quite complex. But essentially all you need to do is loop through all of your objects in the object hierarchy and print the values. Be sure to separate them with commas, or the character you specified to separate them with. Since you could have nested arrays, and it is uncertain how many elements each array contains it will be crucial to use array.length when looping through. Also when outputting the data to csv the inner array elements may need to be related to the outer elements they belong too. So saving the ID of the array/collection you are inside of can be useful for outputting it to console along with the data of the inside array you are looping through.

Once you are satisfied with the output you can now export your project as a jar and use Jsmooth to wrap it as a .exe. Be sure to put both your jar and exe files into the Lumira Data Extensions folder you specified. Fire up Lumira and start a new project. Select from external data source and you should see your parser .exe available. Select it and Lumira will show you a data preview and you can proceed to make your charts and storyboards.

Thanks for reading my guide. If anything needs clarification please ask in the comments.

Labels in this area