I've a csv file something similar to this
"name.firstName","name.givenName","name.DisplayName","phone.type","phone.value"
"john","maverick","John Maverick","mobile","123-123-123"
"jim","lasher","Jim Lasher","mobile","123-123-123"
I want to convert the 2nd and 3rd row into JSON objects.Using the first row as Header. So the result will be
[
{
"name": {
"firstName": "john",
"givenName": "maverick",
"DisplayName": "John Maverick"
},
"phone": {
"type": "mobile",
"value": "123-123-123"
}
},
{
"name": {
"firstName": "john",
"givenName": "maverick",
"DisplayName": "John Maverick"
},
"phone": {
"type": "mobile",
"value": "123-123-123"
}
]
Any idea how to achieve this?
Here is a Java library that may help you. http://www.jonathanhfisher.co.uk/playpen/csv2json/index.htm
Here is a JavaScript library that may or may not be useful to you. http://www.cparker15.com/code/utilities/csv-to-json/
And finally, here is a past answer that may be useful. I like the OpenCSV solution. However, instead of JAXB, you could use Jackson. Converting an CSV file to a JSON object in Java
Related
I'm trying to create SQL Tables from a Json File which is written following the OpenApi Specification. Here is an example of an Input file I must convert:
"definitions": {
"Order": {
"type": "object",
"properties": {
"id": {
"type": "integer",
"format": "int64"
},
"petId": {
"type": "integer",
"format": "int64"
},
"quantity": {
"type": "integer",
"format": "int32"
},
"shipDate": {
"type": "string",
"format": "date-time"
},
"status": {
"type": "string",
"description": "Order Status",
"enum": [
"placed",
"approved",
"delivered"
]
},
"complete": {
"type": "boolean",
"default": false
}
},
"xml": {
"name": "Order"
}
},
"Category": {
"type": "object",
"properties": {
"id": {
"type": "integer",
"format": "int64"
},
"name": {
"type": "string"
}
},
"xml": {
"name": "Category"
}
},
My aim to to create two tables named "Order" and "Category" whose columns must be to ones listed in the "properties" field. I'm using Java.
The Input file is mutable, so I used Gson to read it. I managed to get an Output like this:
CREATE TABLE ORDER
COLUMNS:
id->
type: integer
format: int64
petId->
type: integer
format: int64
quantity->
type: integer
format: int32
shipDate->
type: string
format: date-time
status->
type: string
description: Order Status
Possibilities:
-placed
-approved
-delivered
complete->
type: boolean
default: false
CREATE TABLE CATEGORY
COLUMNS:
id->
type: integer
format: int64
name->
type: string
I'm stuck here, trying to convert the "type" and "format" fields into a type that can be read by PostgreSQL or MySQL. Furthermore, it is hard to work directly on the code to get a readable SQL string due the presence of nesting. So I thought it might be a good idea to work on the output and "translate" it to SQL. Is there any class\package that could help me reading a file like this? I'm trying to avoid the use of thousands IF ELSE conditions. Thank you.
Your assignment involves two phases.
One is "Parsing" the given JSON object and understanding the content
Second one is "Translating" the parsed content into a working SQL query
Here your Java program should work as a kind of Translation engine.
For parsing the JSON objects many java libraries are available.
To translate the parsed json into a SQL query you can simply use basic String manipulation methods.
I want to get the value at the field first inside name.
How i can access in this field using HashMap in java
{ "payload":{
"name": {
"first": "jean",
"last": "bob,
},
"address": {
"code": "75",
"city": "paris",
"country": "France"
},
}}
Use one of the available Java libraries for handling JSON. E.g. Gson from Guava API. They are pretty straing fw.
"transaction": {
"id": 1,
"empid": "12345",
"details1": {
"name": "xyz",
"age": "30",
"sex": "M",
"Address": {
"Office": "office",
"Home": "Home"
}
},
"abcDetails": "asdf",
"mobile": 123455
},
I need to test if JSON record contains more then two keys(details, Address).
Then, I need to pass those key input to this line:
parserValue1 = parserValue.asObject().get("firstKey").asObject().get("secondKey");
Can anyone help me?
Many json parsers have a has("key") or contains("key") accessor.
Otherwise you will have to add a condition to check if get("") returns null, or turn your whole Json object into a map, where you do the same checks.
Since the json objects are limited in usage of characters I'm looking for a better description for this. The json file will be used later as a table and therefore the objects are the head and the values are the content of the table.
{
"employees": {
"employee": [
{
"id": "1",
"firstName": "Tom",
"lastName": "Cruise"
},
{
"id": "2",
"firstName": "Maria",
"lastName": "Sharapova"
},
{
"id": "3",
"firstName": "James",
"lastName": "Bond"
}
]
}
}
Therefore I'm looking to use something like a header where I can define the names. These names shall replace the object strings.
e.g.
{
"header": {
"id":"Identifikation",
"firstName":"First Name",
"lastName":"Last Name"
}
}
How can use such name convention replacement? Do json provide such 'heading' object?
Please help me to parse below JSON string Using XStream and JettisonMappedXmlDriver In Java ??
[{
"uuid": "{empid}",
"attributes": {
"name": "Prem",
"surname": "Nath",
"year": 1965
},
"relationships": {
"ONE_TO_MANY": {
"cars": "{object_name}/{empid}/cars"
}
}
}
]
Your JSON is not valid. The second to last line contains a , that doesn't belong there.