array object with empty field name - javascript

In node-red i have array object in msg.payload.
the array object is [{"":"This Pallet ID is already in Pick Request List"}]
The array object has empty field name. please suggest how to retrieve value form the array.
I have tried msg.payload[0][0], msg.payload[0].Value. But not working

What you need is
var text = msg.payload[0][""];
Works in Chrome at least...

Related

Can not update array field in MYSQL

I am working with MYSQL via Sequelize and I created a field for an array. I read that I could do this with DataTypes of JSON in MYSQL and set the default value to []. I did this and I would like to update that field with string values. I have tried push like this:
category.dataValues.array_Of_food?.push('item1')
await category.save()
It won't save the added item in the database. If I use the update method like this:
await category.update({array_Of_food: category.dataValues.category_name})
This changes the array_of_food field with simply a string and not an array.
How do I simply push an item into the array field if this is possible?
I solved my problem by using this code:
category.update({ array_Of_food: Sequelize.fn(
'JSON_ARRAY_APPEND',
Sequelize.col('array_of_food'),
'$',
food.dataValues.food_Name
)})

JavaScript - Sending javascript object as a hidden input field

I have a JS Array of Objects as follows:
var tables = new Array();
var table = new Object();
table.no=1001;
table.name="Table-1";
tables.push(table);
table.no=1001;
table.name="Table-1";
tables.push(table);
Now I need to send this tables object-array through a hidden field in a form. Can I directly assign the value of hidden field as tables object.
use JSON.stringify() to convert it to JSON string and send your data.
side note:
There is an issue in the way you are creating an appending objects. Each time you are editing the same object. It will result in all the elemnts in your array being the last object you made as they are all references to the same object you have. resolve this by creating new objects when you append items to the array
var tables = new Array();
tables.push({"no":1000,"name":"Table-0"});
tables.push({"no":1001,"name":"Table-1"});
alert(JSON.stringify(tables));
You could stringify the value, put it in the field, send it to the server and then decode it. To stringify an object:
JSON.stringify(tables);
Assuming that you're using PHP on the server, you could decode it using json_decode function.

Show Json Object In TextBox

I have json returned from Database.I want to pick only one object Value and show it in the textbox. Here is my json.
[{
"ErrorMessage":"",
"ID":294,
"ExpenseID":0,
"EffectiveDate":"/Date(1262284200000)/",
"FormattedEffectiveDate":"01-01-2010",
"Perunit":null,
"VATRate":17.5,
"ChangedByID":1,
"ChangedByName":"superuser, superuser",
"Expense":null,
"ErrorSummary":null,
"ErrorList":[]
}]
I have Tried
var Jsoninvoice = JSON.stringify(data)
alert(Jsoninvoice.VATRate) and also alert(data.VATRate)
Thank you In advance.
You have an array containing 1 object. stringify turns this object into a string - you need it parsed so you can use it.
(I'm not sure if the object is parsed already, so to cover all bases, we'll parse it)
var Jsoninvoice = JSON.parse(data);
alert(Jsoninvoice[0].VATRate);
You have to specify the arrays index before you can access the properties.
It is already json object and stringify is not needed as #tymJV said you need to parse it if it is returned as string, just you need to access array item, as it is an array:
alert(data[0].VATRate)
SEE FIDDLE
You could use $.parseJSON(YOURJSON), and then use the keys to pull the data. Since it's in an array, you'll have to use [0] to pull the first item in the array (ie: your data).
Example
$(document).ready(function(){
var j ='[{"ErrorMessage":"","ID":294,"ExpenseID":0,"EffectiveDate":"/Date(1262284200000)/","FormattedEffectiveDate":"01-01-2010","Perunit":null,"VATRate":17.5,"ChangedByID":1,"ChangedByName":"superuser, superuser","Expense":null,"ErrorSummary":null,"ErrorList":[]}]';
var json = $.parseJSON(j);
alert("VATRate: "+json[0].VATRate);
});
Fiddle for reference

Select element from json dynamically

I have a an associative array in php that i parse to get json from it (json_encode) then i store the result in a javascript var
var myArray = <?php print json_encode($phpArray); ?>;
Now whene the user hit a button i should choose another element from the array dynamically, for example, i chose a random first element :
var an_element = myArray.a2.link;
-'a2' is an array in the main array
-'link' is an element in the a2 array.
So now whene the user hit my button, i want to choose a random other array id (for example a5, a9, etc.)
I tried this :
var randomnumber=Math.floor(Math.random()*101); // choose random number
var newRandomArrayID= "a"+randomnumber;
an_element = myArray.newRandomArrayID.link;
It doesn't works, it says myArray.newRandomArrayID is undefined.
Anyone can help?
Thank you
You need to use [] indexing to find properties by name:
an_element = myArray[newRandomArrayID].link;
Otherwise JS is looking for a property actually called newRandomArrayID on myArray rather than using the value of the variable to lookup the property.

Retrieve JSON Array element value

My web service returned a JSON Array (ie. [{"key":"value"}, {"key":"value2"}]). In the array there are two items as you can see, which are separated with comma. I want to know how can I access the second item, and get the value of "key" for the second item.
I've tried:
var a = msg.d[1].key
With no success of course.
This is the returned string:
"[{"Code":"000000","Name":"Black","Id":9},{"Code":"BF2C2C","Name":"Red","Id":11}]"
The string was extracted using FireBug after watching the msg.d.
Need your help in solving this.
msg[1].key
Assuming that the name of that array is msg. I'm not sure what you are using .d for.
If msg.d is a string representing an array, use JSON.parse.
JSON.parse(msg.d)[1].key
You can replace key with the key you are wanting, e.g. Code, Name, Id, etc.
This works as expected for me.
var msg = [{"key":"value"}, {"key":"value2"}];
var a = msg[1].key;
What is msg in the example above? Need more info to help.
If msg.d is a string then you have to eval (uggh) or parse it before applying the array subscript.

Categories