i have html file with javascript, app for driving tests and 2 json files: Categories of name of test and questions with answer it self. this json is dump of sqlite database. example output below.
[
{
"cat_id": "1",
"cat_name": "Общие понятия",
"cat_score": "2"
},
{
"cat_id": "2",
"cat_name": "Обязанности водителя",
"cat_score": "7"
},
{
"cat_id": "3",
"cat_name": "Cостояние здоровья водителя",
"cat_score": "1"
}
]
in my engine.js everything works but my questions and categories files are not loaded. i don't know how to load then and parse to array or variable to use it in script.
for example in php i have
require and include or file_get_content but how i need to load
So i found how to load but it not working
function load() {
var someData_notJSON = JSON.parse(data);
console.log(someData_notJSON);
}
my html file is
<script type="text/javascript" src="./Categories.json"></script>
<script type="text/javascript" src="./Questions.json"></script>
in your case you're trying to parse JSON from undefined data.
function load(data) {
var someData_notJSON = JSON.parse(data);
console.log(someData_notJSON);
}
load(`[
{
"cat_id": "1",
"cat_name": "Общие понятия",
"cat_score": "2"
},
{
"cat_id": "2",
"cat_name": "Обязанности водителя",
"cat_score": "7"
},
{
"cat_id": "3",
"cat_name": "Cостояние здоровья водителя",
"cat_score": "1"
}
]`);
Related
Hello dear stackoverflow community,
I want to make an Electron app [Javascript not jQuary] (or am in the process of doing so) and would like to add a function that puts one config into the "format" of another.
The big file from which I want to take the information I currently read in via "dialog.showOpenDialog" and can also access the json object.
Now to the problem:
The file I get via the dialog is 8000 lines long and contains individual information that I want to pack into a smaller document with about 3000 lines. Important: Individual information have a different name e.g. I want "ABCD: 23" from document 1 in the other as EFG: 23.
I had already asked this question and also received a good answer from a user[click here] which also works well, however I have now copied and pasted the function several times (as below in the example with the for function)
And this is of course not the fine English way... is there a way to make this more compact?
BigCFG.json:
"ANIMALS"{
"DOG": {
"DOG1": "0" ,
"DOG2": "1" ,
"BREED": {
"breed": "1",
"breed": "2",
},
},
"CAT": {
"CAT1": "0",
"CAT2": "1" ,
"BREED": {
"breed": "1",
"breed": "2",
},
},
}
Tables:
let dogTable = {
'dog1':'dog2',
'dog3': 'dog4'
}
let dogbreedTable = {
'breed1':'breed2',
'breed3': 'breed4'
}
let catTable = {
'cat1':'cat2',
'cat3': 'cat4'
}
let catbreedTable = {
'breed1':'breed2',
'breed3': 'breed4'
}
The for function which I would like to have more compact: (I have the currently about 40 times in my project in a row to test)
for (let bigKey in BigCFG.ANIMALS.DOG) {
if (bigKey in dogTable) {
smallCFG.ANIMALS.DOG[dogTable[bigKey]] = BigCFG.ANIMALS.DOG[bigKey]
}
for (let bigKey in BigCFG.ANIMALS.DOG.BREED) {
if (bigKey in dogbreedTable) {
smallCFG.ANIMALS.DOG.BREED[dogbreedTable[bigKey]] = BigCFG.ANIMALS.DOG.BREED[bigKey]
}
for (let bigKey in BigCFG.CAT) {
if (bigKey in catTable) {
smallCFG.ANIMALS.CAT[catTable[bigKey]] = BigCFG.ANIMALS.CAT[bigKey]
}
for (let bigKey in BigCFG.ANIMALS.CAT.BREED) {
if (bigKey in catbreedTable) {
smallCFG.ANIMALS.CAT.BREED[catbreedTable[bigKey]] = BigCFG.ANIMALS.CAT.BREED[bigKey]
}
Expected smallCFG.json:
"ANIMALS": {
"CAT": {
"cat": "3",
"cat": "4",
"BREED": {
"breed": "2",
"breed": "3",
},
},
"DOG": {
"dog": "3",
"dog": "4",
"BREED": {
"breed": "3",
"breed": "4",
},
}
}
I would be very grateful for the help and of course appreciate your time
Not sure if I fully understood your problem and constraints. What I get by your other questions is you need to transform a json document for one structure to another, right? Have you tried JSONata? Maybe it matches your requirements.
PS.: I've tried add this as a comment, but I don't have reputation enough
I know there are several threads on this subject but I've looked through over 30 threads without success.
I have managed to parse a JSON response so it looks like this:
{
"1": {
"id": "1",
"name": "Fruit",
.
.
.
"entities": {
"1": {
"id": "1",
"name": "blue bird",
.
.
.
"status": "1"
},
"2": {
using this code
let json = JSON.parse(body);
console.log(json);
Now I want to access the "id", "name" etc. AND the "id" and "name" for the "entities" tag.
So far I have tried:
console.log(json[0]);
console.log(json.id);
which both returns undefined
I have also tried
console.log(json[0].id);
which gives an error
Any ideas?
In this instance, your first key is 1, so you can access it with json[1].
const json = {
"1": {
"id": "1",
"name": "Fruit"
},
"2": {
"id": "2",
"name": "Veggies"
}
};
console.log(json[1]);
In this json, you can reach the id by
json.1.id
But I think that first of all your json is not correctly written, you should have something like
{
"elements": [
{ "id" : 1, "name" : "fruit" },
{ "id" : 2, "name" : "vegetable" }
]
}
like that, json.elements is a collection/array, and you can loop, count, or any other things you will not be able to do because your json looks like a super heavy list of different properties ( he doesn't know that json.1 and json.2 are the same type of objects.
const jsonData = JSON.parse(body);
for (const i in jsonData) {
for (const j in jsonData[i]) {
console.log('${i}: ${jsonData[i][j]}');
}
}
I have a little problem.. I've got this JSON data:
[
{
"students": {
"student_id": "2",
"student_school": "1",
"student_name": "Charles"
},
"parents": [
{
"parent_id": "2",
"parent_school": "1",
"parent_name": "Tim"
}
]
},
{
"students": {
"student_id": "3",
"student_school": "1",
"student_name": "Johnny"
},
"parents": [
{
"parent_id": "3",
"parent_school": "1",
"parent_name": "Kate"
}
]
}
]
The problem is that I try to call to my html page by angular:
{{student.student.student_name}}
Yeah it works but when I want to call the parents data it doesn´t...
{{student.parents.parent_name}}
Simply:
<div ng-repeat="student in data">
{{student.students.student_name}}
{{student.parents[0].parent_name}}
</div>
Or define function in scope called for example getParentDescription and than
<div ng-repeat="student in data">
{{student.students.student_name}}
{{getParentDescription(student)}}
</div>
Because parents is an array. You must specify the index (0 in your case). See the response here : How to get value from a nested JSON array in AngularJS template?
You can't access child scopes directly from parents. See the comment by Vittorio suggesting<ng-repeat="child in parent.children"/> also Binding to Primitives
I'm guessing student is from an ng-repeat where you go through each object in the array.
Take a closer look at your JSON. While "students": {} points to an object, "parents": [] points to an array. Fix your JSON and it'll be fine
I want to parse a json file and output in html by using javascript
here is my json file
{"quiz":[
{
"quizName":"Quiz 1",
"question": [
{
"text": "1+1?",
"choiceA": "1",
"choiceB": "2",
}
]
},
{
"quizName":"Quiz 2",
"question": [
{
"text": "2+2?",
"choiceA": "3",
"choiceA": "4",
}
]
}
]}
here is my html
<body>
<div id="placeholder"></div>
<script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<script>
$.getJSON('data.json', function(data) {
var output="<ul>";
for (var i in data.quiz) {
output+="<li>" + data.quiz[i].quizName+"</li>";
}
output+="</ul>";
document.getElementById("placeholder").innerHTML=output;
});
</script>
</body>
I want to display all the "quizName" in a list format like this
Quiz 1
Quiz 2
But by code doesn't output anything.
I am new to json and javascript, I don't know whether the json file is not correct or the javascript is not correct. Thanks.
You can't have commas after the last items of an object in a JSON file.
Change:
"choiceB": "2",
To this:
"choiceB": "2"
Do the same for choiceA": "4",
if u r not sure with the Json is right or wrong you can check here online json Parser or jsonEditor
you have extra commas
this is the correct Json
{"quiz":[
{
"quizName":"Quiz 1",
"question": [
{
"text": "1+1?",
"choiceA": "1",
"choiceB": "2"
}
]
},
{
"quizName":"Quiz 2",
"question": [
{
"text": "2+2?",
"choiceA": "3",
"choiceB": "4"
}
]
}
]}
ALso parsing it REFER parsing in fiddle
Hope this Helps
Remove extra commas in your json file at line numbers 9 and 19.
All you need it's do one more loop..
var output="<ul>";
for (var i in dataset) {
for (var j in dataset[i])
{
output+="<li>" + dataset[i][j].quizName+"</li>";
}
}
output+="</ul>";
document.getElementById("placeholder").innerHTML=output;
here you go: http://jsfiddle.net/baximilian/qpJjN/
I wrote a quick script to parse two fairly large json file (~17k records) to do a comparison of the two. I have confirmed they are both valid json (via jsonlintpro) and the same format. (The source is the same so this should be a given. But, I always assume the mistake is mine. And I still do. Just somewhere else.) However, the parsed file just outputs [object, Object]. I'm wondering what the cause could possibly be?
The json format is like this small snippet (anonymized of course):
[
{
"id": "1234",
"name": "Name1",
"url": "https://localhost/Name1",
"date_created": "2013-07-05T18:47:05Z",
"date_cancelled": "",
"props": [
{
"id": "54321",
"type": "Client",
"value": "General Store"
},
{
"id": "65432",
"type": "Contact_Name",
"value": "Joe Smith"
}
]
},
{
"id": "23456",
"name": "Name2",
"url": "https://localhost/Name2",
"date_created": "2014-02-27T17:46:43Z",
"date_cancelled": "",
"props": [
{
"id": "34567",
"type": "Client",
"value": "Bait Shop"
}
]
}]
And here is the pertinent code:
var _ = require('underscore');
var recs = require('./prod.json');
printArr(recs);
console.log(recs.length);
function printArr(arr) {
arr.forEach(function(item) {
console.log(item + ", ");
});
}
Any guidance would be greatly appreciated.
UPDATE:
Ok, so apparently the issue is with my printArr function. I'm not sure what I'm doing wrong there. I'd like to figure it out because I want to expand upon that so I can print selectively.
the parsed file just outputs [object, Object].
This is the expected behavior BECAUSE you are concatenating an object with a string.
Try console.log(item) instead
console.log(item); should indeed print [object, Object], did you try to output its properties instead?
function printArr(arr) {
arr.forEach(function(item) {
console.log( item.id, item.name, item.url, item.date_created, item.date_cancelled, item.props, ';');
});
}
Just export the value from the prod.json file.
prod.json file
module.exports = [
{
"id": "1234",
"name": "Name1"
},
{
"id": "1234",
"name": "Name1"
}]
elsewhere
var recs = require('./prod.json')
console.log(recs)