PythonJavaRepository
Code to achieve json normalization in Java via python
What this code does?
- This is a sample code to perform json normalization in java with the help of python.
Why I have written this code?
- We had a requirement where input json needs to be normalized inorder to be written to db.
- No utility for this requirement is available in Java out of box and we end up writting very messy custom code.
- Found that this utility is readily availble in python via pandas module.So no need of 're-inventing the wheel' again. Hence written a java wrapper over python to achieve it.
What is 'normalization' by the way and what do pandas do?
-
Well please go through following link for a clear explanation:
http://pandas.pydata.org/pandas-docs/version/0.17.0/generated/pandas.io.json.json_normalize.html
Sound coool, well how to run this?
-
Please find steps below to run the same.
-
Please refer http://pandas.pydata.org/pandas-docs/version/0.17.0/generated/pandas.io.json.json_normalize.html to model your input json data.
-
I have used the same example explanied in the above link to run the code.
-
First and formost, we you are a java developer, its time to show some love to python2. Please install python2 and pandas module to run this program.
-
Following are the arguments and orders to be passed to this java program : arg[0] = path to python executable arg[1] = path to python script file arg[2] = input json file arg[3] = record path arg[4] = meta
Last three arguments needs to be formatted a bit, inorder to make it ready for python to execute it as command line arguments. For example :
In order to run this program please give the following String as arguments to this java program:
"--path to python executable OR env_variable name--"
"--path to python file to be executed--"
""[{""state"":""Florida"",""shortname"":""FL"",""info"":{""governor"":""RickScott""},""counties"":[{""name"":""Dade"",""population"":12345},{""name"":""Broward"",""population"":40000},{""name"":""PalmBeach"",""population"":60000}]},{""state"":""Ohio"",""shortname"":""OH"",""info"":{""governor"":""JohnKasich""},""counties"":[{""name"":""Summit"",""population"":1234},{""name"":""Cuyahoga"",""population"":1337}]}]""
""counties""
""[""state"", ""shortname"",[""info"", ""governor""]]""
Where is the python script by the way?
- You may find a simple py script file named "normalization.py" in source code.
So whats next?
- Please check this if it fits into your requirements and let me know for issues or bugs.
Happy coding and have a nice day.

Formed in 2009, the Archive Team (not to be confused with the archive.org Archive-It Team) is a rogue archivist collective dedicated to saving copies of rapidly dying or deleted websites for the sake of history and digital heritage. The group is 100% composed of volunteers and interested parties, and has expanded into a large amount of related projects for saving online and digital history.
