I am working on Shopify and creating a d3 chart.
I tried to pass data from Shopify liquid to javascript.
The codes are as shown in the fiddle.
var temp_test = {
"events": []
};
var test = [
[
"<p>Everything is fresh. delicious and good.</p>",
undefined,
"12-23-2000",
"test",
"Start"
],
[
"<p>Everything is fresh. delicious and good.</p>",
undefined,
"12-23-2000",
"test",
"Start"
]
];
test.forEach(function(data) {
temp_test.events.push({
"title": data[0],
"image": data[1],
"date": data[2],
"content_1": data[3],
"content_2": data[4]
});
});
console.log('check for nan', temp_test);
https://jsfiddle.net/be83yk54/22/
(Strangely, the fiddle works just fine but Shopify does not and it is not Shopify problem).
console.log shows the data just fine, but when I click on the data to expand the object, the string is converted to number automatically and I got NaN instead of formatted date.
I think I got what happened. When I console.log the data the data correct. but the array object might be manipulated in d3 which caused the object data change and when expanding the object, the data is already changed.
Related
I am trying to parse a JSON from a GET request in JavaScript. I can run it through JSONLint and other tools and it's fine. If I copy/paste the exact result into the JavaScript I can access the values from the object as well.
Here is a shortened example of what the JSON structure looks like:
{
"odata.metadata": "EXAMPLE.com/$metadata#EXAMPLE.results",
"value": [{
"SerialNo_Company": "1",
"Calculated_AsBuilt": "AsBuilt",
"SerialNo_SerialNumber": "1",
"SerialNo_PartNum": "2A",
"JobHead_RevisionNum": "A",
"JobMtl_MtlSeq": 30,
"JobMtl_PartNum": "A123",
"JobMtl_RevisionNum": "A",
"JobMtl_Description": "KEY",
"JobMtl_QtyPer": "1.00000000",
"JobMtl_IUM": "EA",
"JobHead_JobNum": "13",
"JobMtl_IssuedQty": "2.00000000",
"JobMtl_RequiredQty": "2.00000000",
"RowIdent": "7e91b43a-897c-49b4-b68f-307b9ba74832"
}, {
"SerialNo_Company": "1",
"Calculated_AsBuilt": "AsBuilt",
"SerialNo_SerialNumber": "1",
"SerialNo_PartNum": "2A",
"JobHead_RevisionNum": "A",
"JobMtl_MtlSeq": 30,
"JobMtl_PartNum": "A123",
"JobMtl_RevisionNum": "A",
"JobMtl_Description": "KEY",
"JobMtl_QtyPer": "1.00000000",
"JobMtl_IUM": "EA",
"JobHead_JobNum": "14",
"JobMtl_IssuedQty": "2.00000000",
"JobMtl_RequiredQty": "2.00000000",
"RowIdent": "6251fbaf-36d0-4fcf-b65a-9dde70ffb13d"
}]
}
This format isn't entirely correct though, what it actually outputs has a structure like so:
"{\r\n \"odata.metadata\":\"EXAMPLE.com/$metadata#Example.results\",\"value\":[\r\n {\r\n \"SerialNo_Company\":\"1\",\"Calculated_AsBuilt\":\"AsBuilt\",\"SerialNo_SerialNumber\":\"1\",\"SerialNo_PartNum\":\"2A\",\"JobHead_RevisionNum\":\"A\",\"JobMtl_MtlSeq\":30,\"JobMtl_PartNum\":\"A13518N-28-KS\",\"JobMtl_RevisionNum\":\"A\",\"JobMtl_Description\":\"KEY\",\"JobMtl_QtyPer\":\"1.00000000\",\"JobMtl_IUM\":\"EA\",\"JobHead_JobNum\":\"13\",\"JobMtl_IssuedQty\":\"2.00000000\",\"JobMtl_RequiredQty\":\"2.00000000\",\"RowIdent\":\"111531e1-f4da-4d2d-b980-0074227a474e\"\r\n }\r\n ]\r\n}
When I try to query the first structure when hard coded in javascript, for example
object.value[0]
I get the first value and all is good.
The second one does not return a value with the same query. I removed the \r\n and it did not work. Also, JSON.stringify() puts in a bunch of backslashes in the structure and I don't know why.
Thanks for reading if you made it this far, please help.
The solution was to change the output in my node.js app. I was using res.json() and it was double-encoding the response. The output resulted in a javascript readable format from there. Thanks to Heretic Monkey for the answer
I have been trying to get resources using GET Request. You can see the actual Query here:
{ "success": true,"data": [
{
"ID": 123
"Code": "Test",
"Enabled": true,
"Flags": 0,
"Niveau": 0,
"SQL": "SELECT GEMEENTE.GM_ID,\r\n GEMEENTE.GM_POSTNUMMER,\r\n
GEMEENTE.GM_DEELGEMEENTE,\r\n GEMEENTE.GM_LAND_FK,\r\n
LAND.LN_ACTIEF\r\nFROM GEMEENTE\r\n LEFT JOIN LAND ON GEMEENTE.GM_LAND_FK =
LAND.LN_ID\r\nWhere GM_VERANDERDOP >=:ChangedOn",
"Parameters": [
"ChangedOn"
],
"QueryType": 0
}]}
However I need to pass an Argument to the "Parameters" property so i can actually see the response data. I see "ChangedOn" is a the argument array I need to set to a date. I tried this: http://localhost/A/B/server.fcgi/Query?Code=Test&Parameters="2015-07-05T22:16:18Z"
I am working with javascript and vue.
What are we looking at here? Is this the server code of your API. In that case, it looks like you need to supply a ChangedOn parameter. Also, your query string should be url encoded. Try calling...
http://localhost/A/B/server.fcgi/Query?Code=Test&ChangedOn=22015-07-05T22%3A16%3A18Z
I am currently working on a Javascript project where I have to parse tons of data. The project requires that I parse some JSON data and bring specific data into another array. Right now, I am using the JSON.stringify method and I can console.log all of the data that I need. The data looks something like this:
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
-85.3865810000125,
33.90171899971196
],
[
-85.38659500025622,
33.9017919996593
],
yes, this is not all that data-it is about 1200 pages long! I only pasted in the top segment of it. All I really need is how to get to the coordinates aspect of it and into an array. So what I am currently doing is this:
var work = JSON.stringify(response, null, 4)
console.log(work);
Which gives me the above response. However, I am not that familiar with JSON.stringify so if I do:
console.log(work.type);
Attempting to see what the value is for type, I get a response of undefined. Now if I try doing this:
var work = JSON.parse(response)
console.log(work);
I get a response of: VM296:1 Uncaught SyntaxError: Unexpected token o in JSON at position 1
Thus, I cannot get the data parsed which I want to get to which will be the coordinates data. Any help with using stringify would great help me. I have read a lot about how it turns the data into a string but have not really seen a lot about how to parse it. Do I have to use parse? Thank you for your help!
You dont' have to use stringify or parse for this. The response object you are receiving is already a javascript object so you can reference its properties normally
response.type; // => "FeatureCollection"
If you try and stringify the response it will lose its type property, for instance:
typeof "some string".someProperty; // => undefined
I am trying to learn promises in javascript and I came across this short video tutorial, there is a complete code from it on plunker.
What I wonder about in this code is in this line in promises.js file:
return $.get('tweets.json?id=' + profile.id);
First of all, why do we even need to pass any parameters there, since there is no profile.id field in tweets.json file, and when removing it I get the same results. If there was that field in tweets.json how would we achieve to get only tweets from the users that we pass with get request?
I have tried something like this but it doesn't work:
In promises.json I have changed the get request to:
return $.get('tweets.json?profile=' + profile.id);
And have added a new field to the tweets.json file, but only to the first tweet:
{
"id": 1,
"profile": 12302151,
"tweet": "OMG, worst day ever, my BF #BobbyBoo dumped me",
"usersMentioned": [
{
"id": 10,
"username": "BobbyBoo"
}
]
},
I was then expecting that I will only get that one tweet for that user, but I am still getting the whole list
I have an API in a Node.js which requires a JSON file, and on some requests maps in content from it. Simple enough, and works perfectly for the first request.
On subsequent requests one of the properties is left off. This is obviously a stripped down version of the code, but is all I'm doing to get the content.
Essentially my JSON looks like:
{"sections": {
"city": {
"title": "Seattle",
"info": "some info",
"tips": [ "tip 1", "tip 2" ]
}
}
}
and I require it:
var Content = require("content");
// some code
return req.status(200).json({ data: data, content: Content.sections.city });
The first request returns the whole content object. Every request after that returns only title and info, but not tips.
Edit -- I'm a dummy. In the //some code section, I work with the tips and eventually call delete on this version of the object. Apparently passing by reference still hasn't sunken in.