receive string from input and convert it to javascript object [duplicate] - javascript

This question already has answers here:
Safely turning a JSON string into an object
(28 answers)
Closed 3 years ago.
I want to receive my javascript object such as
{ "user": { "active": true, "dob": '1988-09-11', "group": 14, "department: "business"" } }
from input using a node package such as readline-sync and store it as an object to be able to access to the values seperately using their keys. The readline-sync stores my input as an string I tried to convert it to javascript object using
let obj= JSON.parse(JSON.stringify(stringObj));
but still it is string.
Is it possible to receive a string from the input and convert it object?

JSON.stringify() Receives an object and returns a string. If what you want to do is the inverse (convert a JSON string into an object), you need to use JSON.parse()
Then, what you want is:
let obj = JSON.parse(stringObj);

Related

How to read Object? [duplicate]

This question already has answers here:
Accessing nested JavaScript objects and arrays by string path
(44 answers)
Closed 5 years ago.
Say i have object like
var a = {"user":
{'average':
{'score':4
}
}
}
How can I read object value using its keys
Say I have user Object with me and have key "average.score" can I get the value directly?
a.user["average.score"];
//Coming as undefined
a.user["average"]["score"]
// Working as expected : 4
I have the key of "average.score" all together with me want to get the value of score how can I do it directly without splitting the key.
Use a.user["average"].score
var a = {"user":
{'average':
{'score':4
}
}
}
console.log(a.user["average"].score);

Access JavaScript array object in PHP [duplicate]

This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 7 years ago.
I have a JSON data about 150 users, i have convert that into java script array, now what i want it to pass this array to PHP, So that i can perform further operations on this array.
var JSONString = '[{'user_id' : "1", "user_name" : "abc", "email":"abc#gmail.com"}, {"user_id":"2", "user_name":"abc", "email":"abc2#gmail.com"}]'
var JSONObject = JSON.parse(JSONString);
for (var key in JSONObject) {
if (JSONObject.hasOwnProperty(key)) {
console.log(JSONObject[key]["user_id"] + ", " + JSONObject[key]["user_name"]);
}
}
now what i need it to pass this array to PHP array variable , that will loop through and perform further operation.
i have seen some solution that display it on html with JavaScript, but i don't need that. i need this array object in php array variable.
Instead of parsing JSON string in JavaScript, pass it directly to PHP and use
PHP function
json_decode($json, true)
it will give you data in array format.

How to access this attribute [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 8 years ago.
I'm new to JavaScript and I've been stuck on this for a little while now. Let's say I have an object inside an array inside an object, like so:
var myCrazyObject = { "name": "A ridiculous object", "some array": [7, 9, { purpose: "confusion", number: 123 }, 3.3], "random animal": "Banana Shark"};
Now I know that I can access the "some array" attribute thusly:
myCrazyObject["some array"]
So, the part that I'm stuck on is, how do I access the purpose or number attributes?
It is the third element in your array, so you can access it by index:
myCrazyObject["some array"][2].purpose
or if you prefer the equivalent:
myCrazyObject["some array"][2]["purpose"]
would return "confusion". Obviously it's pretty brittle stuff. Normally you should be storing elements of the same type inside an array and not some integers at the beginning and then another arbitrary object. If for some reason the third element in this array wasn't an object you would get an error. So it's up to you to make the proper error handling here or fix your data input in order to bring some consistency here.

Javascript, cannot access JSON property [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 8 years ago.
I have a json string received from websocket :
{
"type":"newLoan",
"cartItems":{
"numberOfItems":null,
"bookList":[
{
"id":"1",
"title":"The Count of Monte Cristo",
"author":"Alexandre Dumas ",
"genre":"Comedy",
"returnDate":"January 15"
}
]
}
}
This result is shown after console.log(receivedMessage), but when I try to access type property by console.log(receivedMessage["type"]) it gives me undefined.
Still the same with console.log(receivedMessage.type).
How could I access the type property?
There is probably an issue with the headers that are being sent along with your string, if the content type isn't set to application/json or the javascript equivalent then it will be treated as a string and no a json object
try:
JSON.parse(receivedMessage).type

parsing json string using json parse method [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 8 years ago.
Hi friends I have a I have a json string as shown below. How to parse the string to get day,min_amount,max_amount values .
[{"day":"1970-01-01","min_amount":"0.00","max_amount":"0.00"},{"day":"1970-01-02","min_amount":"1.00","max_amount":"2.00"}]
Just use JSON.parse. The syntax for accessing a value is simple:
obj = JSON.parse(json)
day = obj[0].day
min_amount = obj[0].day
max_amount = obj[0].day
The great thing about Javascript is how simple it is to use JSON, because JSON is just a serialized version of plain-old javascript hashes, arrays, and scalars.
It's already in object form. SO use this :
var x = [{"day":"1970-01-01","min_amount":"0.00","max_amount":"0.00"},{"day":"1970-01-02","min_amount":"1.00","max_amount":"2.00"}]
jQuery.each(x,function(e){
console.log(x[e])
console.log(x[e].day)
});
Here is the working example : http://jsfiddle.net/u6J8A/
As you may not have noticed, JSON is JavaScript synthax.
<script type="text/javascript">
var data = [
{"day":"1970-01-01","min_amount":"0.00","max_amount":"0.00"},
{"day":"1970-01-02","min_amount":"1.00","max_amount":"2.00"}
];
</script>
Dumping it directly in the JavaScript code is perfectly valid.
But if you are fetching this data at run time and have the information as a string, you can convert it using JSON.parse(string).
The information can be then read from this structure by the variables data[0].day, data[0].min_amount, data[0].max_amount, data[1].day, data[1].min_amount, data[1].max_amount.

Categories

Resources