How to access json object appended as form data - javascript

I am passing object of arrays from frontend to backend(react to node) i am using the new FormData and append the "user" obj in the form and then i pass formdata object to my api.
user object in console log view
As shown in the pic this is my obj of arrays object.
This is my react js Code i am passing this user object to backend.
const formData = new FormData()
formData.append("user", user)
axios.post('http://localhost:5000/AddEmployee',
formData
)
Below code is Node js Code
console.log(typeof req.body.user)
It is showing us the string in console log view.
And In debugger mode the user object is showing in this form.
'[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]'
How can i access this object.
And i want to access this object in the below mentioned form.
let primary = req.body.user[1].primaryContact;

"How can i access this object."
Convert the array to a json string so you can see what's in it:
console.log(JSON.stringify(req.body.user))
"And i want to access this object in the below mentioned form."
In order to answer this question, we need to see what the structure of user is. Hopefully my first answer should help with that! If you need more help, post the output to my first answer.

Related

Display JSON Data from URL

I'm new to JSON and have not been able to get the result I want. In learning, all I want to do is almost a HELLO WORLD for JSON, with the ultimate goal being to display the data in a table.
I have a JSON Call URL that gives me JSON data, and it's formatted correctly.
I have written a script to see if I can get an alert for my JSON data:
Updated
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script>
$.getJSON("http://li93-171.members.linode.com:8080/BrokerManager/getActiveBrokerNames/?callback=?", function(data){
console.log(data);
});
</script>
Alert shown says "[object Object],[object Object]" -- I'm obviously doing something wrong. Please help!
First of all, JSON stands for JavaScript Object Notation, which means when your JSON gets received by the client, jQuery will automatically change it from a normal string to a more usable Object. That is why all you see in the alert is [object Object] since alert can only display strings. All non-string values will be casted into strings.
alert(data);
alert(data.toString()); //both are the same
To display an Object for debugging usages, use console.log or
$("<pre>").html(JSON.stringify(data, null, 4)).appendTo("body");
http://jsfiddle.net/DerekL/4XayF/
don't use alert use console.log(data) with objects
if you are using chrome click control + shift + i to view your console , IE hit F12

parsing an object array with JSON in Javascript for local storage

I am storing an array of Latlng objects for later use with a map, using local storage, when I use JSON.parse to get and put the values back into an array i get the following error
Uncaught Error: Invalid value for constructor parameter 0: [object Object],[object Object],[object Object]
my code is:
localStorage["positions"] = JSON.stringify(this.positions); //stores array
var stored_positions = JSON.parse(localStorage["positions"]); //get array
If i output the stringified array as a string, without parsing i see
[{"ob":11,"pb":11},{"ob":11,"pb":12},{"ob":10,"pb":12},{"ob":10,"pb":12}]
Ok so after a while I still could not manage to retrieve the google map latLng objects, so i stored an array of just the latitude variables and another of just the longitude. turning it back into the object after retrieval .
Drawing lines on the map with this gave slow laggy performance, i turned geodesic to false. and its fast again.

Assigning array to JSON data in URL

I'm trying to assign a JSON array from a URL to a variable. Here's my code:
$.getJSON("tljson.json",function(result){
var items = [];
items.push(result);
});
However, 'alerting' the items only returns
[object Object],[object Object],[object Object],[object Object]
What am I doing wrong?
What you're doing wrong is alerting the result. You have an array of four objects, but alert only shows the default text representation of objects, [object Object]. Convert your data to string yourself before printing. For example, instead of alert(result), you can try alert(JSON.stringify(result)).
Also, alert is ugly, annoying and hard to use; if you can, use console.log() and its friends instead, much easier on the programmer. Check the results in the JavaScript console. (This is under the assumption the alert() was for your own debugging benefit; if it's for users, try doing something in HTML instead.)
It already is a variable, result is the json response that you can access the same way you would if you pushed it to items.

JSON Object property is undefined

I make an AJAX call to fetch some data, get the data back as a JSON Object (not a string).
When I log the object, I get the correct object and it's properties. However, when I try to log one of the objects properties, I get undefined.
For a screenshot of my code:
http://i.imgur.com/gnt3w.gif
For a screenshot of the console log:
http://i.imgur.com/DO09m.gif
What am I doing wrong?
It looks like your POST is returning data in an array, not as an individual object. I bet if you log data[0].bursary_name, you will see the correct logged output.

How to convert JSON string to javascript objects?

I searched for this topic, and I can't seem to find the right way to parse the JSON string to read the objects.
Here is my code
$.getJSON("<url>",
function(data) {
alert("success!"+data);
});
outputs:
success![object Object],[object Object],[object Object],[object Object]
Firebug shows correct response, and when I click JSON tab I see all the objects.
Thanks!
When a JSON string is parsed, it is turned into a Javascript object. If you use a string method on an object, the string [object Object] is returned.
You need to use object property access methods instead (e.g. alert(data.somekey);).
Don't use alert() for debugging in cases like this if you have Firebug available. Use console.log(data) and you will get direct insights into your JSON data.
In this case you'd have realized that there's absolutely nothing wrong :D .
JSON = JavaScript Object Notation precisely because it is the way to declare object literals in JavaScript. The data parameter is already a Javascript object (in your case an array of objects) that you can access as:
data[index].fieldname
enter your json string here, and click on the created tree view On the top left you will see how you can access it
link text

Categories

Resources