I am using txt.general_1.value in template to display its value but it shows error in console Can't find txt.general_1.value in [object Object]
But if I just use txt in template, it displays [object Object]
Following is the code. Kindly help me in resolving this issue.
Please note that following code is just to reproduce the problem, actual data is different.
<script>
var json_obj =
{
"all": [
{
"row": "row",
"column": "col-md-6",
"txt": {
"general_1": {
"type": "a",
"value": "aaa - Hello world"
},
"general_2": {
"type": "b",
"value": "bbb - Hello world"
},
"general_3": {
"type": "c",
"value": "ccc - Hello world"
}
}
}
]
};
var data = json_obj['all'][0];
var template = "<div class=\"{{row}}\"><p class=\"{{column}}\">{{txt.general_1.value}}</p></div>";
var text = Mustache.render(template, data);
$('#target').html(text);
</script>
I tried to reproduce the problem in the code snippet but didn't see any issue. The value is rendered correctly. Check the json_obj in your app, maybe it doesn't always contain the txt.general_1.value properties.
var json_obj = {
"all": [{
"row": "row",
"column": "col-md-6",
"txt": {
"general_1": {
"type": "a",
"value": "aaa - Hello world"
},
"general_2": {
"type": "b",
"value": "bbb - Hello world"
},
"general_3": {
"type": "c",
"value": "ccc - Hello world"
}
}
}]
};
var data = json_obj['all'][0];
var template = "<div class=\"{{row}}\"><p class=\"{{column}}\">{{txt.general_1.value}}</p></div>";
var text = Mustache.render(template, data);
$('#target').html(text);
<script src="https://unpkg.com/mustache#latest"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="target"></div>
Related
I'm trying to parse a multi-line JSON response to get a value for a key using JavaScript. I read we can't parse multi-line json, how I can achieve getting "Created" value from below json?
I tried converting the JSON to string and used replace to converting multi-line to single line with \n as deliminator. -- Unable to replace multi-liner text.
I tried extracting index of mischief key value and remove from the string -- Syntax error.
var v1 = {
"data": {
"type": "articles",
"id": "1",
"attributes": {
"title": "JSON:API paints
my bikeshed!",
"body": "The shortest article. Ever.",
"created": "2015-05-22T14:56:29.000Z",
"updated": "2015-05-22T14:56:28.000Z"
},
"relationships": {
"author": {
"data": {
"id": "42",
"type": "people"
}
}
}
}
};
alert(result.data.attributes.created);
My expectation is to get 2015-05-22T14:56:29.000Z as output.
In your example i see syntax error
Try to change string definition "" to ``
var v1 = {
"data": {
"type": "articles",
"id": "1",
"attributes": {
"title": `JSON:API paints
my bikeshed!`,
"body": "The shortest article. Ever.",
"created": "2015-05-22T14:56:29.000Z",
"updated": "2015-05-22T14:56:28.000Z"
},
"relationships": {
"author": {
"data": {
"id": "42",
"type": "people"
}
}
}
}
};
console.log(v1.data.attributes.created)
In case of new line you can replace all the new lines and then parse
the JSON.
Each OS generate different newline characters you need to
be aware of that while replacing newline characters.
var v1 = `{
"data": {
"type": "articles",
"id": "1",
"attributes": {
"title": "JSON:API paints
my bikeshed!",
"body": "The shortest article. Ever.",
"created": "2015-05-22T14:56:29.000Z",
"updated": "2015-05-22T14:56:28.000Z"
},
"relationships": {
"author": {
"data": {
"id": "42",
"type": "people"
}
}
}
}
}`;
var result = v1.replace(/(?:\r\n|\r|\n)/g, '');
var resultObj = JSON.parse(result);
console.log(resultObj.data.attributes.created);
I have this code (which is variant thought the time) in JSON
{
"5780": {
"app": "Bye",
"name": "Hello World",
"surname": "Friend"
},
"6654": {
"app": "Hello",
"name": "Hi",
"surname": "godbye"
}
}
I want to get the information from each section (for example the "app" section) an insert in a js variable. The problem is that the object title (5780 & 6654) change will change (at the same time, the sections information). So I need something like section[1].app = jsvariable1
You can loop
var obj = {
"5780": {
"app": "Bye",
"name": "Hello World",
"surname": "Friend",
},
"6654": {
"app": "Hello",
"name": "Hi",
"surname": "godbye",
}
}
for (var key in obj) {
console.log(obj[key].app);
}
This will log
Bye
Hello
If you want to update the value of app:
for (var key in obj) {
obj[key].app = "New Name";
}
The key is the 5780 and 6654
First of all, your json is not valid so you have to check it(as i did in the code below) . So here's what i did :
var sections = {
"5780": {
"app": "Bye",
"name": "Hello World",
"surname": "Friend",
},
"6654": {
"app": "Hello",
"name": "Hi",
"surname": "godbye",
}
};
var index = [];
//setting the index array
for (var x in sections) {
index.push(x);
}
console.log(sections[index[0]].app)
console.log(sections[index[1]].app)
I am currently trying to send a user information about a JSON object that I've recieved from an API. An example of the format is
[
{
"lang_code": "eng",
"site_language": "1",
"name": "English"
},
{
"lang_code": "afr",
"site_language": "1",
"name": "Afrikaans"
},
{
"lang_code": "ale",
"site_language": "0",
"name": "Aleut"
},
]
I want to be able to access the lang_code property of every single language and send it. I've tried to use
var languageCodes;
var languageResult = body.lang_code; //body is the result from a request.get({ ... })
for(var codes in languageResult) {
languageCodes = languageResult[codes];
}
Object.keys does nothing, as it just sends 72 numbers to me. Any thoughts?
On a side note, I also want people to be able to type "! languages [my command] eng", for example, and it sends "English" instead of just sending "1 is [object Object]".
Assuming body is the array at the top of your question, if you just want an array of all the language codes, this should suffice
var languageCodes = body.map(function(lang) {
return lang.lang_code;
});
var body = [{
"lang_code": "eng",
"site_language": "1",
"name": "English"
}, {
"lang_code": "afr",
"site_language": "1",
"name": "Afrikaans"
}, {
"lang_code": "ale",
"site_language": "0",
"name": "Aleut"
}];
var languageCodes = body.map(function(lang) {
return lang.lang_code;
});
document.getElementById('out').innerHTML = JSON.stringify(languageCodes);
<pre id="out"></pre>
I looped through your lang_codes like this:
var codes = [{"lang_code":"eng","site_language":"1","name":"English"}, {"lang_code":"afr","site_language":"1","name":"Afrikaans"},{"lang_code":"ale","site_language":"0","name":"Aleut"}];
for(var i = 0; i < codes.length; i++) {
console.log(codes[i].lang_code);
}
I am working on this demo. Why am I not able to query the JSON object by using the jsonSelect plugin?
var jsonData = {
"name": {
"first": "Lloyd",
"last": "Hilaiel"
},
"favoriteColor": "yellow",
"languagesSpoken": [{
"language": "Bulgarian",
"level": 2
}, {
"language": "English",
"level": 1
}, {
"language": "Spanish",
"level": 7
}]
};
var selector = '.name > *';
JSONSelect.forEach(selector, jsonData, function (resultObj) {
$('body').append('<p>' + $.trim(JSON.stringify(resultObj, null, ' ')) + '</p>');
});
You cannot directly include the JS file from Github. You need to host it yourself or find a CDN.
I know that I can create this JSON:
[
{
"Title": "Something",
"Price": "234",
"Product_Type": "dsf sf"
},
{
"Title": "hskiuea",
"Price": "4234",
"Product_Type": "sdawer"
}
]
*It uses the values obtained from text inputs contained within an element with the class "newpappend" - As shown below
Using the following code which obtains values from my HTML:
var jsonObj = []; //declare array
$(".newpappened").each(function () {
var p_title = $(this).find('#p_title').val();
var p_price = $(this).find('#p_price').val();
var p_ptype = $(this).find('#p_ptype').val();
jsonObj.push({Title: p_title, Price: p_price, Product_Type: p_ptype});
$(this).remove();
});
But, my goal is to end up with the JSON structured like this:
{
"Product_List": {
"Product": [
{
"Title": "asdf",
"Price": "53",
"Product_Type": "Adfsdf"
},
{
"Title": "asgsd",
"Price": "123",
"Product_Type": "Ntohig"
}
]
}
}
Basically, I am struggling with the correct Javascript to use to reach my goal
You are really close. Start with what you have, and then later:
var output = {
"Product_List": {
"Product": jsonObj
}
}
// output is now what you are looking for.