Related
I am following the tutorial: https://youtu.be/WZWCzsB1xUE
and the error state occurs at: 50:00
I have checked multiple times using "", yet problem remains unsolved.
Help would be appreciated as I am new to JS and ReactJS.
The contract is deployed on polygon testnet with Moralis server support
and using the Ethereum bolierplate
Can anybody point out if any silly mistake?
const [contractABI, setContractABI] = useState('[
{
"inputs": [
{
"internalType": "address",
"name": "nftContract",
"type": "address"
},
{
"internalType": "uint256",
"name": "tokenId",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "price",
"type": "uint256"
}
],
"name": "createMarketItem",
"outputs": [],
"stateMutability": "payable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "nftContract",
"type": "address"
},
{
"internalType": "uint256",
"name": "itemId",
"type": "uint256"
}
],
"name": "createMarketSale",
"outputs": [],
"stateMutability": "payable",
"type": "function"
},
{
"inputs": [],
"stateMutability": "nonpayable",
"type": "constructor"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "uint256",
"name": "itemId",
"type": "uint256"
},
{
"indexed": true,
"internalType": "address",
"name": "nftContract",
"type": "address"
},
{
"indexed": true,
"internalType": "uint256",
"name": "tokenId",
"type": "uint256"
},
{
"indexed": false,
"internalType": "address",
"name": "seller",
"type": "address"
},
{
"indexed": false,
"internalType": "address",
"name": "owner",
"type": "address"
},
{
"indexed": false,
"internalType": "uint256",
"name": "price",
"type": "uint256"
},
{
"indexed": false,
"internalType": "bool",
"name": "sold",
"type": "bool"
}
],
"name": "MarketItemCreated",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "uint256",
"name": "itemId",
"type": "uint256"
},
{
"indexed": false,
"internalType": "address",
"name": "owner",
"type": "address"
}
],
"name": "MarketItemSold",
"type": "event"
},
{
"inputs": [],
"name": "fetchMarketItems",
"outputs": [
{
"components": [
{
"internalType": "uint256",
"name": "itemId",
"type": "uint256"
},
{
"internalType": "address",
"name": "nftContract",
"type": "address"
},
{
"internalType": "uint256",
"name": "tokenId",
"type": "uint256"
},
{
"internalType": "address payable",
"name": "seller",
"type": "address"
},
{
"internalType": "address payable",
"name": "owner",
"type": "address"
},
{
"internalType": "uint256",
"name": "price",
"type": "uint256"
},
{
"internalType": "bool",
"name": "sold",
"type": "bool"
}
],
"internalType": "struct marketPlaceBoilerPlate.MarketItem[]",
"name": "",
"type": "tuple[]"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "owner",
"outputs": [
{
"internalType": "address",
"name": "",
"type": "address"
}
],
"stateMutability": "view",
"type": "function"
}
]');
const [marketAddress, setMarketAddress] = useState();
Error:
"resource": "/c:/ethereum-nft-marketplace-boilerplate/src/providers/MoralisDappProvider/MoralisDappProvider.js",
"owner": "eslint",
"severity": 8,
"message": "Parsing error: Unterminated string constant.\n\n 7 | const [walletAddress, setWalletAddress] = useState();\n 8 | const [chainId, setChainId] = useState(); \n> 9 | const [contractABI, setContractABI] = useState('[\n | ^\n 10 | {\n 11 | \"inputs\": [\n 12 | {",
"source": "eslint",
"startLineNumber": 9,
"startColumn": 50,
"endLineNumber": 9,
"endColumn": 50
},{
"resource": "/c:ethereum-nft-marketplace-boilerplate/src/providers/MoralisDappProvider/MoralisDappProvider.js",
"owner": "typescript",
"code": "1002",
"severity": 8,
"message": "Unterminated string literal.",
"source": "ts",
"startLineNumber": 9,
"startColumn": 52,
"endLineNumber": 9,
"endColumn": 52
},{
"resource": "/c:src/providers/MoralisDappProvider/MoralisDappProvider.js",
"owner": "typescript",
"code": "1005",
"severity": 8,
"message": "',' expected.",
"source": "ts",
"startLineNumber": 10,
"startColumn": 5,
"endLineNumber": 10,
"endColumn": 6
},{
"resource": "/c:/src/providers/MoralisDappProvider/MoralisDappProvider.js",
"owner": "typescript",
"code": "1005",
"severity": 8,
"message": "',' expected.",
"source": "ts",
"startLineNumber": 187,
"startColumn": 3,
"endLineNumber": 187,
"endColumn": 4
},{
"resource": "/c:src/providers/MoralisDappProvider/MoralisDappProvider.js",
"owner": "typescript",
"code": "1002",
"severity": 8,
"message": "Unterminated string literal.",
"source": "ts",
"startLineNumber": 187,
"startColumn": 33,
"endLineNumber": 187,
"endColumn": 33
}]```
[error image here][2]
[1]: https://github.com/ethereum-boilerplate/ethereum-boilerplate
[2]: https://i.stack.imgur.com/EgnwO.png
I am trying to restructure my fetched JSON data. A sample of my JSON response is here:
{
"sys": {
"type": "Array"
},
"total": 3,
"skip": 0,
"limit": 100,
"items": [
{
"sys": {
"space": {
"sys": {
"type": "Link",
"linkType": "Space",
"id": "SPACEID"
}
},
"id": "ENTRYID1",
"type": "Entry",
"createdAt": "2019-05-22T15:03:51.318Z",
"updatedAt": "2019-05-22T15:03:51.318Z",
"environment": {
"sys": {
"id": "master",
"type": "Link",
"linkType": "Environment"
}
},
"revision": 1,
"contentType": {
"sys": {
"type": "Link",
"linkType": "ContentType",
"id": "contentTypeID"
}
},
"locale": "en-US"
},
"fields": {
"firstName": "John",
"lastName": "Doe",
"title": "John's Title",
"building": "John's Building",
"roomNumber": 1234,
"picture": {
"sys": {
"type": "Link",
"linkType": "Asset",
"id": "ASSETID1"
}
}
}
},
{
"sys": {
"space": {
"sys": {
"type": "Link",
"linkType": "Space",
"id": "SPACEID"
}
},
"id": "ENTRYID2",
"type": "Entry",
"createdAt": "2019-05-22T15:03:51.318Z",
"updatedAt": "2019-05-22T15:03:51.318Z",
"environment": {
"sys": {
"id": "master",
"type": "Link",
"linkType": "Environment"
}
},
"revision": 1,
"contentType": {
"sys": {
"type": "Link",
"linkType": "ContentType",
"id": "contentTypeID"
}
},
"locale": "en-US"
},
"fields": {
"firstName": "Jane",
"lastName": "Doe",
"title": "Jane's title",
"building": "Jane's Building",
"roomNumber": 4321,
"picture": {
"sys": {
"type": "Link",
"linkType": "Asset",
"id": "ASSETID2"
}
}
}
},
{
"sys": {
"space": {
"sys": {
"type": "Link",
"linkType": "Space",
"id": "SPACEID"
}
},
"id": "ENTRYID3",
"type": "Entry",
"createdAt": "2019-05-22T15:03:51.318Z",
"updatedAt": "2019-05-22T15:03:51.318Z",
"environment": {
"sys": {
"id": "master",
"type": "Link",
"linkType": "Environment"
}
},
"revision": 1,
"contentType": {
"sys": {
"type": "Link",
"linkType": "ContentType",
"id": "contentTypeID"
}
},
"locale": "en-US"
},
"fields": {
"firstName": "Bob",
"lastName": "Doe",
"title": "Bob's title",
"building": "Bob's Building",
"roomNumber": 1111
}
}
],
"includes": {
"Asset": [
{
"sys": {
"space": {
"sys": {
"type": "Link",
"linkType": "Space",
"id": "SPACEID"
}
},
"id": "ASSETID1",
"type": "Asset",
"createdAt": "2019-05-22T15:03:41.369Z",
"updatedAt": "2019-05-22T15:03:41.370Z",
"environment": {
"sys": {
"id": "master",
"type": "Link",
"linkType": "Environment"
}
},
"revision": 1,
"locale": "en-US"
},
"fields": {
"title": "johndoe",
"file": {
"url": "//some.image.link/SPACEID/ASSETID1/1234567890/JohnDoe.jpg",
"details": {
"size": 16681,
"image": {
"width": 244,
"height": 352
}
},
"fileName": "JohnDoe.jpg",
"contentType": "image/jpeg"
}
}
},
{
"sys": {
"space": {
"sys": {
"type": "Link",
"linkType": "Space",
"id": "SPACEID"
}
},
"id": "ASSETID2",
"type": "Asset",
"createdAt": "2019-05-22T15:03:41.369Z",
"updatedAt": "2019-05-22T15:03:41.370Z",
"environment": {
"sys": {
"id": "master",
"type": "Link",
"linkType": "Environment"
}
},
"revision": 1,
"locale": "en-US"
},
"fields": {
"title": "janedoe",
"file": {
"url": "//some.image.link/SPACEID/ASSETID2/0987654321/JaneDoe.jpg",
"details": {
"size": 16681,
"image": {
"width": 244,
"height": 352
}
},
"fileName": "JaneDoe.jpg",
"contentType": "image/jpeg"
}
}
}
]
}
};
I want to take the fields key and properties from the includes > Asset and put them into the correct item > fields > picture that has the matching ASSETID. Then the includes would just be removed from the whole thing because it is no longer needed.
This is what the final result that I want:
{
"sys": {
"type": "Array"
},
"total": 3,
"skip": 0,
"limit": 100,
"items": [
{
"sys": {
"space": {
"sys": {
"type": "Link",
"linkType": "Space",
"id": "SPACEID"
}
},
"id": "ENTRYID1",
"type": "Entry",
"createdAt": "2019-05-22T15:03:51.318Z",
"updatedAt": "2019-05-22T15:03:51.318Z",
"environment": {
"sys": {
"id": "master",
"type": "Link",
"linkType": "Environment"
}
},
"revision": 1,
"contentType": {
"sys": {
"type": "Link",
"linkType": "ContentType",
"id": "contentTypeID"
}
},
"locale": "en-US"
},
"fields": {
"firstName": "John",
"lastName": "Doe",
"title": "John's Title",
"building": "John's Building",
"roomNumber": 1234,
"picture": {
"sys": {
"type": "Link",
"linkType": "Asset",
"id": "ASSETID1"
},
"fields": {
"title": "johndoe",
"file": {
"url": "//some.image.link/SPACEID/ASSETID1/1234567890/JohnDoe.jpg",
"details": {
"size": 16681,
"image": {
"width": 244,
"height": 352
}
},
"fileName": "JohnDoe.jpg",
"contentType": "image/jpeg"
}
}
}
}
},
{
"sys": {
"space": {
"sys": {
"type": "Link",
"linkType": "Space",
"id": "SPACEID"
}
},
"id": "ENTRYID2",
"type": "Entry",
"createdAt": "2019-05-22T15:03:51.318Z",
"updatedAt": "2019-05-22T15:03:51.318Z",
"environment": {
"sys": {
"id": "master",
"type": "Link",
"linkType": "Environment"
}
},
"revision": 1,
"contentType": {
"sys": {
"type": "Link",
"linkType": "ContentType",
"id": "contentTypeID"
}
},
"locale": "en-US"
},
"fields": {
"firstName": "Jane",
"lastName": "Doe",
"title": "Jane's title",
"building": "Jane's Building",
"roomNumber": 4321,
"picture": {
"sys": {
"type": "Link",
"linkType": "Asset",
"id": "ASSETID2"
},
"fields": {
"title": "janedoe",
"file": {
"url": "//some.image.link/SPACEID/ASSETID2/0987654321/JaneDoe.jpg",
"details": {
"size": 16681,
"image": {
"width": 244,
"height": 352
}
},
"fileName": "JaneDoe.jpg",
"contentType": "image/jpeg"
}
}
}
}
},
{
"sys": {
"space": {
"sys": {
"type": "Link",
"linkType": "Space",
"id": "SPACEID"
}
},
"id": "ENTRYID3",
"type": "Entry",
"createdAt": "2019-05-22T15:03:51.318Z",
"updatedAt": "2019-05-22T15:03:51.318Z",
"environment": {
"sys": {
"id": "master",
"type": "Link",
"linkType": "Environment"
}
},
"revision": 1,
"contentType": {
"sys": {
"type": "Link",
"linkType": "ContentType",
"id": "contentTypeID"
}
},
"locale": "en-US"
},
"fields": {
"firstName": "Bob",
"lastName": "Doe",
"title": "Bob's title",
"building": "Bob's Building",
"roomNumber": 1111
}
}
]
}
What I currently have is a function that iterates over each item in items and tries to see if the Assets array includes the ASSETID from the item.
const items = data.items.slice(0);
const assets = data.includes.Asset.slice(0);
items.forEach(item => {
if (item.fields.picture !== undefined) {
const {
id
} = item.fields.picture.sys;
const mappedAssets = assets.map(asset => asset.sys.id);
if (mappedAssets.includes(id)) {
console.log('This id matched:', id)
}
}
})
const data = {
"sys": {
"type": "Array"
},
"total": 3,
"skip": 0,
"limit": 100,
"items": [{
"sys": {
"space": {
"sys": {
"type": "Link",
"linkType": "Space",
"id": "SPACEID"
}
},
"id": "ENTRYID1",
"type": "Entry",
"createdAt": "2019-05-22T15:03:51.318Z",
"updatedAt": "2019-05-22T15:03:51.318Z",
"environment": {
"sys": {
"id": "master",
"type": "Link",
"linkType": "Environment"
}
},
"revision": 1,
"contentType": {
"sys": {
"type": "Link",
"linkType": "ContentType",
"id": "contentTypeID"
}
},
"locale": "en-US"
},
"fields": {
"firstName": "John",
"lastName": "Doe",
"title": "John's Title",
"building": "John's Building",
"roomNumber": 1234,
"picture": {
"sys": {
"type": "Link",
"linkType": "Asset",
"id": "ASSETID1"
}
}
}
},
{
"sys": {
"space": {
"sys": {
"type": "Link",
"linkType": "Space",
"id": "SPACEID"
}
},
"id": "ENTRYID2",
"type": "Entry",
"createdAt": "2019-05-22T15:03:51.318Z",
"updatedAt": "2019-05-22T15:03:51.318Z",
"environment": {
"sys": {
"id": "master",
"type": "Link",
"linkType": "Environment"
}
},
"revision": 1,
"contentType": {
"sys": {
"type": "Link",
"linkType": "ContentType",
"id": "contentTypeID"
}
},
"locale": "en-US"
},
"fields": {
"firstName": "Jane",
"lastName": "Doe",
"title": "Jane's title",
"building": "Jane's Building",
"roomNumber": 4321,
"picture": {
"sys": {
"type": "Link",
"linkType": "Asset",
"id": "ASSETID2"
}
}
}
},
{
"sys": {
"space": {
"sys": {
"type": "Link",
"linkType": "Space",
"id": "SPACEID"
}
},
"id": "ENTRYID3",
"type": "Entry",
"createdAt": "2019-05-22T15:03:51.318Z",
"updatedAt": "2019-05-22T15:03:51.318Z",
"environment": {
"sys": {
"id": "master",
"type": "Link",
"linkType": "Environment"
}
},
"revision": 1,
"contentType": {
"sys": {
"type": "Link",
"linkType": "ContentType",
"id": "contentTypeID"
}
},
"locale": "en-US"
},
"fields": {
"firstName": "Bob",
"lastName": "Doe",
"title": "Bob's title",
"building": "Bob's Building",
"roomNumber": 1111
}
}
],
"includes": {
"Asset": [{
"sys": {
"space": {
"sys": {
"type": "Link",
"linkType": "Space",
"id": "SPACEID"
}
},
"id": "ASSETID1",
"type": "Asset",
"createdAt": "2019-05-22T15:03:41.369Z",
"updatedAt": "2019-05-22T15:03:41.370Z",
"environment": {
"sys": {
"id": "master",
"type": "Link",
"linkType": "Environment"
}
},
"revision": 1,
"locale": "en-US"
},
"fields": {
"title": "johndoe",
"file": {
"url": "//some.image.link/SPACEID/ASSETID1/1234567890/JohnDoe.jpg",
"details": {
"size": 16681,
"image": {
"width": 244,
"height": 352
}
},
"fileName": "JohnDoe.jpg",
"contentType": "image/jpeg"
}
}
},
{
"sys": {
"space": {
"sys": {
"type": "Link",
"linkType": "Space",
"id": "SPACEID"
}
},
"id": "ASSETID2",
"type": "Asset",
"createdAt": "2019-05-22T15:03:41.369Z",
"updatedAt": "2019-05-22T15:03:41.370Z",
"environment": {
"sys": {
"id": "master",
"type": "Link",
"linkType": "Environment"
}
},
"revision": 1,
"locale": "en-US"
},
"fields": {
"title": "janedoe",
"file": {
"url": "//some.image.link/SPACEID/ASSETID2/0987654321/JaneDoe.jpg",
"details": {
"size": 16681,
"image": {
"width": 244,
"height": 352
}
},
"fileName": "JaneDoe.jpg",
"contentType": "image/jpeg"
}
}
}
]
}
};
const items = data.items.slice(0);
const assets = data.includes.Asset.slice(0);
items.forEach(item => {
if (item.fields.picture !== undefined) {
const {
id
} = item.fields.picture.sys;
const mappedAssets = assets.map(asset => asset.sys.id);
if (mappedAssets.includes(id)) {
console.log('This id matched:', id)
}
}
})
I'm not sure how to spread the properties in the way that I want to. Am I going in the right direction or is my thinking off?
The solution ended up being:
data.items.forEach(item => {
if (item.fields.picture !== undefined) {
const { id } = item.fields.picture.sys;
const foundAssets = data.includes.Asset.find(asset => asset.sys.id === id);
item.fields.picture = { ...item.fields.picture, fields: {...foundAssets.fields} };
}
});
delete data.includes;
I guess could simply use forEach, do the matching, spread what is required and delete what is not required. Something like this
const input = {
"sys": {
"type": "Array"
},
"total": 3,
"skip": 0,
"limit": 100,
"items": [{
"sys": {
"space": {
"sys": {
"type": "Link",
"linkType": "Space",
"id": "SPACEID"
}
},
"id": "ENTRYID1",
"type": "Entry",
"createdAt": "2019-05-22T15:03:51.318Z",
"updatedAt": "2019-05-22T15:03:51.318Z",
"environment": {
"sys": {
"id": "master",
"type": "Link",
"linkType": "Environment"
}
},
"revision": 1,
"contentType": {
"sys": {
"type": "Link",
"linkType": "ContentType",
"id": "contentTypeID"
}
},
"locale": "en-US"
},
"fields": {
"firstName": "John",
"lastName": "Doe",
"title": "John's Title",
"building": "John's Building",
"roomNumber": 1234,
"picture": {
"sys": {
"type": "Link",
"linkType": "Asset",
"id": "ASSETID1"
}
}
}
},
{
"sys": {
"space": {
"sys": {
"type": "Link",
"linkType": "Space",
"id": "SPACEID"
}
},
"id": "ENTRYID2",
"type": "Entry",
"createdAt": "2019-05-22T15:03:51.318Z",
"updatedAt": "2019-05-22T15:03:51.318Z",
"environment": {
"sys": {
"id": "master",
"type": "Link",
"linkType": "Environment"
}
},
"revision": 1,
"contentType": {
"sys": {
"type": "Link",
"linkType": "ContentType",
"id": "contentTypeID"
}
},
"locale": "en-US"
},
"fields": {
"firstName": "Jane",
"lastName": "Doe",
"title": "Jane's title",
"building": "Jane's Building",
"roomNumber": 4321,
"picture": {
"sys": {
"type": "Link",
"linkType": "Asset",
"id": "ASSETID2"
}
}
}
},
{
"sys": {
"space": {
"sys": {
"type": "Link",
"linkType": "Space",
"id": "SPACEID"
}
},
"id": "ENTRYID3",
"type": "Entry",
"createdAt": "2019-05-22T15:03:51.318Z",
"updatedAt": "2019-05-22T15:03:51.318Z",
"environment": {
"sys": {
"id": "master",
"type": "Link",
"linkType": "Environment"
}
},
"revision": 1,
"contentType": {
"sys": {
"type": "Link",
"linkType": "ContentType",
"id": "contentTypeID"
}
},
"locale": "en-US"
},
"fields": {
"firstName": "Bob",
"lastName": "Doe",
"title": "Bob's title",
"building": "Bob's Building",
"roomNumber": 1111
}
}
],
"includes": {
"Asset": [{
"sys": {
"space": {
"sys": {
"type": "Link",
"linkType": "Space",
"id": "SPACEID"
}
},
"id": "ASSETID1",
"type": "Asset",
"createdAt": "2019-05-22T15:03:41.369Z",
"updatedAt": "2019-05-22T15:03:41.370Z",
"environment": {
"sys": {
"id": "master",
"type": "Link",
"linkType": "Environment"
}
},
"revision": 1,
"locale": "en-US"
},
"fields": {
"title": "johndoe",
"file": {
"url": "//some.image.link/SPACEID/ASSETID1/1234567890/JohnDoe.jpg",
"details": {
"size": 16681,
"image": {
"width": 244,
"height": 352
}
},
"fileName": "JohnDoe.jpg",
"contentType": "image/jpeg"
}
}
},
{
"sys": {
"space": {
"sys": {
"type": "Link",
"linkType": "Space",
"id": "SPACEID"
}
},
"id": "ASSETID2",
"type": "Asset",
"createdAt": "2019-05-22T15:03:41.369Z",
"updatedAt": "2019-05-22T15:03:41.370Z",
"environment": {
"sys": {
"id": "master",
"type": "Link",
"linkType": "Environment"
}
},
"revision": 1,
"locale": "en-US"
},
"fields": {
"title": "janedoe",
"file": {
"url": "//some.image.link/SPACEID/ASSETID2/0987654321/JaneDoe.jpg",
"details": {
"size": 16681,
"image": {
"width": 244,
"height": 352
}
},
"fileName": "JaneDoe.jpg",
"contentType": "image/jpeg"
}
}
}
]
}
};
input.items.forEach(i => {
const foundAsset = input.includes.Asset.find(j => j.id === i.id);
i.fields.picture = { ...i.fields.picture,
...foundAsset.fields
};
});
delete input.includes;
console.log(input);
I am trying to iterate the json below, I have got this as an output of an astify of sql parser. Now i want to validate the column type with a DB schema that i have.
{
"with": null,
"type": "select",
"options": null,
"distinct": null,
"columns": "*",
"from": [
{
"db": null,
"table": "TABLE_1",
"as": null
}
],
"where": {
"type": "binary_expr",
"operator": "AND",
"left": {
"type": "binary_expr",
"operator": "AND",
"left": {
"type": "binary_expr",
"operator": "=",
"left": {
"type": "column_ref",
"table": null,
"column": "COLUMN_1"
},
"right": {
"type": "string",
"value": "NORM"
}
},
"right": {
"type": "binary_expr",
"operator": "IN",
"left": {
"type": "column_ref",
"table": null,
"column": "COLUMN_2"
},
"right": {
"type": "expr_list",
"value": [
{
"type": "string",
"value": "11"
},
{
"type": "string",
"value": "12"
},
{
"type": "string",
"value": "13"
},
{
"type": "string",
"value": "14"
},
{
"type": "string",
"value": "15"
},
{
"type": "string",
"value": "16"
},
{
"type": "string",
"value": "17"
},
{
"type": "string",
"value": "18"
},
{
"type": "string",
"value": "42"
},
{
"type": "string",
"value": "43"
},
{
"type": "string",
"value": "44"
},
{
"type": "string",
"value": "45"
},
{
"type": "string",
"value": "101"
},
{
"type": "string",
"value": "102"
},
{
"type": "string",
"value": "103"
},
{
"type": "string",
"value": "104"
},
{
"type": "string",
"value": "128"
},
{
"type": "string",
"value": "129"
},
{
"type": "string",
"value": "130"
},
{
"type": "string",
"value": "131"
},
{
"type": "string",
"value": "159"
}
]
}
}
},
"right": {
"type": "binary_expr",
"operator": "OR",
"left": {
"type": "binary_expr",
"operator": "IS NOT",
"left": {
"type": "column_ref",
"table": null,
"column": "COLUMN_3"
},
"right": {
"type": "null",
"value": null
}
},
"right": {
"type": "binary_expr",
"operator": "=",
"left": {
"type": "column_ref",
"table": null,
"column": "COLUMN_4"
},
"right": {
"type": "number",
"value": 1
}
},
"parentheses": true
}
},
"groupby": null,
"having": null,
"orderby": null,
"limit": null
}
Can you please help me get an output like which will help me identify the column and its type whether its string or not. Basically i need to validate the query whether its mapped against the correct data type while using a var condition.
Output like:
map = <key,value>
<Column_1, String>
<Column_2, String>
<Column_3, INT>
etc.
#Suzy, please check below code for your reference:
var arr = {
"with": null,
"type": "select",
"options": null,
"distinct": null,
"columns": "*",
"from": [
{
"db": null,
"table": "TABLE_1",
"as": null
}
],
"where": {
"type": "binary_expr",
"operator": "AND",
"left": {
"type": "binary_expr",
"operator": "AND",
"left": {
"type": "binary_expr",
"operator": "=",
"left": {
"type": "column_ref",
"table": null,
"column": "COLUMN_1"
},
"right": {
"type": "string",
"value": "NORM"
}
},
"right": {
"type": "binary_expr",
"operator": "IN",
"left": {
"type": "column_ref",
"table": null,
"column": "COLUMN_2"
},
"right": {
"type": "expr_list",
"value": [
{
"type": "string",
"value": "11"
},
{
"type": "string",
"value": "12"
},
{
"type": "string",
"value": "13"
},
{
"type": "string",
"value": "14"
},
{
"type": "string",
"value": "15"
},
{
"type": "string",
"value": "16"
},
{
"type": "string",
"value": "17"
},
{
"type": "string",
"value": "18"
},
{
"type": "string",
"value": "42"
},
{
"type": "string",
"value": "43"
},
{
"type": "string",
"value": "44"
},
{
"type": "string",
"value": "45"
},
{
"type": "string",
"value": "101"
},
{
"type": "string",
"value": "102"
},
{
"type": "string",
"value": "103"
},
{
"type": "string",
"value": "104"
},
{
"type": "string",
"value": "128"
},
{
"type": "string",
"value": "129"
},
{
"type": "string",
"value": "130"
},
{
"type": "string",
"value": "131"
},
{
"type": "string",
"value": "159"
}
]
}
}
},
"right": {
"type": "binary_expr",
"operator": "OR",
"left": {
"type": "binary_expr",
"operator": "IS NOT",
"left": {
"type": "column_ref",
"table": null,
"column": "COLUMN_3"
},
"right": {
"type": "null",
"value": null
}
},
"right": {
"type": "binary_expr",
"operator": "=",
"left": {
"type": "column_ref",
"table": null,
"column": "COLUMN_4"
},
"right": {
"type": "number",
"value": 1
}
},
"parentheses": true
}
},
"groupby": null,
"having": null,
"orderby": null,
"limit": null
};
function findProp(obj, key, out) {
var i,
proto = Object.prototype,
ts = proto.toString,
hasOwn = proto.hasOwnProperty.bind(obj);
if ('[object Array]' !== ts.call(out)) out = [];
for (i in obj) {
if (hasOwn(i)) {
if (i === key) {
out.push(obj[i]);
} else if ('[object Array]' === ts.call(obj[i]) || '[object Object]' === ts.call(obj[i])) {
findProp(obj[i], key, out);
}
}
}
return out;
}
console.log(findProp(arr, "column"));
Parse the JSON into an object using JSON.parse(), then iterate through them while checking if the value is JSON or not.
Check if the value is JSON by using catch on JSON.parse()
To get the object keys for iteration, Use var keys = Object.keys();
I have an array on objects like this,
[
{
"link": "link1",
"model": "model1",
"role": "role1",
"access": "true"
},
{
"link": "link1",
"model": "model1",
"role": "role2",
"access": "true"
},
{
"link": "link1",
"model": "model1",
"role": "role3",
"access": "true"
},
{
"link": "link1",
"model": "model1",
"role": "role4",
"access": "true"
},
{
"link": "link1",
"model": "model2",
"role": "role1",
"access": "false"
},
{
"link": "link1",
"model": "model2",
"role": "role2",
"access": "false"
},
{
"link": "link1",
"model": "model2",
"role": "role3",
"access": "false"
},
{
"link": "link1",
"model": "model2",
"role": "role4",
"access": "false"
},
{
"link": "link2",
"model": "model1",
"role": "role1",
"access": "false"
},
{
"link": "link2",
"model": "model1",
"role": "role2",
"access": "true"
},
{
"link": "link2",
"model": "model1",
"role": "role3",
"access": "false"
},
{
"link": "link2",
"model": "model1",
"role": "role4",
"access": "true"
},
{
"link": "link2",
"model": "model2",
"role": "role1",
"access": "false"
},
{
"link": "link2",
"model": "model2",
"role": "role2",
"access": "true"
},
{
"link": "link2",
"model": "model2",
"role": "role3",
"access": "false"
},
{
"link": "link2",
"model": "model2",
"role": "role4",
"access": "true"
}
]
With respect to this question, the input was a CSV sample with very less number of rows. In real I have a large CSV file and I found a way to import it and convert to JSON using d3.js, but the problem is I'm unable to convert it to the desired format. I tried using the data.forEach and the output was really weird and I couldn't understand why I get model2 alone in both the links.
Code:
d3.csv('data.csv', function(data) {
var newData = {};
data.forEach(function(e, i) {
newData[e.link] = {};
newData[e.link][e.model] = {};
})
d3.select('main').append('pre')
.text(JSON.stringify(newData, null, ' '));
});
Output:
{
"link1": {
"model2": {}
},
"link2": {
"model2": {}
}
}
Desired Output:
"link1": {
"model1": {
"role1": true,
"role2": true,
"role3": true,
"role4": true,
},
"model2": {
"role1": false,
"role2": false,
"role3": false,
"role4": false,
}
},
"link2": {
"model1": {
"role1": false,
"role2": true,
"role3": false,
"role4": true,
},
"model2": {
"role1": false,
"role2": true,
"role3": false,
"role4": true,
}
}
Any help will be much appreciated. Thanks in advance.
In your forEach, you're not assigning the role and access pair on each iteration, and you're always emptying each section.
Change your forEach with this...
data.forEach(function(e, i) {
// Check if this "link" already exists exists, if not, create it.
if (!newData[e.link])
newData[e.link] = {};
// Check if this "model" already exists inside of this "link", if not, create it.
if (!newData[e.link][e.model])
newData[e.link][e.model] = {};
newData[e.link][e.model][e.role] = e.access;
});
I hope it helps
can you please tell me how to validate form which is prepared dynamically using dfom plugin?
I used jquery validation but it don't work on chrome.
can you please tell me why dform with jquery validation not working in chrome ?.It is working fine in firefox but not with in chrome why ?? Here is http://jsfiddle.net/8bweG/13/?
$("#sform").dform(
{
"elements": [
{
"html": [
{
"html": [
{
"type": "number",
"id": "totalRetryCount",
"name": "totalRetryCount",
"required": false,
"value": 0,
"tabindex": 1,
"validate": {
"messages": {
"number": "Naveen Kanjush hain"
}
}
}
],
"type": "fieldset",
"caption": "Total Retry Count"
},
{
"html": [
{
"type": "number",
"id": "totalRepeatCount",
"name": "totalRepeatCount",
"required": false,
"value": 0,
"tabindex": 2,
"validate": {
"messages": {
"number": "Naveen Kanjush hain"
}
}
}
],
"type": "fieldset",
"caption": "Total Repeat Count"
},
{
"html": [
{
"type": "select",
"options": {
"true": "true",
"false": "false"
},
"id": "summaryReportRequired",
"name": "summaryReportRequired",
"required": false,
"value": "true",
"tabindex": 3,
}
],
"type": "fieldset",
"caption": "Summary Report Required"
},
{
"html": [
{
"type": "select",
"options": {
"ALWAYS": "ALWAYS",
"ON_SUCCESS": "ON_SUCCESS"
},
"id": "postConditionExecution",
"name": "postConditionExecution",
"required": false,
"value": "ON_SUCCESS",
"tabindex": 4,
}
],
"type": "fieldset",
"caption": "Post Condition Execution"
}
],
"type": "div",
"class": "inputDiv",
"caption": "<h3>Configuration Parameters</h3>"
}
],
"id": "testSuiteConfigurationform",
"name": "testSuiteConfigurationform",
"method": "post"
}
);
Problem is that
First field of type ="number".User only type number in that .If user type string or character and move to another field it gives error.if you check on firefox this fiddle it is giving validation error .But on chrome and safari same functionality not work.
please follow these steps
on first field write "abc" then click below or next field.it gives error front of first field.it is display on firefox but not on chrome
It is working but you are missing the Submit button.
This is the correct version: http://jsfiddle.net/8bweG/22/
$("#sform").dform({
"action" : "http://dgg.gg",
"method" : "get",
"elements": [
{
"html": [
{
"html": [
{
"type": "number",
"id": "totalRetryCount",
"name": "totalRetryCount",
"required": false,
"value": 0,
"tabindex": 1,
"validate": {
"messages": {
"number": "Naveen Kanjush hain"
}
}
}
],
"type": "fieldset",
"caption": "Total Retry Count"
},
{
"html": [
{
"type": "number",
"id": "totalRepeatCount",
"name": "totalRepeatCount",
"required": false,
"value": 0,
"tabindex": 2,
"validate": {
"messages": {
"number": "Naveen Kanjush hain"
}
}
}
],
"type": "fieldset",
"caption": "Total Repeat Count"
},
{
"html": [
{
"type": "select",
"options": {
"true": "true",
"false": "false"
},
"id": "summaryReportRequired",
"name": "summaryReportRequired",
"required": false,
"value": "true",
"tabindex": 3,
}
],
"type": "fieldset",
"caption": "Summary Report Required"
},
{
"html": [
{
"type": "select",
"options": {
"ALWAYS": "ALWAYS",
"ON_SUCCESS": "ON_SUCCESS"
},
"id": "postConditionExecution",
"name": "postConditionExecution",
"required": false,
"value": "ON_SUCCESS",
"tabindex": 4,
}
],
"type": "fieldset",
"caption": "Post Condition Execution"
},
{
"html": [
{
"type" : "submit",
"value" : "Login"
}
],
"type": "fieldset",
"caption": "Go away"
}
],
"type": "div",
"class": "inputDiv",
"caption": "<h3>Configuration Parameters</h3>"
}
],
"id": "testSuiteConfigurationform",
"name": "testSuiteConfigurationform",
"method": "post"
}
);