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
Struggling to parse what appears to be a simple response from an API service (GraphQL / JSON). The response is retrieved as String based on HTTP request, and have tried different approaches (using both JsonObject/JsonParse and JsonArrayfrom com.google.gsonlibrary to get desired string elements to String, although without being able to extract anything.
Anyone willing to share suggestions how to parse and get "total" and "startsAt" from the response shown below?
{
"data": {
"viewer": {
"homes": [
{
"currentSubscription": {
"priceInfo": {
"current": {
"total": 0.5626,
"energy": 0.4336,
"tax": 0.129,
"startsAt": "2019-11-14T20:00:00+01:00"
}
}
}
}
]
}
}
}
You can get the json value like this
Assign the response to some variable like res.
For ex :
var res = {
"data": {
"viewer": {
"homes": [
{
"currentSubscription": {
"priceInfo": {
"current": {
"total": 0.5626,
"energy": 0.4336,
"tax": 0.129,
"startsAt": "2019-11-14T20:00:00+01:00"
}
}
}
}
]
}
}
var total=res.data.viewer.homes[0].currentSubscription.priceInfo.current.total;
var startAt = res.data.viewer.homes[0].currentSubscription.priceInfo.current.startsAt;
console.log('total is: ',total);
console.log('startsAt: ', startsAt)
I want to access the the below JSON payload :
[
{
"ApplicantContactDetail": [
{
"ApplicantBaseDetails": {
"CATEGORY": "P",
"ULN": "1265847478"
} }
]
}
]
and need output as
{
"Number":2,
"Data":
{
"ResidencyCode":"CATEGORY"
}
}
i need to map CATEGORY in the output from liquid template, may i know the correct syntax for this to achieve.
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": ""
}
}
}
}
}
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.