Importing data from JSON file to MySQL database using java [closed] - java

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I currently ran into a requirement of having the data dynamically imported into MySQL database (But by using java).
I am not sure what is the best practice for this but let me put forward my scenario.
The json can be dynamic and its uncertain of what will be in the json each time I read from an API endpoint
for example:
"table1" : {
"field1" : "value1",
"field2" : "value2",
"field3" : "value3"
}
One time the json will be like this
the other time it might be like
"table2" : {
"field1" : "value1",
"field2" : "value2",
"field3" : "value3",
"field4" : "value4"
}
so my question is should I parse the json and compare it with the fields in table which matches the json name?
or is there any library available in java which will enable me to easily and efficiently handle this.
NOTE: I have to prepare this as a service which will continously scan a given API after certain interval and the respective JSON needs to get imported into the table it belongs, and if the table doesn't exist then it must get automatically created.
Thanks all for your patient reading and support, this is a new kind of requirement which I have never handled.
Thank you in Advance!

You can use google-gson or jackson api to make your code more efficient

In my project i used google-gson API. It worked very well and it was painless.
You can find the project with it's description here.
Below is a simple example from API docs wich supports multi-dimensional arrays, with arbitrarily complex element types:
Collections Examples
Gson gson = new Gson();
Collection<Integer> ints = Lists.immutableList(1,2,3,4,5);
// Serialization
String json = gson.toJson(ints); // ==> json is [1,2,3,4,5]
// Deserialization
Type collectionType = new TypeToken<Collection<Integer>>(){}.getType();
Collection<Integer> ints2 = gson.fromJson(json, collectionType);
// ==> ints2 is same as ints
Hope this helps.

Related

Can't pass extracted string into body object in JMeter [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 months ago.
Improve this question
I am trying to pass a string extracted from an API response in a subsequent call in JMeter. I am able to extract the object I want ("thing": "THING"), store it as variable $thisThing and then pass it, but not as a string.
Using this as my body data:
{
"foo": "bar",
"thing": ${thisThing}
}
...results in this request body:
{
"foo": "bar",
"thing": THING,
}
And the API errors out, unexpected token. Looking into post-processing solutions but I can't dig up anything of relevance.
As per JSON Object Literals:
Keys must be strings, and values must be a valid JSON data type:
string
number
object
array
boolean
null
so if this THING supposed to be a JSON String - you need to surround it with quotation marks:
{
"foo": "bar",
"thing": "${thisThing}"
}
or amend your Post-Processor configuration to extract the THING value along with surrounding quotation marks.
You might also need to add a HTTP Header Manager and configure it to send Content-Type header with the value of application/json

How to read the Json file for specific value in Java using Simple.json library [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
This what my Json file looks like.
[
[
"Name"
"Age"
"Height"
]
[
"Name"
"Age"
"Height"
]
]
How can I read the specific value like "Name" using simple.json in Java?
This is invalid json format. But anyway to read json data from a file you can open it and read it to string as shown in here
Then using org.json library you can parse string to either json array or object.
String fileDataJsonObject = "{"Name":"SomeName"}"; //string containing a json object
String fileDataJsonArray = "[{"Name":"SomeName"},{"Name":"SomeName2"}]"; //string containing a json array
JSONObject obj = new JSONObject(fileDataJsonObject);
JSONArray array = new JSONArray(fileDataJsonArray);
This is just an example of how you can parse string to json array or an object. For arrays you can then use getJSONObject(index) method to get json objects from array. Also you can use json object getters to get any element within that object.
p.s I just found this useful discussion on stackoverflow. The answers have different ways of parsing json object or array using different libraries.

How to interpret cryptic Java class documentation [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have a Stack Overflow question where I'm attempting to use Jython to extract a field value from JSON text:
Jython: Parse JSON object to get value (using Java functions)
A Stack Overflow community member has been kind enough to point me towards some Java documentation:
IBM >> Maximo >> Class JSONObject (Java)
Unfortunately, I've been staring at the Java documentation page for hours now, and to be honest, I have absolutely no idea what I'm looking at.
Where does this documentation show me how to extract a value from JSON text?
In other words, how do interpret this cryptic Java class documentation?
Start here, by passing the JSON string into the parse function.
Then, once you have your JSONObject, you can traverse the tree treating the object as a HashMap.
String jsonInput = "{ 'foo':'bar' }";
JSONObject jsonObject = JSONObject.parse(jsonInput);
String fooValue = jsonObject.get('foo');
Of course, this is the hard way. You might consider a more fluid library like JsonPath, which also has documentation that's more fluid.

JSON file proper Case and word spacing [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
I am wondering what will be the best approach for word case and word delimiter in JSON
considering that later on the JSON file will be converted to a Java Pojo and i would like this Java Pojo to be in ProperCase
I am debating between:
"sectionSuspensionTiresSteering": [{
"SectionSuspensionTiresSteering": [{
"section_suspension_tires_steering": [{
"section Suspension Tires Steering": [{
I don't really think that JSON has a naming convention, so you can "choose" sort of speak. As I have a Java background, I prefer using camelCase (your first option). I would avoid using blank spaces in the JSON keys, it is allowed but causes problems because most of the existing framework aren't able to deal with it.
So you are free to use the style you want. Regarding the conversion back to Java Pojo, this is just a matter of annotations. When you use for example Jackson, it allows you to annotate your fields in order to convert the JSON file back to a Java Pojo.
Use always the first one:
"sectionSuspensionTiresSteering": [{
Examples of similar files in official guides:
https://spring.io/guides/gs/authenticating-ldap/
As you can see here, the gradle file uses this notation and XMLs files too.
I understand that JSON properties are written in camel case (sectionSuspensionTiresSteering) style.
But it has nothing to do with how the property will be written or coded in java.
For example if you use Gson() to do the conversions between java an JSON you can name the java property whatever you want and annotate the property with #SerializedName("jsonName") passing in the JSON property name.
What I mean is that the two names are not coupled.

Array of arrays with different data types [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am working on a map based web application.I am not able to create the location data which is a array containing the arrays in the below format in plain java code.(array containing arrays ).The inner array should be having first and second element as double and the third as String.
var LocationData = [
[12.3, 77.1, "ABDC" ],
[12.4, 77.3, "PQRS" ],
[12.6, 77.4, "XYZA" ]
];
It is like we need to populate the map markers in the maps.The javascript data above is the location specific data.Somehow I need to pass the data in the above format from the spring controller.Advise needed if this can be send it as JSON string or if the data in the above format can be obtained in plain java code
Regards.
You should create a POJO for this as its a more OO approach. Here's a very simple snippet:
class MyData {
private double first;
private double second;
private String str;
//constructors using fields, getter, setter, equals, hashCode, etc.
}
Then just create an array of MyData objects.
MyData[] array = new MyData[3];
array[0] = new MyData(12.3, 77.1, "ABDC");
...
Above answer with OO approach is the best way. For some reason if you can't use that, you can use the array of objects.
Object obj1[] = {1,1.2,"test"};
Object obj2[] = {1,1.2,"test"};
Object obj3[] = {obj1,obj2};

Categories