i have a problem of transfer JSON to java type, I want to transfer this JSON:
{ "kind": "fusiontables#sqlresponse", "columns": [ "Pre", "Post" ], "rows": [ [ "Si2", "Si2" ], [ "CeSP-A", "CeSP-A" ], [ "Si2", "Si1" ], [ "Si1", "Si2" ], [ "Si2", "Si1" ], [ "Si1", "Si2" ] ]}
to a java list which each one of the list has two private string Pre and Post
Here is my code:
Gson gson = new Gson();
List<Edges> list = gson.fromJson(jsonU,
new TypeToken<List<Edges>>() {
}.getType());
for (int i = 0; i < list.size(); i++) {
Edges p = list.get(i);
System.out.println(p.getPre() + ", " + p.getPost());
System.out.println();
}
and
jsonU is the String of JSON file
{ "kind": "fusiontables#sqlresponse", "columns": [ "Pre", "Post" ], "rows": [ [ "Si2", "Si2" ], [ "CeSP-A", "CeSP-A" ], [ "Si2", "Si1" ], [ "Si1", "Si2" ], [ "Si2", "Si1" ], [ "Si1", "Si2" ] ]}
However, i got the error:
Exception in thread "main" com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2
can you please help me resolve this problem? Thank you very much
You are telling GSON to deserialize a List<Edges> yet the JSON you are passing starts with an object, with the { and } tokens.
GSON, based on the type parameters you've passed to it, is expecting something like:
[
{ "kind": .... },
{ "kind: .... },
...
]
In other words, how can GSON construct a List of something when you are passing it JSON corresponding to a single object?
Related
I am trying to send SQL queries to elasticsearch server using the SQL API ( I am using the free version so I can't use the JDBC driver they provide)
and I was wondering how I can map the result of the query since it comes back like this:
{
"columns": [
{
"name": "project_id",
"type": "text"
}
],
"rows": [
[
"item1"
],
[
"item2"
],
[
"item3"
],
[
"item4"
],
[
"item5"
],
[
"item6"
]
]
}'''
Below is the JSON response, I have used JSONPath get() to retrieve value of totalContractAmout by using below path - subscriptionQuoteResponseDetails.customerQuoteDetails[0].billPlanQuoteDetails[0].serviceList[0].skuList[0].totalContractAmount
But this seems to be hard coded, is there any way I can make it generic using JAVA language.
{
"transactionId": "Transaction123",
"systemId": "AAA",
"userId": "User123",
"resultDate": "2019-11-23T12:52:16.400-06:00",
"resultCode": "100",
"resultMessage": "SUCCESS",
"subscriptionQuoteResponseDetails": {
"quoteDetailsStatusCode": 2,
"customerQuoteDetails": [
{
"customerId": "546789",
"buid": "111",
"billPlanQuoteDetails": [
{
"serviceList": [
{
"serviceType": "/service/",
"skuList": [
{
"skuId": "932125",
"productName": "DummyName",
"quantity": 4,
"totalContractAmount": 1728,
"rateCards": [
{
"productCadence": "M",
"cadenceAmount": 48
},
{
"productCadence": "F",
"cadenceAmount": 1728
}
]
}
]
}
]
}
]
}
]
}
}
JSON string:
{
"vcardArray": [
"vcard",
[
[
"version",
{},
"text",
"4.0"
],
[
"fn",
{},
"text",
"Focus IP, dba AppDetex"
]
]
]
}
You have to use Arraylist<Arraylist<Object>> since you have different types.
I have the following JSON input data:
{
"lib": [
{
"id": "a1",
"type": "push",
"icons": [
{
"iId": "111"
}
],
"id": "a2",
"type": "pull",
"icons": [
{
"iId": "111"
},
{
"iId": "222"
}
]
}
]
I want to get the following Dataset:
id type iId
a1 push 111
a2 pull 111
a2 pull 222
How can I do it?
This is my current code. I use Spark 2.3 and Java 1.8:
ds = spark
.read()
.option("multiLine", true).option("mode", "PERMISSIVE")
.json(jsonFilePath);
ds = ds
.select(org.apache.spark.sql.functions.explode(ds.col("lib.icons")).as("icons"));
However the result is wrong:
+---------------+
| icons|
+---------------+
| [[111]]|
|[[111], [222...|
+---------------+
How can I get the correct Dataset?
UPDATE:
I tries this code, but it generates some extra combinations of id, type and iId that do not exist in the input file.
ds = ds
.withColumn("icons", org.apache.spark.sql.functions.explode(ds.col("lib.icons")))
.withColumn("id", org.apache.spark.sql.functions.explode(ds.col("lib.id")))
.withColumn("type", org.apache.spark.sql.functions.explode(ds.col("lib.type")));
ds = ds.withColumn("its", org.apache.spark.sql.functions.explode(ds.col("icons")));
As already pointed out, the JSON String seems to be malformed. with the updated one, you can use the following to get result you wanted:
import org.apache.spark.sql.functions._
spark.read
.format("json")
.load("in/test.json")
.select(explode($"lib").alias("result"))
.select($"result.id", $"result.type", explode($"result.icons").alias("iId"))
.select($"id", $"type", $"iId.iId")
.show
Your JSON appears to be malformed. Fixing the indenting makes this slightly more apparent:
{
"lib": [
{
"id": "a1",
"type": "push",
"icons": [
{
"iId": "111"
}
],
"id": "a2",
"type": "pull",
"icons": [
{
"iId": "111"
},
{
"iId": "222"
}
]
}
]
Does your code work correctly if you feed it this JSON instead?
{
"lib": [
{
"id": "a1",
"type": "push",
"icons": [
{
"iId": "111"
}
]
},
{
"id": "a2",
"type": "pull",
"icons": [
{
"iId": "111"
},
{
"iId": "222"
}
]
}
]
}
Note the inserted }, { just before "id": "a2" to break the object with duplicate keys into two, and the closing } at the very end which had previously been omitted.
I have this Json and i can´t recover the field "entidad" and "oficina":
{
"resultado": [
{
"columa": [
"p"
],
"datos": [
{
"row": [
{
"oficina": "0000",
"entidad": "1234",
"nombre": "nombre persona"
}
],
"meta": [
{
"id": 4700925,
"type": "node",
"deleted": false
}
]
}
]
}
],
"errors": [],
"responseTime": 84
}
How can I recover the field "oficina" and "entidad"?
I could use Gson or Jackson.
I can´t recover this fields.
thank you
Get your array 'row'. When you got it, you can iterate it and extract the elements:
Get resultado --> get datos --> get row, then:
for(int i=0; i<arrayJSON.length; i++) {
JSONObject objectJSON= arrayJSON.get(i);
String entidad = objectJSON.getString("entidad");
String oficina = objectJSON.getString("oficina");
}