List Objects in JSON - arrays

Which one is the correct JSON syntax for List of Objects ?
I have seen different implementation at different sources.
{
"Employees": [
{
"name": "foo",
"age": 23
},
{
"name": "bar",
"age": 37
}
]
}
or
{
"employees": [
{
"employee": {
"name": "foo",
"age": 23
}
},
{
"employee": {
"name": "bar",
"age": 37
}
}
]
}

I would prefer the first one for your data type.
{
"Employees": [
{
"name": "foo",
"age": 23
},
{
"name": "bar",
"age": 37
}
]
}
Lets say you want to retrieve an employee.
var firstEmployee = Employees[0]
Seems logical.

Related

Find the documents whose age is 24 or lives in the state UP

Sample data
{
"_id": ObjectId("6068b4d1ba926fd240c5216f"),
"name": "Amit",
"branch": "ECE",
"joiningYear": 2017,
"language": [
"Python",
"C#"
],
"personal": {
"contactinfo": 234556789,
"state": "UP",
"age": 25,
"semestermarks": [
80,
80.1,
98,
70
]
},
"salary": 10000
}
Query
db.college.find({$or:[{"age":24},{"state":"UP"}]})
Demo - https://mongoplayground.net/p/mJeXeT-oCe8
Key is "personal.age"
As your data is inside personal key you have to query inside it.
db.collection.find({
$or: [
{
"personal.age": 24
},
{
"personal.state": "UP"
}
]
})

Extracting an array from within an object

I have the following JSON object from which I'd like to extract a specific property
{
"gender": "male",
"age": 36,
"profession": "teacher",
"hobbies": [
{
"id": 28,
"name": "gardening"
},
{
"id": 878,
"name": "Football"
},
{
"id": 35,
"name": "Reading"
},
{
"id": 10751,
"name": "Socialising"
}
],
"Country": "Sweden",
"id": 4542236,
}
Lets say I wanted to create a new object that hold only the "hobbies" property with all the values like this
{
"hobbies": [
{
"id": 28,
"name": "gardening"
},
{
"id": 878,
"name": "Football"
},
{
"id": 35,
"name": "Reading"
},
{
"id": 10751,
"name": "Socialising"
}
]
}
Since the JSON is coming from an external API I have no control on future positioning (index) of any properties

Database query to filter data inside an object array

I have bunch of documents in the following format in my mongodb, I am using moongoose as ORM.
Can some one help me make a query to get all the contents having the name=abc inside data-items.content
Document 1:
{
"title": "Some title",
"addresse": "data",
"data-items": {
"content": [
{
"name": "abc",
"age": "poster5.jpg"
},
{
"name": "def",
"age": "poster5.jpg"
},
{
"name": "hij",
"age": "poster5.jpg"
}]
}
}
Document 2:
{
"title": "another title",
"addresse": "data",
"data-items": {
"content": [
{
"name": "abc",
"age": "poster7.jpg"
},
{
"name": "def",
"age": "poster5.jpg"
},
{
"name": "hij",
"age": "poster5.jpg"
}]
}
}
Any help is appreciated
You can simply use the dot notation to query an array of nested documents:
Model.find({"data-items.content.name": "abc"})
EDIT: to get only subdocuments matching your condition you can use below aggregation:
Model.aggregate([
{
$match: {
"data-items.content.name": "abc"
}
},
{
$unwind: "$data-items.content"
},
{
$match: {
"data-items.content.name": "abc"
}
},
{
$replaceRoot: {
newRoot: "$data-items.content"
}
}
])
$unwind will give you single document per content and $replaceRoot will promote it to the root level.

GROOVY transform single array into deep nested array

I have an array as the source. I want to transform source into result by using Groovy.
I don't see any similar question. That's why I post here.
I tried to get the first member in the family and put all other members into a subList with this code but it failed
source.each{ family -> family.each{
member -> member.get(0).collate(1,family.size()-1)
}
}
source:
[
[{
"id": "0001",
"role": "parent",
"age": 30
},
{
"id": "0002",
"role": "child",
"age": 1
},
{
"id": "0003",
"role": "child",
"age": 3
}
],
[{
"id": "0004",
"role": "parent",
"age": 31
},
{
"id": "0005",
"role": "child",
"age": 5
}
]
]
result:
[{
"id": "0001",
"role": "parent",
"age": 30,
"children": [{
"id": "0002",
"role": "child",
"age": 1
},
{
"id": "0003",
"role": "child",
"age": 3
}
]
},
{
"id": "0004",
"role": "parent",
"age": 31,
"children": [{
"id": "0005",
"role": "child",
"age": 5
}]
}]
You can shape the data by adding the "parent" map with a new map only containing the children (+ in groovy does that merge). E.g.:
def data = new groovy.json.JsonSlurper().parseText('[[{ "id": "0001", "role": "parent", "age": 30 }, { "id": "0002", "role": "child", "age": 1 }, { "id": "0003", "role": "child", "age": 3 } ], [{ "id": "0004", "role": "parent", "age": 31 }, { "id": "0005", "role": "child", "age": 5 }]]')
println(data.collect{ groups ->
// XXX
groups.find{ it.role=="parent" } + [children: groups.findAll{it.role=="child"}]
})
// => [[id:0001, role:parent, age:30, children:[[id:0002, role:child, age:1], [id:0003, role:child, age:3]]], [id:0004, role:parent, age:31, children:[[id:0005, role:child, age:5]]]]

Groovy JsonBuilder, array inside an array

I need to build the following json using groovy's JsonBuilder().
[
{
"date": "2017-12-08",
"dog": [
{
"name": "Joe",
"age": "1"
},
{
"name": "Bro",
"age": "2"
},
{
"name": "Doe",
"age": "3"
}
]
}
]
I cant get the array in the array, because of the date, it wants to always to put date not in the same level as dog array, but put it in { }, like:
[{
{
"date": "2017-12-08",
},
"dog": [
{
"name": "Joe",
"age": "1"
},
{
"name": "Bro",
"age": "2"
},
{
"name": "Doe",
"age": "3"
}
]
}
]
Just stick the date alongside your list in the model:
import groovy.json.JsonBuilder
import groovy.transform.Canonical
#Canonical
class Dog {
int age
String name
}
#Canonical
class DogList {
String date
List<Dog> dog
}
def ark = [
new DogList('2017-12-08', [
new Dog(1, 'Joe'),
new Dog(2, 'Bro'),
new Dog(3, 'Doe')
])
]
def json = new JsonBuilder(ark).toPrettyString()
println json
prints:
[
{
"date": "2017-12-08",
"dog": [
{
"age": 1,
"name": "Joe"
},
{
"age": 2,
"name": "Bro"
},
{
"age": 3,
"name": "Doe"
}
]
}
]

Resources