How to create Interface for Nested JSON Object - json

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
} }
}
]
}`

Related

MongoDB: Variable nested in several objects

I have a problem creating a query in MongoDB. I have the following JSON. How to ask mongodb question to get _id from level5 object?
{
"Id": "1",
"level1": {
"level2": {
"level3": {
"level4": {
"level5": {
"_id": 2
}
}
}
}
}
}
You can use the dot notation to get value from nested object:
db.collection.aggregate([
{
$project: {
value: "$level1.level2.level3.level4.level5._id"
}
}
])
Mongo Playground

Generate JSON With JSON-Views

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
}*/

Nested JSON Format

I have a data Called 'Village'.
Country:
{
State:{
District:{
Taluk:{
Village: String
}
}
}
}
Is this correct JSON format for Data "Village".
Following is the modified json to make it valid
{
"Country": {
"State": {
"District": {
"Taluk": {
"Village": "String"
}
}
}
}
}
and to validate any json string use http://jsonlint.com/
You don't declare types such as String in JSON. Also, you need to quote your variable names. It seems like your declaring a structure in JSON. I would replace String with an empty string, that is essentially declaring the type:
{"Country":
{
"State":{
"District":{
"Taluk":{
"Village": ""
}
}
}
}
}

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.

how to declare two arrays in json?

I want to create a json string for my web application. Actually i am new to this json format.In my json string i have to create two arrays in my json structure.But i have some syntax problem in creating two arrays. my json string is given below for your reference.
{
MarkUpdate:[
{
'FinalMarks':[
{
'studentId':'S1',
'Ques_Mark':[
{
'qId' :'Q1',
'mark':'14',
},
{
'qId':'Q2',
'mark':'10',
}
]
},
{
'studentId':'S2',
'Ques_Mark':[
{
'qId' :'Q1',
'mark':'12',
},
{
'qId':'Q2',
'mark':'13',
}
]
}
]
}
]
}
In my above json string format,my MarkUpdate contains one array object named as FinalMarks.So,here i have to create one more array object named as EvalMarks under MarkUpdate.
Acually my EvalMarks contains following elements...
'EvalMarks':[
{
'EvalId':'E1',
'Ques_Mark':[
{
'qId' :'Q1',
'studId':'S1',
'mark':'13',
},
{
'qId':'Q2',
'studId':'S1',
'mark':'13',
}
]
},
{
'EvalId':'E2',
'Ques_Mark':[
{
'qId' :'Q1',
'studId':'S2',
'mark':'10',
},
{
'qId':'Q2',
'studId':'S2',
'mark':'10',
}
]
}
]
So, i have declare this EvalMarks under the MarkUpdate.I missed the syntax...
Could you plz tell me how to add this array object under the MarkUpdate.
guide me to get out of this issue...
To declare two arrays in one JSON object, remember that the JSON object can only be a single object, therefore the array must be inside the enclosing curly braces. For example:
{
"array1":[1,2,3],
"array2":["jim","louise","mark"]
}
For your case, it's important to remember that you should properly indent your braces, square and curly, so that you can visually identify mistakes before they become problems. I stringly recommend http://jslint.com/ to validate your JSON before using it. It's also great for Javascript:
{
"MarkUpdate":[
{
"FinalMarks":[
{
"studentId":"S1",
"Ques_Mark":[
{
"qId" :"Q1",
"mark":"14"
},
{
"qId":"Q2",
"mark":"10"
}
]
},
{
"studentId":"S2",
"Ques_Mark":[
{
"qId" :"Q1",
"mark":"12"
},
{
"qId":"Q2",
"mark":"13"
}
]
}
]
}
],
"EvalMarks":[
{
"EvalId":"E1",
"Ques_Mark":[
{
"qId" :"Q1",
"studId":"S1",
"mark":"13"
},
{
"qId":"Q2",
"studId":"S1",
"mark":"13"
}
]
},
{
"EvalId":"E2",
"Ques_Mark":[
{
"qId" :"Q1",
"studId":"S2",
"mark":"10"
},
{
"qId":"Q2",
"studId":"S2",
"mark":"10"
}
]
}
]
}
Its not a valid JSON if you have commas after the last key-value pair in an object. I would start by knocking off all those unnecessary commas after the last key-value pairs inside most of your objects and validating the JSON in www.jslint.com
To be more clear ,
{
"qId":"Q2",
"studId":"S2",
"mark":"10",
}
is Not Valid.
On the other hand,
{
"qId":"Q2",
"studId":"S2",
"mark":"10"
}
Is valid.