Generate JSON With JSON-Views - json

I am trying to use JSON-Views in Grails 3.1.
I have the following controller:
package myapp
BasketController {
def index(ProductFilterCommand cmd) {
[basketList: service.findAllBaskets()]
}
}
And the following classes:
package myapp
class Basket {
List<BasketItem> items
}
class BasketItem {
String name
}
Here are the gson files which I thought would work:
basket/index.gson
import myapp.Basket
model {
Iterable<Basket> basketList
}
json.baskets(basketList) {
g.render(template: "basket", model: [basket: it])
}
basket/_basket.gson
import myapp.Basket
model {
Basket basket
}
json.items(basket.items) {
g.render(template: "item", model:[item: it])
}
basket/_item.gson
import myapp.Item
model {
Item item
}
json g.render(item)
I want to generate json such as:
{
"baskets": [{
"items": [{
"name": "T-shirt"
}, {
"name": "Pants"
}]
}, {
"items": [{
"name": "T-shirt"
}, {
"name": "Pants"
}]
}]
}
But instead I am getting:
{
"baskets": [
{},
{}
]
}

Looks like a bug to me. The only way to achieve what you are looking for is to use the views as shown below. Also note the usage of collection instead of model. I would file a bug with the sample app I used to test below.
Note the usage of template as a fully qualified name basket/item. This is the
defect.
//index.gson
import com.example.Basket
model {
Iterable<Basket> basketItems
}
json {
baskets g.render(template: 'basket', collection: basketItems, var: 'basket')
}
//_basket.gson
import com.example.Basket
model {
Basket basket
}
json {
items g.render(template: "basket/item", collection: basket.items, var: 'item')
}
//_item.gson
import com.example.BasketItem
model {
BasketItem item
}
json g.render(item)
//or if id is not required in response
/*json {
name item.name
}*/

Related

How to create Interface for Nested JSON Object

I am having a Nested JSON Object and I need to create the Interface and Use that in my TS File...
{
"states":[
{
"location":"banglore",
"weather":{
"place":{
"heat":40,
"cold":60,
"place":"banglore"
}
}
},
]
}
Try this one
`export interface States{
"states":[
{
"location": string,
"weather":{
"place":{
"heat":number,
"cold":number,
"place":string
} }
}
]
}`

How to use JSON type in prisma, graphql nexus framework?

Please for example how to use JSON type
generator prisma_client {
provider = "prisma-client-js"
}
datasource db {
provider = "mysql"
url = env("DATABASE_URL")
}
model t {
id Int #default(autoincrement()) #id
val Json?
}
I need code of mutation.
I used answer from
GraphQL Mutation with JSON Patch
enabled crud by
import { use } from 'nexus'
import { prisma } from 'nexus-plugin-prisma'
use(prisma({features:{crud:true}}))
and send this mutation:
mutation {
createOnet(data: {
val: "{ \"name\": \"michael\" }"
}) {
id
val
}
}
But I have response:
{
"error": [
{
"message": "Expected type Json, found \"{ \\\"name\\\": \\\"michael\\\" }\"; Cannot read property 'forEach' of undefined",
"locations": [
{
"line": 2,
"column": 26
}
]
}
]
}
It should be as follows:
mutation {
createOnet(data: {
val: { name: "michael" }
}) {
id
val
}
}
No escaping is required as Nexus automatically handles that for you.

Obtaining the child values randomly in JSON

I have a JSON like below: I need to extract the Options -> Child as a Random and also Values within the options as randomly. How can we achieve in jmeter ?
{
"id":37,
"merchant_id":"39",
"title":"Parker Pens",
"subtitle":null,
"price":1000,
"description":null,
"images":[ ],
"image_thumbs":[ ],
"options":[
{
"code":"color",
"label":"Color",
"extra_info":"",
"values":[
{ },
{ },
{ }
]
},
{
"code":"size",
"label":"Size",
"extra_info":"",
"values":[
{ },
{ },
{ }
]
}
],"options_available":[
{ },
{ },
{ },
{ },
{ },
{ },
{ },
{ },
{ }
], "custom_options":[
]
}
I have to fetch the child of options randomly . In that i have to fetch the value of "Code" and its associated value within the "Value" .
Help is appreciated and useful
Your requirements are a little bit vague as you haven't indicated what is the desired output format. One of the solutions would be using JSR223 PostProcessor in order to obtain the random value from random options array like:
import com.jayway.jsonpath.JsonPath
import org.apache.commons.lang3.RandomUtils
import org.apache.jmeter.samplers.SampleResult
def options = JsonPath.read(prev.getResponseDataAsString(), '$.options')
def randomOption = options.get(RandomUtils.nextInt(0, options.size()))
def values = randomOption.get('values')
def randomValue = values.get(RandomUtils.nextInt(0, values.size())) as String
vars.put('randomValue', randomValue)
References:
Jayway JsonPath - A Java DSL for reading JSON documents
Apache Groovy - Why and How You Should Use It
Apache Groovy - Parsing and Producing JSON

map json response into nested typescript object with rxjs

In my Angular2-App I´m receiving a JSON-Response via http-Request that kind of looks like that:
{
"documents": [
{
"title": "Example-Doc 1",
"versions": [
{
"fileSize": 15360
},
{
"fileSize": 2048
}
]
},
{
"title": "Example-Doc 2",
"versions": [
{
"fileSize": 15360
},
{
"fileSize": 2048
}
]
}
],
"meta": {
"total": [2]
}
}
Now i wonder how to map this structure into my TypeScript-Classes, i checked different approaches, but it never worked. I actually need the constructor of the Version class to be called.
export class Document {
title: string; // Titel des Dokuments
versions: Version[];
}
If you have complex classes that need to be serialized and deserialized, I suggest that you implement static methods to your classes like fromJson and toJson - however without knowing your classes the rest of the answer will be kind of a guess-work:
Assuming you have a fromJson in place, then you can map your data like the following:
const myDocuments: Document[] = myJson.documents.map(Document.fromJson);
And the fromJson-method could look like this:
class Document {
constructor(public title: string, public versions: Version[])
public static fromJson(json: any): Document {
return new Document(
json.title,
json.versions.map(Version.fromJson)
);
}
}
class Version {
constructor(public fileSize: number) {}
public static fromJson(json: any): Version {
return new Version(json.fileSize);
}
}

How do we display generated class name in small letters in JSON Response

I have JSON schema as follows
{
"type":"object",
"$schema": "http://json-schema.org/draft-03/schema",
"required":false,
"properties":{
"example": {
"type":"object",
"required":false,
"properties":{
"age": {
"type":"number",
"required":false
}
}
}
}
}
Which is saved as ExampleResponse.jss .
Using 'jsonschema2pojo' maven plugin, i generated POJO class from this schema. Using this class I am trying to create a JSON response and the expected output response should be
{
"example":{
"age":68
}
}
Since the Generated POJO is
class Example { ..... }
output Response i am getting is
{
"Example":{
"age":68
}
}
How I can achieve desired response in which i have lower case 'example' word.