I am trying to write my JSON data to excel but im only getting half of the data.
Here is my JSON:
[
{
"engineSN":"20",
"timeRun":"30",
"companyName":"toyota",
"hp":"110"
},
{
"engineSN":"35",
"timeRun":"380",
"companyName":"nissan",
"hp":"130"
},
{
"engineSN":"560",
"timeRun":"1870",
"companyName":"toyota",
"hp":"197"
},
{
"engineSN":"35",
"timeRun":"570",
"companyName":"audi",
"hp":"289"
},
{
"engineSN":"35",
"timeRun":"3800",
"companyName":"nissan",
"hp":"330"
},
]
the code that is written, I checked the documentation of xlsx and its the right code
let xlsx = require("xlsx");
const jsonData = require('./dataJsonOriginal.json');
let newWb = xlsx.utils.book_new();
let newWs = xlsx.utils.json_to_sheet(jsonData);
xlsx.utils.book_append_sheet(newWb, newWs, "Original data");
xlsx.writeFile(newWb, "dataExcel.xlsx");
the output returnes indexes 1 & 3
[
{
"engineSN":"35",
"timeRun":"380",
"companyName":"nissan",
"hp":"130"
},
{
"engineSN":"35",
"timeRun":"570",
"companyName":"audi",
"hp":"289"
}
]
These are the only indexes that are present in the excel file
Related
Hi guys i am trying to store my json data into my datatbase table column.
Here is my sample json code..this data only i am trying to store in db.
{
"template": {
"question_section": {
"section_data": [
{
"index": 1,
"section": "text section",
"properties": {
"font_size": "14",
"font_weight": "400",
"font_color": "#000000",
"row": 1
}
}
]
},
"solution_section": {
"section_data": []
}
}
}
here is my sample code:
function updateJson(field_id, from_where, section_counter, property_name, ques_section_counter, sol_section_counter)
{
if(from_where == 'question')
{
let prop_value = $('#'+field_id).val();
template_json.template.question_section.section_data[ques_section_counter-1].properties[property_name] = prop_value;
}
else if(from_where == 'solution')
{
let prop_value = $('#'+field_id).val();
template_json.template.solution_section.section_data[sol_section_counter-1].properties[property_name] = prop_value;
}
$('#code_json').html(JSON.stringify(template_json, undefined, 2));
}
This is one sample function that this is how i am getting json code and all..that above json wwhole code i would like to store.
Here is my form code:
$template->template_json = $request->input('');//this is the column
Can anyone help me on how can i store.
you have to save it as text in database like this:
$template->template_json = json_encode($request->input(''));
and then when you want to use it convert it to object before like this:
$template->template_json = json_decode($template->template_json);
0
I have the following assessment which is to Create Array of Favorite Food items object in data.json file.
The Array of Objects should have the following fields :
Name
Type
Price
After writing the JSON data, this file should be imported in loopobject.js.
I tried the above request with the below data.json
data.json
"{[{\"Name\":\"Apple\",\"Type\":\"fruit\",\"Price\":123},{\"Name\":\"pizza\",\"Type\":\"italian\",\"Price\":360},{\"Name\":\"burger\",\"Type\":\"mac&cheese\",\"Price\":321},{\"Name\":\"jangri\",\"Type\":\"sweet\",\"Price\":329}]}"
loopObject.js
var json = require('./data.json');
json.forEach(function(object) { console.log(object.Name); });
verify.js
const Joi = require('joi');
const fss =require('fs');
const schema = Joi.array().min(3).has({
Name: Joi.string().required(),
Type: Joi.string().required(),
Price: Joi.number().required(),
});
var data;
try{
data = require("./data.json");
}catch(e)
{
data={};
}
var XMLWriter = require('xml-writer');
xw = new XMLWriter;
const result = Joi.validate(data, schema);
// You can also pass a callback which will be called synchronously with the validation result.
Joi.validate(data, schema, function (err, value) {
if(err==null)
{ console.log("JSON is valid.");
}else{
console.log("JSON schema is not correct. Enter specified JSON scehma.");
}
});
i have tried couple of ways, everything looks good,but don't know where i am doing mistake. Could you please help on this??
The JSON format is not correct, try this one:
data.json
[
{
"Name": "someName",
"Type": "someType",
"Price": 123
},
{
"Name": "someName",
"Type": "someType",
"Price": 123
},
{
"Name": "someName",
"Type": "someType",
"Price": 123
},
{
"Name": "someName",
"Type": "someType",
"Price": 123
}
]
The JSON data describes an array, and each element of that array is an object.
I am trying to create a local lang file that will be formatted as json. I have the following navigation in json format below. And I need to create a new JSON file using GULP to create a lang file (see below)
"lists": [
{
"title": "Application Intel",
"items": [
{
"title": "Analytics Dashboard",
"href": "intel_analytics_dashboard.html"
},
{
"title": "Marketing Dashboard",
"href": "intel_marketing_dashboard.html"
},
{
"title": "CEO Dashboard",
"href": "intel_ceo_dashboard.html"
},
{
"title": "Introduction",
"href": "intel_introduction.html"
},
{
"title": "Build Notes",
"href": "intel_build_notes.html",
"text": "Build Notes",
"span": {
"class": "",
"text": "v{{version}}"
}
}
]
}
I need to create a file that looks like the following json:
"nav": {
"application_intel": "Application Intel",
"intel_analytics_dashboard": "Analytics Dashboard",
"intel_marketing_dashboard": "Marketing Dashboard",
"intel_ceo_dashboard": "CEO Dashboard",
"intel_introduction": "Introduction",
"intel_build_notes": "Build Notes",
}
Whats the best way to go about this?
Here is solution.
Let's say you have nav.json file inside src and you want to change its shape and place it into dest directory. You can achieve this from within gulpfile.js
const { src, dest } = require("gulp");
const through = require("through2");
// gulp task
function json() {
return src("src/nav.json")
.pipe(
through.obj((file, enc, cb) => {
// get content of json file
const rawJSON = file.contents.toString();
// parse raw json into javscript object
const parsed = JSON.parse(rawJSON);
// transform json into desired shape
const transformed = transformJson(parsed);
// make string from javascript obj
const stringified = JSON.stringify(transformed, null, 2);
// make bufer from string and attach it as current file content
file.contents = Buffer.from(stringified);
// pass transformed file into next gulp pipe
cb(null, file);
})
)
.pipe(dest("dest"));
}
// transformation
function transformJson(input) {
const result = { nav: {} };
// read json field by field
Object.keys(input).forEach(topLevelKey => {
// current object
const topLevelItem = input[topLevelKey];
// in your design topLevelItems are arrays
topLevelItem.forEach(menuItem => {
if (menuItem.title) {
// make url either from item href or title
const itemUrl = makeUrl(menuItem.href || menuItem.title);
result.nav[itemUrl] = menuItem.title;
}
// prcoess children
if (menuItem.items) {
menuItem.items
.filter(child => !!child.title) // process only child items with title
.forEach(child => {
const childUrl = makeUrl(child.href || child.title);
result.nav[childUrl] = child.title;
});
}
});
});
return result;
}
// helper func
function makeUrl(href) {
return href
.toLowerCase()
.replace(/\.html$/, "")
.replace(/\s/g, "_");
}
// export for use in command line
exports.json = json;
json transformation function is bit forEachy and if you have deep nested navigation structure, maybe you should change it into something recursive
I have formData node that has dynamic jsonObject value in mongodb
{
"_id": {
"$oid": "5a71fea0f36d2848ae4f8f0a"
},
"formData": {
"pages": [
{
"name": "page1",
"questions": [
{
"type": "comment",
"name": "about",
"title": "Please tell us about your main requirements "
}
]
}
]
},
"editorId": "59678e58f36d2842f777bc48",
"datetimeSubmit": "2018/01/15"
}
I write a node API to fetch the data from mongodb, it only display ids, editorI and datetimesubmit nodes, but it ignores the formData(jsondata) field.
const Model = require('./myModel');
module.exports = {
getData: function (callback) {
Model.find({}, (err, jsonObj) => {
callback(null, {
data: jsonObj
})
})
}
}
looks like the model.find() doesn't return jsonObject value?
thanks
got my own question fixed, basically, i should also define the data type as JSON in schema, otherwise, will be ignored.
Hi i have a json like this called body:
{
data: {
id:1,
name:hallo
},
paging: {
cursors: {
next: "abc"
}
}
}
so I have to save it into a map and then I'll need to print or to modify it.
If I save it with this code
var Map = require("collections/map");
var new_body = JSON.parse(body)
var map = new Map(new_body);
I have some difficulties to print for example map.paging.cursor.next, how can I do it? maybe I had to define a structure of that map before inserting the json inside it, I don't know...
Your json have errors. Correct them like this.
{
"data": {
"id": 1,
"name": "hallo"
},
"paging": {
"cursors": {
"next": "abc"
}
}
}
Then you can parse the json like this and get the paging.cursors.next value.
var map = JSON.parse(body);//This will parse the json string
Now map.paging.cursors.next will have the value "abc"