how to export complex json to excel with alasql - javascript

I have a json like this (there might be some syntax errors, I did extract and rewrite some private data)
"todo-items": [{
"id": 3511710,
"company-name": "company1",
"author" : "Jon Doe",
"tags": [{
"id": 10101,
"name": "2.Marketing Sales",
"color": "#f78234"
}],
"creation-date": 20160101
}, {
"id": 3511474,
"company-name": "company1",
"author" : "Jon Don",
"tags": [{
"id": 10101,
"name": "2.Marketing Sales",
"color": "#f78234"
}, {
"id": 10103,
"name": "4.Automotive",
"color": "#9b7cdb"
}],
"creation-date": 20160101
}, {
"id": 3511478,
"company-name": "company1",
"author" : "Peter Jon Doe",
"tags": [{
"id": 10101,
"name": "2.Marketing Sales",
"color": "#f78234"
}, {
"id": 9706,
"name": "3.sales",
"color": "#37ced0"
}, {
"id": 9562,
"name": "3.IT",
"color": "#37ced0"
}],
"creation-date": 20160101
}]
and I use alasql with xlsx.core libraries to export this to excel file.
For first I remove STATUS from original json (provided by teamwork API) and then I change JSON to javascript array
$.ajax({
type: "GET",
url: requrl,
headers: {"Authorization": "BASIC " + window.btoa(key + ":xxx")},
success: function(data) {
delete data.STATUS;
//alert(data);
//console.log(data);
var arr = $.map(data, function(el) { return el });
//console.log(arr);
alasql("SELECT * INTO XLSX('test.xlsx',{headers:true}) FROM ? ",[arr]);
},
error: function(response) {
alert(response);
}
});
this works nice and I can my json extract as excel file
but I have issues with tags objects in this json..when I change it to array and then save it in excel, all columns are ok except tags, where I see just [object][object]
Could you help me how to get these tags also into correct array? The best would be one tag = one column in excel
edit:
this is desired output - max # of tags in JSON will define number of columns tags (doesnt matter if it is tags tags tags or tags tags2 tags3)

Related

Merge a Product Config JSON with Generic Config JSON- Issue While Merging Arrays inside it

I have 2 different Json files
1: a Deaflut JSON file
2: A Product Based JSON file
I need to merge them together in such a way that if a feature in default is not in a product that needs to be added to product config from default one.
for this merge, I used "lodash.mergewith" https://www.npmjs.com/package/lodash.mergewith.
so this is now taking care of the merge, But the file contains multiple nested JSON arrays inside it.
to handle that there is an option to use a customizer method that can handle array merge as mentioned in the usage of lodash.mergewith. I need a customizer that can find the Label from Deaflut and compare it with the Product if the Product has the same Label value then replace the URL with the Product URL. else if the Label is not in Product config, then use it from default as it is.
Example
Default config.json:-links is an array of this json with path : object►login►options►sections►2►links
"links": [{
"url": "www.google.com",
"label": "Lable1"
},
{
"url": "www.google.com",
"label": "Label2"
},
{
"url": "www.google.com",
"label": "Label3"
},
{
"url": "www.google.com",
"label": "Label4"
}
]
Productconfig.json:- links is an array inside this of the path: object►login►options►sections►2►links
"links": [{
"url": "www.product1.com",
"label": "label1"
},
{
"url": "www.product2.com",
"label": "Label2"
}
]
** after merge mergedconfig.json "Links" need to be like this.**
"links": [{
"url": "www.product1.com",
"label": "Label1"
},
{
"url": "www.product2.com",
"label": "Label2"
},
{
"url": "www.google.com",
"label": "Label3"
},
{
"url": "www.google.com",
"label": "Label4"
}
]
The main concern is this Array is coming inside a JSON file inside some JSON objects
like eg if the Array is inside links[] it will be in a path like : object►login►options►sections►2►links[]. and this Links Array similarly present inside in some other paths eg: object►register►options►sections►2►links[]
So I need to figure out all the Array like this and for each of the Arrays, I need to perform this action.
Just use Array.map and Array.find:
let links= [
{
"url": "www.google.com",
"label": "Label1"
},
{
"url": "www.google.com",
"label": "Label2"
},
{
"url": "www.google.com",
"label": "Label3"
},
{
"url": "www.google.com",
"label": "Label4"
}
];
let plinks= [{
"url": "www.product1.com",
"label": "label1"
},
{
"url": "www.product2.com",
"label": "Label2"
}
];
let results = links.map(lnk=>{
plnk = plinks.find(pl=>pl.label.toLowerCase()===lnk.label.toLowerCase());
return plnk || lnk
});
console.log(results);
for clean access to nested JSON keys you can use ES like this:
let a = {
b: {
c: [1,2,3]
}
};
let {c} = a?.b;
console.log(c);

How to access json variable which is inside script instead of using $.getJSON to get external json?

Currently working with this highcharts code:
https://jsfiddle.net/umL154cd/
It works with external data source like shown below:
$.getJSON('https://www.highcharts.com/samples/data/jsonp.php?filename=activity.json&callback=?', function (activity) {
$.each(activity.datasets, function (i, dataset) { ///
But I'd like to truncate the data and put it inside the code itself by simply anouncing json.variable:
var datafromjson =
{
"xData": [0.001567,0.011765,0.022194],
"datasets": [{
"name": "Speed",
"data": [13.833,12.524,11.441,10.651],
"unit": "km/h",
"type": "line",
"valueDecimals": 1
}, {
"name": "Elevation",
"data": [26.857,27,27.111,27.2,27.272],
"unit": "m",
"type": "area",
"valueDecimals": 0
}, {
"name": "Heart rate",
"data": [101,98,103],
"unit": "bpm",
"type": "area",
"valueDecimals": 0
}]
};
How do I replace $.getJSON request so the script will take the turncated in-script data insted of accessing external data?
Thank you!

Converting JSON data to DOM using jQuery

I'm looking to convert each 'item'(from JSON) to appear inside a section(or div) with the image and its link appearing with the name, id, and price - how would this be done with jQuery. jQuery and JSON are below, I don't currently have any classes in the HTML other than 'placements-title' for the header and 'placements-items' for the section.
Current jQuery:
$.ajax({
type: 'GET',
url: 'pathtoJSONdata.json',
dataType: 'json',
success: function (data) {
$(".placements-title h2").append(data.placements[0].message);
$(".placements-items").append(data.placements[0].items[1].id);
}
});
Use a loop build the html as a string and append it to your desired dom element
var data = {
"placements": [{
"message": "If you like this, you might be into these",
"items": [{
"id": "029148",
"name": "Woodblock Play Suit",
"linkURL": "http://www.warehouse.co.uk/gb/just-arrived/all/woodblock-play-suit/029148.html",
"imageURL": "http://demandware.edgesuite.net/aaxe_prd/on/demandware.static/-/Sites-WAREHOUSE/default/dw0f93fcd4/images/hi-res/warehouse_02914899_2.jpg",
"price": "46.00"
},
{
"id": "0294526806",
"name": "Smock Dress",
"linkURL": "http://www.warehouse.co.uk/gb/just-arrived/all/smock-dress/0294526806.html",
"imageURL": "http://demandware.edgesuite.net/aaxe_prd/on/demandware.static/-/Sites-WAREHOUSE/default/dwc9d5ea05/images/hi-res/warehouse_02945268_5.jpg",
"price": "39.00"
},
{
"id": "0297180006",
"name": "Cami",
"linkURL": "http://www.warehouse.co.uk/gb/just-arrived/all/cami/0297180006.html",
"imageURL": "http://demandware.edgesuite.net/aaxe_prd/on/demandware.static/-/Sites-WAREHOUSE/default/dw4b954022/images/hi-res/warehouse_02971800_2.jpg",
"price": "9.00"
},
{
"id": "0298473606",
"name": "Asymmetric Wrap Cami Dress",
"imageURL": "http://demandware.edgesuite.net/aaxe_prd/on/demandware.static/-/Sites-WAREHOUSE/default/dw686fea84/images/hi-res/warehouse_02984736_2.jpg",
"price": "46.00"
},
{
"id": "0297155306",
"name": "Casual Stripe Tee",
"linkURL": "http://www.warehouse.co.uk/gb/just-arrived/all/casual-stripe-tee/0297155306.html",
"imageURL": "http://demandware.edgesuite.net/aaxe_prd/on/demandware.static/-/Sites-WAREHOUSE/default/dw4609af3e/images/hi-res/warehouse_02971553_2.jpg",
"price": "16.00"
}
]
}]
}
$.each(data.placements[0].items,function(i,v){
$('body').append('<img src="'+v.imageURL+'" height="50" width="50"><div class="placements-title"><a href="'+v.linkURL+'"><h2>'+v.name+'</h2>'+v.price+'</div>')
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Understanding JSON and displaying

I have the following ajax request...
Ajax
function initUpdate()
{
var id = $(this).attr('id').split('_')[1];
//grab id from link update_xxxx
//get all gallery information
$.ajax(
{
type: 'POST',
url: 'ajax/update',
data: {"id": id},
dataType: 'json',
success: function(json)
{
//query was successful now populate form
$("input[name='galleryName']").val(/*what goes here?*/);
}
});
}
That returns the following data
{
"selected_gallery": [
{
"id": "4004",
"name": "Album Proofer Sample Gallery",
"clientRef": "205",
"clientName": "Mike "
}
]
}
How would I access "name" to insert in the val()?
Thanks!
What have you tried? I would think you could get to it via:
json.selected_gallery[0].name
selected_gallery is a JavaScript array, so you would access the first item in the collection using [0] in order access the first item's properties.
UPDATE
You could access other items in the array if they existed:
{
"selected_gallery": [
{
"id": "4004",
"name": "Album Proofer Sample Gallery",
"clientRef": "205",
"clientName": "Mike "
},
{
"id": "5005",
"name": "blah",
"clientRef": "405",
"clientName": "Dave "
},
{
"id": "6006",
"name": "boo",
"clientRef": "605",
"clientName": "Doug"
}
]
}
To get the second item in the array, you'd reference it as:
json.selected_gallery[1].name (or id or clientRef or ...). You can get to the third item via json.selected_gallery[2].name.
Hope this helps.

parse facebook json javascript

I am using javascript api to get facebook comments.
i am getting the following json result , but how can i parse them to use on my page ?
{
"id": "1234567891_2823098717038_3160191",
"from": {
"name": "User",
"id": "1234567891"
},
"message": "comment only...",
"can_remove": true,
"created_time": "2012-05-05T07:43:11+0000"
},
{
"id": "1234567891_2823098717038_3160281",
"from": {
"name": "User",
"id": "1234567891"
},
"message": "just another comment...",
"can_remove": true,
"created_time": "2012-05-05T08:14:17+0000"
},
{
"id": "1234567891_2823098717038_3160336",
"from": {
"name": "user2",
"id": "56265654845454"
},
"message": "congratz dear :)",
"can_remove": true,
"created_time": "2012-05-05T08:29:05+0000"
}
],
"paging": {
"next": "http://link.dddd"
}
}
How can i loop through this and display the contents ?
jQuery solution is acceptable.
Thank you.
Use JQuery.parseJSON: http://api.jquery.com/jQuery.parseJSON/
assoc_data = jQuery.param(response); //where response is your json
$.ajax({
type: "POST",
url: "submit_fb_data.php",
data: assoc_data,
success: function(data) {
//etc
}
});
make sure you use a >=1.4 jquery version

Categories