How can I store my data into the MongoDB? - json

I am trying to download stock data using Alpha Vantage API. I send a request and after some work on responded data I have an array like below:
[
{
id: "MSFT",
stockData: [
{ date: "1634601600", summary: [Object] },
{ date: "1634688000", summary: [Object] } ]
}
]
And [Object] is something like:
{
date: "1646956800",
summary: { open: 287.96, high: 289.51, low: 279.43, close: 280.07, volume: 27209330 }
}
But I don't know how can I save it into the MongoDB? I could save a simpler structure and I knew I must create a class with the same property names as the response JSON, then instantiate the class and assign the properties of the response value to the properties of the class instance then save it to the database.
But now I am a little confused and don't know how properly deal with this. Also what if I have an array with more nested values of different size like below:
[
{
id: "AAPL",
stockData: [
{ date: 1634601600, summary: [Object] },
{ date: 1634688000, summary: [Object] },
{ date: 1634774400, summary: [Object] },
{ date: 1634860800, summary: [Object] }
]
},
{
id: "MSFT",
stockData: [
{ date: 1634601600, summary: [Object] },
{ date: 1634688000, summary: [Object] }
]
}
]

Related

Opensea Api returns Asset does not exist when creating an offer

Hi I use the opensea api to create an offer but all it returns is a 400 Error with the message "Asset does not exist". Even though I use the same API before to retrieve the asset. I use the seaport SDK to create the signature and it all works fine
const { executeAllActions } = await seaport.createOrder(
{
offer: [
{
amount: basePrice.toString(),
token: WETH,
}
],
consideration: [item, ...considerationFeeItems],
endTime: endTime.toString(),
zone: DEFAULT_ZONE_BY_NETWORK[opensea.Network.Main],
restrictedByZone: true,
allowPartialFills: false,
conduitKey: CROSS_CHAIN_DEFAULT_CONDUIT_KEY,
counter: 0
},
ourAddress
);
const order = await executeAllActions();
But as soon as I send it to that endpoint https://docs.opensea.io/v2.0/reference/create-an-offer
it gives me back an error.
The order object returned from executeAllActions looks like following:
{
parameters: {
offerer: '0x...',
zone: '0x004c00500000ad104d7dbd00e3ae0a5c00560c00',
zoneHash: '0x3000000000000000000000000000000000000000000000000000000000000000',
startTime: '1660557986',
endTime: '1660644385',
orderType: 2,
offer: [ [Object] ],
consideration: [ [Object], [Object], [Object] ],
totalOriginalConsiderationItems: 3,
salt: '0xc532bab0fd9ae9529b4d8cfc9fc2f02e',
conduitKey: '0x0000007b02230091a7ed01230072f7006a004d60a8d4e71d599b8104250f0000',
counter: 0
},
signature: '0x....'
}
What is going wrong?

Reverse JSON Grails/Groovy

I have a JSON which looks like :
[
{ Name: "mike",
Address: "xyz"
Parent: {
NAme: "john"
Address: "xyz"
Parent: {
NAme: "Ross"
Adress: "asdsa"
}
}
]
I want to reverse it such that it looks like :
[
{ Name: "Ross",
Address: "adsad"
child: {
NAme: "john"
Address: "xyz"
child: {
NAme: "Mike"
Adress: "xyz"
}
}
]
What would be the best way to reverse this using (something generic i.e. the json can be much bigger with more parent objects)?

Root property of a nested JSON or model in sencha

i have a json in the following format:
{
"collection": [
{
"id": 4,
"tickets": {
"collection": [
{
"inner_id": 8,
},
{
"inner_id": 10,
}
],
"count": 2,
"type": "collection"
},
},
{
"id": 5,
"tickets": {
"collection": [
{
"inner_id": 1,
},
{
"inner_id": 2,
}
],
"count": 2,
"type": "collection"
},
},
]
}
For this particular JSON i created the models as:
Ext.define("myProject.model.ABC", {
extend: "Ext.data.Model",
config: {
idProperty: "id",
fields:[
{name: "id", type: "int" },
],
hasMany: [
{
model: "myProject.model.XYZ",
name: "tickets",
associationKey: "tickets",
},
],
}
});
And second store as:
Ext.define("myProject.model.XYZ", {
extend: "Ext.data.Model",
config: {
// idProperty: "id",
fields:[
{name: "inner_id", type: "int" },
],
belongsTo: 'myProject.model.ABC'
}
});
But now i am confused. How do i populate the second store with a root property of collection again.
I know one way is to easily change the json so that there is no collection child inside tickets but i dont want to do that.
Any help would be appreciated. I have simplified the JSON for an easy example.
EDIT:
To be more clear, is there a way i can directly create a model which will read the Collection array inside the tickets object.
EDIT:
Adding the store which populates the model ABC for more understanding
Ext.define("myProject.store.ABCs", {
extend: "Ext.data.Store",
config: {
model: "myProject.model.ABC",
autoLoad: false,
proxy: {
type: "ajax",
url: '', //myURL
reader: {
type: "json",
rootProperty: "collection", // this is the first collection
},
},
}
});
This store loads the ABC model correctly but now i want to load the XYZ model which can load the inner array of collection
belongsTo should be define as follows :
Ext.define('Product', {
extend: 'Ext.data.Model',
fields: [
{ name: 'id', type: 'int' },
{ name: 'category_id', type: 'int' },
{ name: 'name', type: 'string' }
],
associations: [
{ type: 'belongsTo', model: 'Category' }
]
Have you read the doc? They specified the root property.
The name of the property which contains the data items corresponding to the Model(s) for which this Reader is configured. For JSON reader it's a property name (or a dot-separated list of property names if the root is nested). For XML reader it's a CSS selector. For Array reader the root is not applicable since the data is assumed to be a single-level array of arrays.
By default the natural root of the data will be used: the root JSON array, the root XML element, or the array.
The data packet value for this property should be an empty array to clear the data or show no data.
Sometimes the JSON structure is even more complicated. Document databases like CouchDB often provide metadata around each record inside a nested structure like this:
{
"total": 122,
"offset": 0,
"users": [
{
"id": "ed-spencer-1",
"value": 1,
"user": {
"id": 1,
"name": "Ed Spencer",
"email": "ed#sencha.com"
}
}
]
}
In the case above the record data is nested an additional level inside the "users" array as each "user" item has additional metadata surrounding it ('id' and 'value' in this case). To parse data out of each "user" item in the JSON above we need to specify the record configuration like this:
reader: {
type : 'json',
root : 'users',
record: 'user'
}
root as a Function
reader: {
type : 'json',
root : function (obj) {
// I can't reproduce your problem
// so you should check in your console collection.id is right
return obj.collection.id
}
}
// Or, we can use dot notation
reader: {
type : 'json',
root : collection[0].tickets.collection
}
There are two ways to solve this problem. After researching extensively, i found two solutions...
SOLUTION 1:
Instead of making a second model, just create one model and create an array field with type as "auto"
Ext.define("myProject.model.ABC", {
extend: "Ext.data.Model",
config: {
idProperty: "id",
fields:[
{name: "id", type: "int" },
{ name: "tickets", convert: function(value, record) {
if(value.collection instanceof Array) {
return value.collection;
} else {
return [value.collection]; // Convert to an Array
}
return value.collection;
}
}
],
}
});
Now you can refer to an array of tickets from records by:
record.get('tickets')
SOLUTION 2:
Create three model instead of two.
Model 1:
hasOne Association with Tickets
Model 2:
hasMany association with Collection
Model 3:
has all the fields of the innermost array
I can give an example if its not clear enough

Sencha touch 2 load store and associated store with nested JSON

I've started creating a Sencha Touch 2 app that has two models defined, Change and Configuration. A Change belongsTo a Configuration. Both of the models also have a store setup. On the Changes store I have a proxy setup to request data that comes back in JSON format. The JSON data has a Change with a nested configuration. The Change loads just fine but when I try and get the associated Configuration from the Change instance it isn't working.
I've defined the models like this:
Change model:
Ext.define('Changes.model.Change', {
extend: 'Ext.data.Model',
xtype: 'changemodel',
config: {
fields: [
{name:'id', type:'int'},
{name:'changeId', type:'string'},
{name:'briefDescription', type:'string'},
{name:'configuration_id', type:'int'}
],
associations: [{
type:'belongsTo',
model:'Changes.model.Configuration',
primaryKey: 'id',
foreignKey: 'configuration_id',
associationKey: 'configurations'
}],
},
});
Configuration Model:
Ext.define('Changes.model.Configuration', {
extend: 'Ext.data.Model',
xtype: 'configurationmodel',
config: {
fields: [
{ name: 'id', type: 'int'},
{ name: 'longName', type: 'string' },
{ name: 'ciName', type: 'string' },
],
hasMany: {model: 'Change', name: 'changes'}
}
});
Each model has a store.
Changes store:
Ext.define('Changes.store.Changes', {
extend: 'Ext.data.Store',
requires: 'Changes.model.Change',
config: {
model: 'Changes.model.Change',
proxy: {
type: 'ajax',
url: 'services/changes.php',
reader: {
type: 'json',
rootProperty: 'changes'
}
},
sorters: [{ property: 'briefDescription', direction: 'ASC'}],
}
});
Configurations store:
Ext.define('Changes.store.Configurations', {
extend: 'Ext.data.Store',
requires: ['Ext.data.proxy.LocalStorage'],
config: {
model: 'Changes.model.Configuration',
grouper: {
sortProperty: "ciName",
direction: "DESC",
groupFn: function (record) {
return record.get('ciName')[0];
}
},
proxy: {
type: 'ajax',
url: 'services/configurationItems.php',
reader: {
type: 'json',
rootProperty: 'configurationItems'
}
}
}
});
My JSON that is being returned from services/changes.php looks like this:
{
"success": true,
"changes": [
{
"id": 1,
"changeId": "XYZ19178263",
"briefDescription": "Loaded from Proxy",
"configuration_id": 3,
"configurations": [
{
"id": "3",
"longName": "999-99_-_windows_7_desktop.Computer.Windows",
"ciName": "999-99_-_windows_7_desktop"
}
]
}
]
}
In the browser's console I can issue the following commands:
Changes.myStore = Ext.getStore('Changes');
Changes.myStore.load();
Changes.record = Changes.myStore.findRecord('id', '1');
Changes.record.getAssociatedData()
The last command will return an object with a Configuration object inside but all of the field values show null except for id which appears to be set to a random value:
Object
Configuration: Object
ciName: null
id: "ext-record-11"
longName: null
Can anyone see why the nested Configuration instance in my JSON isn't being saved? And should the nested Configuration instance in the JSON be added to the Configurations store automatically?
Sadly this just doesn't work. Appears to be a shortcoming of the framework. You can't load and save associated (aggregated/nested) objects. You need to flatten out your structure and then associate objects in code yourself.
So for example... your JSON would then be...
{
"success": true,
"changes": [
{
"id": 1,
"changeId": "XYZ19178263",
"briefDescription": "Loaded from Proxy"
}
]
}
And a second JSON file/response as follows:
{
"success": true,
"configurations": [
{
"id": "3",
"longName": "999-99_-_windows_7_desktop.Computer.Windows",
"ciName": "999-99_-_windows_7_desktop",
"change_id": 1
}
]
}
Tinashe's answer is not correct. You can certainly get nested models.
You have your associations backward with respect to your JSON, however.
"changes": [
{
"id": 1,
"changeId": "XYZ19178263",
"briefDescription": "Loaded from Proxy",
"configuration_id": 3,
"configurations": [
{
"id": "3",
"longName": "999-99_-_windows_7_desktop.Computer.Windows",
"ciName": "999-99_-_windows_7_desktop"
}
]
}
]
means a change hasMany configurations, but you are saying it belongsTo it. This is the correct JSON:
"changes": [
{
"id": 1,
"changeId": "XYZ19178263",
"briefDescription": "Loaded from Proxy",
"configuration_id": 3,
"configuration":
{
"id": "3",
"longName": "999-99_-_windows_7_desktop.Computer.Windows",
"ciName": "999-99_-_windows_7_desktop"
}
}
]
Either way, you need to set getterName (setterName if you like too) on the relationship:
associations: [{
type:'belongsTo',
model:'Changes.model.Configuration',
primaryKey: 'id',
foreignKey: 'configuration_id',
associationKey: 'configuration',
getterName:'getConfiguration'
}]
Then after the store loads, you can call myChangeModel.getConfiguration();

jstree types plugin

This is my current JSON input for my jstree (using jstree-grid for the data):
{
data: "StrukturNavn",
attr: {id:"1", class:"structureNode", contentItemId:"123", rel:"root"},
children: [
{
data: "Seksjon 1",
attr: {id:"2", class:"structureNode", contentItemId:"231"}
}
]
}
And this is the relevant jstree code:
types: {
root : {delete_node: false}
},
plugins: ["json_data", "crrm", "ui", "themes", "grid", "types"],
But, for some reason, the root node is still deletable, what am I doing wrong?
Thanks :-)
I found the problem, I was simply not configuring the plugin correctly.
types: {
root : {delete_node: false}
},
should be
types: {
valid_children: ["root"],
types: {
root : {delete_node: false}
}
},