Trying to pass a nested list with JSON to JS with Flask [duplicate] - javascript

This question already has answers here:
JavaScript raises SyntaxError with data rendered in Jinja template
(3 answers)
Closed yesterday.
The problem is I'm passing my argument to the Javascript like this: link.
The variable "chatlist" is the problem. The value of that variable is
[[{"user": "lol"}, {"msg": "lol has created this chat."}], [{"user": "lol"}, {"msg": "lol"}]]
But when I try to access that variable through this code: link, it ends up looking like
[[{"user": "lol"}, {"msg": "lol has created this chat."}], [{"user": "lol"}, {"msg": "lol"}]]
What I'm expecting is in both my Python code and my Javascript, I get
[[{"user": "lol"}, {"msg": "lol has created this chat."}], [{"user": "lol"}, {"msg": "lol"}]]
as the value of the variable labelled "chatList". This value also needs to be "parsable" (not sure if that's the right word). I need to be able to access all elements of the list and all keys and values of each dictionary in each element.

Pass the list as is to render_template.
return render_template("chat.html", chatlist=chatInfo, pin=pin)
In the template you can use the jinja filter tojson.
var chatList = {{ chatlist | tojson }};
console.log(chatList);

It looks like the double quotes are getting HTML escaped.
I would solve this by using Javascript's decodeURI() method.

Related

ReferenceError: list is not defined Potsman error [duplicate]

Hi I need to retrieve a JSON key which contains special characters, my JSON example is as follows.
{"profile":"dev","appconf:[classpath:/application.yml]":{"a.b":value,"x.y":value}
Please can someone provide example code to retrieve value of this key.
appconf:[classpath:/application.yml]
I would use bracket notation:
var value = myJsonData["appconf:[classpath:/appliation.ytml]"];

Get value in data object Javascript

I have a data object, and i want to get on of the value from it, when i try to print the data:
console.log(data);
i got an object like the image below :
the problem is i want to get the order[billing_address][country_id] which i think is an object, but i don't know how to fetch it. i've tried :
console.log(data.order); //didn't work
console.log(data.order[billing_address][country_id]);//didn't work
The name of the property is: "order[billing_address][country_id]"
To access its value try:
console.log(data['order[billing_address][country_id]'); // Should work
It appears that the values you are looking for have keys that are the whole string:
"order[billing_address][telephone]"
You can access these values like this:
data["order[billing_address][telephone]"] //"5"
You are currently trying this:
data.order[billing_address][country_id]
What you are trying doesn't work because there are no variables billing_address or country_id that are defined, and the object is not that deeply nested - just has the above mentioned long string for a key.

Assign unserialize Laravel object to JS array

I have serialize object returning from Controller to blade view in laravel like:
"a:3:{i:0;s:1:"2";i:1;s:1:"4";i:2;s:1:"6";}"
from my blade view I use this JS code block to get those value as array.
var branches = {{unserialize($preliminary->branches)}};
But in there I'm getting error saying
expression expected
any suggestions to solve this situation..?
Run json_encode on top of your unserialize.
E.g.
var branches = {{json_encode(unserialize($preliminary->branches))}};
unserialize is giving you a PHP object, which you are trying to inject directly into JS. By passing it through json_encodeyou convert it to a string javascript can grok.

Access fields in an array contained in a model

I have some JavaScript code that needs to be able to access fields of an array of objects that is contained within my model. I currently have this:
var model = #Html.Raw(Json.Encode(Model));
for(var i = 0; i < model.testobject.length; i++) {
console.log(model.testobject[i]);
}
Which prints out the fields within each object of testobject. But say I have a field, ID, in my testobject class. How do I then access that? Doing this:
console.log(model.testobject[i].ID);
Does not work. Do I have to somehow encode that specific instance of testobject before accessing it's fields?
And yes, before anyone says it I know this should be contained within the controller. As it currently stands though, that's not possible for this project.
This is the general structure of what is printed out:
Object {field: value}
Edit:
I attempted to use JSON.stringify on my model.IdentifiApprovalConfigurations and it seems I got a little close to reaching my solution. This is what it looks like now:
console.log(JSON.stringify(model.testobject[i]).ID);
However, this prints out undefined.
Edit 2:
Oops, seems the ID field I'm trying to access isn't being populated before I send them to my view which is my own issue. JSON.stringify works though, and I understand why it wasn't working earlier.
Final edit:
JSON.parse(JSON.stringify(model.testobject[i])).Value
I had to stringify and then parse my JSON to access the value.
As I'm still starting out with web development, I forgot that I needed to convert my object into a JSON object. This:
JSON.parse(JSON.stringify(model.testobject[i])).Value
Is the final piece of code that allows me to stringify an object, parse the JSON and then access fields within that object.

Check passed data with a view is not empty in laravel

I am using laravel 4 and I want to pass a data with a view.
I have used this code in a controller.
$view = View::make('settings.editEvent');
$view->bounderyData = $bounderyData;
And I want to check whether this data exists or not in the view settings/editEvent.blade.php
Tried using this..
<script>
if('{{$bounderyData.length()}}'!=null)
console.log('exists');
</script>
Error :
Array to string conversion error
How can I check the existence ?
Do not assign the data to the View variable, but instead, pass it along using with as Laravel requests you to use:
$view = View::make('settings.editEvent')
->with('bounderyData', $bouderyData);
Actually both of the snippets work the same way. You can either pass data using with() method or by assigning it to view as property. So it doesn't really matter. But it looks like you are using some weird syntax because you are trying to access method length() using dot syntax inside Blade echo statement. Try:
if({{count($bounderyData)}}!=null)
console.log('exists');
or something similar. Remember that everything inside {{}} is going to be echo'ed by PHP. So if you have some sort of array there you may either want to count number of elements or maybe cast it to JSON and then decode it inside Javascript. If you still have problem, let us know what is the issue.

Categories