JSON parsing in ios5 without array name? - iphone

Here i m putting code of my json file..can anyone help me "How to parse this json file?".I tried a lot of tutorials.I got some hints.But in my json file i dont have array name.so i couldnt use any key value to access those objects.
[
{
id: 1,
name: "Ice Cube",
properties: [
{
propertyMeta: {
name: "Color",
type: 5
},
value: "Venfield"
},
{
propertyMeta: {
name: "Size",
type: 1
},
value: "38"
}
]
},
{
id: 2,
name: "Lite Shirt",
properties: [
{
propertyMeta: {
name: "Color",
type: 5
},
value: "Otto"
},
{
propertyMeta: {
name: "Size",
type: 1
},
value: "42"
}
]
},
{
id: 3,
name: "Holiday Tops",
properties: [
{
propertyMeta: {
name: "Color",
type: 5
},
value: "Van Heusen"
},
{
propertyMeta: {
name: "Size",
type: 1
},
value: "39"
}
]
}
]

If you're using SBJSON you'd just:
NSArray *myArray = [myJSONString JSONvalue];
And then loop through it as normal

Related

MongoDB validation part with any one of the field check

Trying to figure out how to validate an array of object and map(key, value) pair
{
"commonIdentification": {
"CR": "BR",
"SN": "NAVS87397394"
}
"digitalIdentiifcation": {
"UUID": "326f040b-cf14-4cf9-9e67-57f7ca3ce1b2"
}
}
Here , I want to add the validation like,
In commonIdentification with the presence of CR ,no need for digitalIdentiifcation's presence
In commonIdentification with the absence of CR, digitalIdentiifcation's 'UUID' field is required.
Can anyone please help on reformatting the validation part?I am struggling to validate based on the above points.
{
$jsonSchema: {
properties: {
commonIdentification: {
type: 'object',
required: [
'CR'
],
properties: {
CR: {
type: 'string',
description: 'CR is mandatory '
}
}
}
},
type: 'object',
required: [
'commonIdentification',
'digitalIdentiifcation'
]
}
}
{
$jsonSchema: {
type: 'object',
required: [
'commonIdentification'
],
anyOf: [
{
required: [
'digitalIdentiifcation'
]
},
{
required: [
'commonIdentification'
]
}
],
properties: {
commonIdentification: {
type: 'object',
anyOf: [
{
required: [
'CR'
]
},
{
required: [
'SN'
]
}
],
properties: {
SN: {
type: 'string',
description: 'SN or CR or digitalIdentiifcation is mandatory '
},
CR: {
type: 'string',
description: 'SN or CR or digitalIdentiifcation is mandatory '
}
},
description: 'SN or CR or digitalIdentiifcation is mandatory '
},
digitalIdentiifcation: {
description: 'digitalIdentiifcation'
}
}
}
}

In Sails.js v0.10 after populating data as a value i get [Getter/Setter]

After populating data i get result not as expected.
Example:
//User.js Model
module.exports = {
attributes: {
//"id" attribute is here primary key by default
schools: { collection: 'School', via: 'id' },
name: { type: 'String', trim: true, required: true }
}
}
And this is my School.js
//School.js Model
module.exports = {
attributes: {
//"id" attribute is here primary key by default
name: { type: 'String', trim: true, required: true },
}
}
My User entity data looks like this:
//User document in MongoDB
{
_id: 1,
name: "Foo",
schools: [1,2,3]
}
My School entity data looks like this:
//School documents in MongoDB
{
_id: 1,
name: "School 1"
}
{
_id: 2,
name: "School 2"
}
{
_id: 3,
name: "School 3"
}
Now i want to populate the schools. I do it like this:
User.find().populate("schools").exec(function(err, res){ console.log(res[0]) });
And this is what i get as a result:
{
schools: [Getter/Setter],
name: "Foo",
id: 1
}
Expected:
{
schools: [
{id: 1, name: "School 1"},
{id: 2, name: "School 2"},
{id: 3, name: "School 3"}
],
name: "Foo",
id: 1
}
How can i get expected results?
I use MongoDB as data storage.
Versions:
Sails.js: v0.10.0-rc5
Waterline: v0.10.0-rc7
sails-mongo: 0.10.0-rc2
Those are the expected results when using console.log! If you want to see the expanded object, do:
console.log(res[0].toObject());

MongoDB get attribute from array inside another array

Series is one document. It has an array of series inside it (in this case it has 'Revenge' and 'Raines').
Each series has a cast array with names. And I need a query to get those names.
Who know how can I get a list of all the names from both cast arrays?
My best approach was this query db.series.find( {}, { _id: 0, cast: 1 } ) where a get a cursor with the two cast json arrays.
{ series:
[
{
name: 'Revenge',
user_rating: 7.9,
duration: 44,
genres: [ ' Drama', ' Mystery', ' Thriller' ],
year_start: '2011',
year_end: '',
cast:
[ { name: 'Madeleine Stowe' },
{ name: 'Emily VanCamp' },
{ name: 'Gabriel Mann' },
{ name: 'Nick Wechsler' },
{ name: 'Henry Czerny' },
{ name: 'Joshua Bowman' },
{ name: 'Christa B. Allen' },
{ name: 'Ashley Madekwe' },
{ name: 'Connor Paolo' },
{ name: 'Barry Sloane' },
{ name: 'Margarita Levieva' } ],
seasons: [ { number: '3' }, { number: '2' }, { number: '1' } ]
},
{
name: 'Raines',
user_rating: 7.4,
duration: 45,
genres: [ ' Crime', ' Drama' ],
year_start: '2007',
year_end: '',
cast:
[ { name: 'Jeff Goldblum' },
{ name: 'Matt Craven' },
{ name: 'Nicole Sullivan' },
{ name: 'Linda Park' },
{ name: 'Dov Davidoff' },
{ name: 'Malik Yoba' },
{ name: 'Madeleine Stowe' } ],
seasons: [ { number: '1' } ]
}
]
}
I need an output like this:
I need this:
{ name: 'Madeleine Stowe' },
{ name: 'Emily VanCamp' },
{ name: 'Gabriel Mann' },
{ name: 'Nick Wechsler' },
{ name: 'Henry Czerny' },
{ name: 'Joshua Bowman' },
{ name: 'Christa B. Allen' },
{ name: 'Ashley Madekwe' },
{ name: 'Connor Paolo' },
{ name: 'Barry Sloane' },
{ name: 'Margarita Levieva' },
{ name: 'Jeff Goldblum' },
{ name: 'Matt Craven' },
{ name: 'Nicole Sullivan' },
{ name: 'Linda Park' },
{ name: 'Dov Davidoff' },
{ name: 'Malik Yoba' },
{ name: 'Madeleine Stowe' }
You can use aggregation framework for this:
db.series.aggregate( { $unwind : "$series" },
{ $unwind : "$series.cast" },
{ $group : { _id : "$_id",
cast : {$push:"$series.cast}
}
}
);
If you want to consolidate multiple actor appearances into one then replace $push with $addToSet.

Get desired output using mongoDB find

I have following documents in my collection
{
_id: ObjectId("5166fefbc482c31052000002"),
contact: [
{
home: 7735734105
},
{
office: 9583866301
}
],
user_name: "moti",
reportsTo: "bikram",
technology: [
{
name: ".net",
rating: 5
},
{
name: "JavaScript",
rating: 2
}
],
project: [
"Agile School",
"Draftmate"
],
type: "developer",
email: "motiranjan.pradhan#ajatus.co.in"
}
and
{
_id: ObjectId("5166fe90c482c31052000001"),
contact: [
{
home: 7735734103
},
{
office: 9583866901
}
],
user_name: "ganesh",
reportsTo: "bikram",
technology: [
{
name: "JavaScript",
rating: 3
},
{
name: ".net",
rating: 4
}
],
project: [
"SLBC",
"Draftmate"
],
type: "developer",
email: "ganesh.patra#ajatus.co.in"
}
Now I need to find the rating of the people who know only JavaScript.Currently if I run
db.users.find(
{
technology: {
$elemMatch: {
name: 'JavaScript'
}
}
},{user_name:1,'technology.name':1,_id:0}
).pretty()
I am getting names of all technologies(.net & JavaScript) and their corresponding ratings. I need only user names,and their respective ratings in JavaScript only. Do I need to use any aggregation techniques?
The positional operator '$' can be used to limit query results to the first matching element. To use in your query above you would change it to:
db.users.find( { technology: { $elemMatch: { name: 'JavaScript' } } },{user_name:1,'technology.$.name':1,_id:0} )

Ember-data: Best Json related resources

Hi I am wondering which is the best solution to work with related resources in ember-data.
In order to build a REST backend.
1) embedded resources
user: {
id: "foo",
albums: [
album: {
id: "foo",
name: "bar",
description: "foobar",
},
album: {
id: "foobar"
name: "foobarfoo",
description: "fumanchu",
},
],
}
2) embedded id of the resources
user: {
id: "foo",
albums: [
album: {
id: "foo",
},
album: {
id: "foobar"
},
],
}
3) linked resources
user: {
id: "blabla",
links: {
"self": { "href": "/users/blabla" },
"albums": { "href": "/albums/blabla" }
},
}
Do you think is possible to use hipermedia as in HATEOAS?
Thanks in advance
Did you have a look at ActiveModelSerializers gem?
It is the easier way to get the job done when working with Rails on the server side.
For relations, it may embed or use references to sub-resources. References (which I would recommend) are serialized this way:
{
users: [{
id: 42,
firstName: "John",
albums: [ 1, 2, 3 ]
}]
}
{
albums: [{
id: 1,
name: "Foo"
}, {
id: 2,
name: "Bar"
}, {
id: 3,
name: "Buzz"
}]
}
Here, albums may also be included as sideloaded data in the first hash:
{
users: [{
id: 42,
firstName: "John",
albums: [ 1, 2, 3 ]
}],
albums: [{
id: 1,
name: "Foo"
...
}]
}