Kotlin Jackson generation objects from JSON - json

Please help! I'm trying to generate object from JSON with jackson kotlin module. Here is json source:
{
"name": "row",
"type": "layout",
"subviews": [{
"type": "horizontal",
"subviews": [{
"type": "image",
"icon": "ic_no_photo",
"styles": {
"view": {
"gravity": "center"
}
}
}, {
"type": "vertical",
"subviews": [{
"type": "text",
"fields": {
"text": "Some text 1"
}
}, {
"type": "text",
"fields": {
"text": "Some text 2"
}
}]
}, {
"type": "vertical",
"subviews": [{
"type": "text",
"fields": {
"text": "Some text 3"
}
}, {
"type": "text",
"fields": {
"text": "Some text 4"
}
}]
}, {
"type": "vertical",
"subviews": [{
"type": "image",
"icon": "ic_no_photo"
}, {
"type": "text",
"fields": {
"text": "Some text 5"
}
}]
}]
}]
}
I'm trying to generate instance of Skeleton class.
data class Skeleton (val type : String,
val name: String,
val icon: String,
val fields: List<Field>,
val styles: Map<String, Map<String, Any>>,
val subviews : List<Skeleton>)
data class Field (val type: String, val value: Any)
As you can see, Skeleton object can have other Skeleton objects inside (and these objects can have other Skeleton objects inside too), also Skeleton can have List of Field objects
val mapper = jacksonObjectMapper()
val skeleton: Skeleton = mapper.readValue(File(file))
This code ends with exception:
com.fasterxml.jackson.databind.JsonMappingException: Instantiation of [simple type, class com.uibuilder.controllers.parser.Skeleton] value failed (java.lang.IllegalArgumentException): Parameter specified as non-null is null: method com.uibuilder.controllers.parser.Skeleton.<init>, parameter name
at [Source: docs\layout.txt; line: 14, column: 3] (through reference chain: com.uibuilder.controllers.parser.Skeleton["subviews"]->java.util.ArrayList[0]->com.uibuilder.controllers.parser.Skeleton["subviews"]->java.util.ArrayList[0])

There are several issues I found about your mapping that prevent Jackson from reading the value from JSON:
Skeleton class has not-null constructor parameters (e.g. val type: String, not String?), and Jackson passes null to them if the value for those parameters is missing in JSON. This is what causes the exception you mentioned:
Parameter specified as non-null is null: method com.uibuilder.controllers.parser.Skeleton.<init>, parameter name
To avoid it, you should mark the parameters that might might have values missing as nullable (all of the parameters in your case):
data class Skeleton(val type: String?,
val name: String?,
val icon: String?,
val fields: List<Field>?,
val styles: Map<String, Map<String, Any>>?,
val subviews : List<Skeleton>?)
fields in Skeleton has type List<Field>, but in JSON it's represented by a single object, not by an array. The fix would be to change the fields parameter type to Field?:
data class Skeleton(...
val fields: Field?,
...)
Also, Field class in your code doesn't match the objects in JSON:
"fields": {
"text": "Some text 1"
}
You should change Field class as well, so that it has text property:
data class Field(val text: String)
After I made the changes I listed, Jackson could successfully read the JSON in question.
See also: "Null Safety" in Kotlin reference.

Related

RealmList<String> as a JSON Schema - Mongo DB Realm

I have a simple model class from which I need to generate the schema on Mongo DB Atlas. But I'm having troubles when it comes to defining RealmList<String> inside a JSON schema. If I insert "array" as a bsonType, I get an error. What should I write instead?
Model class:
class Note : RealmObject {
#PrimaryKey
var _id: ObjectId = ObjectId.create()
var title: String = ""
var description: String = ""
var images: RealmList<String> = realmListOf()
var date: RealmInstant = RealmInstant.from(System.currentTimeMillis(),0)
}
Current Schema:
{
"bsonType": "object",
"properties": {
"_id": {
"bsonType": "objectId"
},
"title": {
"bsonType": "string"
},
"description": {
"bsonType": "string"
},
"images": {
"bsonType": "array"
},
"date": {
"bsonType": "date"
}
},
"required": [
"_id",
"title",
"description",
"images",
"date"
],
"title": "Note"
}
I am not sure which mode you're using but if you're in development mode, when you add an object in the SDK, the server will automatically generate a matching object, as long as the changes are additive, like adding a new object or property
In the queston, the 'images' bson definition looks incomplete
"images": {
"bsonType": "array"
},
While it is an array, it's an array of strings so I believe it should look more like this
"images": {
"bsonType": "array",
"items": {
"bsonType": "string"
}
}
Where the type of items is defined as a string

How do I assign a value in JSON that becomes an enum string value in TypeScript?

I'm trying to add sample data for my unit tests and I'm getting an assignment error when I'm trying to read in the JSON file related to the enum assignment.
types.ts
export enum TestType {
TYPE1 = "type1",
TYPE2 = "type2",
TYPE3 = "type 3"
}
export interface SampleDataType {
id: string;
name: string;
dataType?: TestType;
}
exampleData.json
[
{
"id": "1",
"name": "first",
"dataType": "type1"
},
{
"id": "2",
"name": "second",
"dataType": "type2"
},
{
"id": "3",
"name": "third",
"dataType": "type3"
}
]
unitTest.ts
import * as exampleData from "../../components/__mocks__/test-data/exampleData.json";
//...
const testItems: SampleDataType[] = exampleData;
I'm getting the following error:
I've also tried using "TYPE1" as the sample data value, but no luck. Is there a way to assign the value in json and have it read in the unit test?

Generate Data class on dynamic value inside a JSON payload

I have a payload which look like this :
{
"data": {
"12345": {
"is_indexable": true,
"description": "Lorem description",
"items": {
"id": 2644,
"name": "Naming here"
}
},
"678910": {
"is_indexable": false,
"description": "Lorem description 2",
"items": {
"id": 29844,
"name": "Naming here again"
}
}
}
}
I wanted to generate a specific data class for that payload using tools like https://transform.tools/json-to-kotlin but it's impossible since the array inside the "data" object is an ID (so a dynamic data)
data class Root(
val data_field: Data
)
data class Data(
val payload: List<Payload>, // Something to represent the dynamic ids
)
data class Payload(
val isIndexable: Boolean,
val description: String,
val items: Items
)
data class Items(
val id: Long,
val name: String
)
I don't know if I'm clear, did you have an idea to make this in a clean way ?
Thank you all !
You need to array of map to achieve this. You are building map in map right now. It should be like following if you need array;
{
"data": [
{ id: "12345",
"is_indexable": true,
"description": "Lorem description",
"items": {
"id": 2644,
"name": "Naming here"
}
},
{"id" : "678910"
"is_indexable": false,
"description": "Lorem description 2",
"items": {
"id": 29844,
"name": "Naming here again"
}
}
]
}
There is an amazing plugin add to the android studio where it converts this payload to suitable classes
for example, the JSON has one object and inside the object, there are a lot of objects and array this tool will determine all this as classes
you can load it from this link:
https://plugins.jetbrains.com/plugin/9960-json-to-kotlin-class-jsontokotlinclass
From this link, you can also know how you can use this tool
go to android studio and from file->setting->plugin->Install Plugin from disk
enter image description here
and convert it by easy and without problems
good luck

How to traverse list of nested maps in scala

I have been given a json string that looks like the following one:
{
"dataflows": [
{
"name": "test",
"sources": [
{
"name": "person_inputs",
"path": "/data/input/events/person/*",
"format": "JSON"
}
],
"transformations": [
{
"name": "validation",
"type": "validate_fields",
"params": {
"input": "person_inputs",
"validations": [
{
"field": "office",
"validations": [
"notEmpty"
]
},
{
"field": "age",
"validations": [
"notNull"
]
}
]
}
},
{
"name": "ok_with_date",
"type": "add_fields",
"params": {
"input": "validation_ok",
"addFields": [
{
"name": "dt",
"function": "current_timestamp"
}
]
}
}
],
"sinks": [
{
"input": "ok_with_date",
"name": "raw-ok",
"paths": [
"/data/output/events/person"
],
"format": "JSON",
"saveMode": "OVERWRITE"
},
{
"input": "validation_ko",
"name": "raw-ko",
"paths": [
"/data/output/discards/person"
],
"format": "JSON",
"saveMode": "OVERWRITE"
}
And I have been asked to use it as some kind of recipe for an ETL pipeline, i.e., the data must be extracted from the "path" specifid in the "sources" key, the transformations to be carried out are specified within the "transformations" key and, finally, the transformed data must saved to one of the two specified "sink" keys.
I have decided to convert the json string into a scala map, as follows:
val json = Source.fromFile("path/to/json")
//parse
val parsedJson = jsonStrToMap(json.mkString)
implicit val formats = org.json4s.DefaultFormats
val parsedJson = parse(jsonStr).extract[Map[String, Any]]
so, with that, I get a structure like this one:
which is a map whose first value is a list of maps. I can evaluate parsedJson("dataflows") to get:
which is a list, as expected, but, then I cannot traverse such list, even though I need to in order to get to the sources, transformations and sinks. I have tried using the index of the listto, for example, get its first element, like this: parsedJson("dataflows")(0), but to no avail.
Can anyone please help me traverse this structure? Any help would be much appreciated.
Cheers,
When you evaluate parsedJson("dataflows") a Tuple2 is returned aka a Tuple which has two elements that are accessed with ._1 and ._2
So for dataflows(1)._1 the value returned is "sources" and dataflows(1)._2 is list of maps (List[Map[K,V]) which can be traversed like you would normally traverse elements of a List where each element is Map
Let's deconstruct this for example:
val dataFlowsZero = ("sources", List(Map(42 -> "foo"), Map(42 -> "bar")))
The first element in the Tuple
scala> dataFlowsZero._1
String = sources
The second element in the Tuple
scala> dataFlowsZero._2
List[Map[Int, String]] = List(Map(42 -> foo), Map(42 -> bar))`
Map the keys in each Map in List to a new List
scala> dataFlowsZero._2.map(m => m.keys)
List[Iterable[Int]] = List(Set(42), Set(42))
Map the values in each Map in the List to a new List
scala> dataFlowsZero._2.map(m => m.values)
List[Iterable[String]] = List(Iterable(foo), Iterable(bar))
The best solution is to convert the JSON to the full data structure that you have been provided rather than just Map[String, Any]. This makes it trivial to pick out the data that you want. For example,
val dataFlows = parse(jsonStr).extract[DataFlows]
case class DataFlows(dataflows: List[DataFlow])
case class DataFlow(name: String, sources: List[Source], transformations: List[Transformation], sinks: List[Sink])
case class Source(name: String, path: String, format: String)
case class Transformation(name: String, `type`: String, params: List[Param])
case class Param(input: String, validations: List[Validation])
case class Validation(field: String, validations: List[String])
case class Sink(input: String, name: String, paths: List[String], format: String, saveMode: String)
The idea is to make the JSON handler do most of the work to create a type-safe version of the original data.

Parse JSON array in Typescript

i have a JSON response from remote server in this way:
{
"string": [
{
"id": 223,
"name": "String",
"sug": "string",
"description": "string",
"jId": 530,
"pcs": [{
"id": 24723,
"name": "String",
"sug": "string"
}]
}, {
"id": 247944,
"name": "String",
"sug": "string",
"description": "string",
"jlId": 531,
"pcs": [{
"id": 24744,
"name": "String",
"sug": "string"
}]
}
]
}
In order to parse the response, to list out the "name" & "description", i have written this code out:
interface MyObj {
name: string
desc: string
}
let obj: MyObj = JSON.parse(data.toString());
My question is how do i obtain the name and description into a list that can be displayed.
You gave incorrect type to your parsed data. Should be something like this:
interface MyObj {
name: string
description: string
}
let obj: { string: MyObj[] } = JSON.parse(data.toString());
So it's not MyObj, it's object with property string containing array of MyObj. Than you can access this data like this:
console.log(obj.string[0].name, obj.string[0].description);
Instead of using anonymous type, you can also define interface for it:
interface MyRootObj {
string: MyObj[];
}
let obj: MyRootObj = JSON.parse(data.toString());