"system_details":
{
"ucs_version":"00.02",
"ucs_crash_id":"1500000000002A30040000003002B038",
"ucs_static_id":"0x1500000000002A30",
"ucs_variable_id":"0x040000003002B038"
}
I have a javascript object -json, whose value is as above.
1.How can i store each character from json into a array?
a[0]=", a[1]=v,a[2]=e and so on...........
2.How can i convert the json to a string or key-value pair?
if a want value of the 'version' from json, it should output me value 00.02.
The code shown is already an object, so objName.system_details.ucs_version will return "00.02".
To turn the obj into a string, use JSON.stringify( objName );
To store the characters of a string into an array, you can split it on nothing, so string.split('') will just split a string into all its characters.
Eg. "system_details".split('') becomes ["s","y","s","t","e","m","_","d","e","t","a","i","l","s"]
Note that the " around each key and value is not part of the string, so if your array needs to include these, you'll have to add them to the front and the back of the array after splitting the string.
Hope it helps.
convert your object to string use simple java script function => JSON.stringify
try this boss
JSON.stringify( Your_object);
Related
I have an array value which is coming from database as an string. I need to convert it into an array. When I check my value in console I can see value as
"[["COL1","COL2","COL3"],["COL4","space,"COL5"]]"
In order to perform my operations I need it to be in below structure
[["COL1","COL2","COL3"],["COL4","space,"COL5"]]
I have already tried JSON.parse() and parseJSON
Expected Result :
[["COL1","COL2","COL3"],["COL4","space,"COL5"]]
Actual Result :
"[["COL1","COL2","COL3"],["COL4","space,"COL5"]]"
You need to remove the outer quotes from your string, then pass the value to JSON.parse() to get the array.
Also, you have to quote each item correctly, "space should be "space".
You can sanitize the string with String.prototype.replace() (assuming the quoting of space has been fixed in the DB):
const data = '"[["COL1","COL2","COL3"],["COL4","space","COL5"]]"';
const dataSanitized = data.replace(/^"|"$/g,"");
console.log(JSON.parse(dataSanitized));
I would suggest you do parse
JSON.parse('[["COL1","COL2","COL3"],["COL4","space","COL5"]]')
i would not suggest eval as i just read an article about "how eval is evil"
https://javascriptweblog.wordpress.com/2010/04/19/how-evil-is-eval/
I have array of object in data attribute and I want to get that value as array of object in js. I confused how to parse string to array in js. Here is my data attribute:
var values = [{title:"My Office",loc:{lat:27.7081018,lng:85.3342199}},{title:"My Hostel",loc:{lat:27.7072867,lng:85.3253844}},{title:"Sudhir House",loc:{lat:27.6802258,lng:85.3805697}},{title:"Indra Chowk",loc:{lat:27.7057217,lng:85.3084168}},{title:"Jamal",loc:{lat:27.7017848,lng:85.3127387}},{title:"Patan",loc:{lat:27.5978047,lng:85.355257}},{title:"Baktapur",loc:{lat:27.6773968,lng:85.406957}},{title:"Dhulikhel",loc:{lat:27.6241873,lng:85.5410204}},{title:"Nagarjun",loc:{lat:27.7249402,lng:85.3591267}},{title:"Chitlang",loc:{lat:27.6478865,lng:85.1335696}},{title:"Pilot baba ji",loc:{lat:27.6406024,lng:85.4202461}}];
// Result I want to get
console.log(values)
// Return string
console.log($('div').attr("data-values"));
// Return single array
console.log(new Array($('div').attr("data-values")));
// Return error
console.log(JSON.parse($('div').attr("data-values")));
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div data-values='[{title:"My Office",loc:{lat:27.7081018,lng:85.3342199}},{title:"My Hostel",loc:{lat:27.7072867,lng:85.3253844}},{title:"Sudhir House",loc:{lat:27.6802258,lng:85.3805697}},{title:"Indra Chowk",loc:{lat:27.7057217,lng:85.3084168}},{title:"Jamal",loc:{lat:27.7017848,lng:85.3127387}},{title:"Patan",loc:{lat:27.5978047,lng:85.355257}},{title:"Baktapur",loc:{lat:27.6773968,lng:85.406957}},{title:"Dhulikhel",loc:{lat:27.6241873,lng:85.5410204}},{title:"Nagarjun",loc:{lat:27.7249402,lng:85.3591267}},{title:"Chitlang",loc:{lat:27.6478865,lng:85.1335696}},{title:"Pilot baba ji",loc:{lat:27.6406024,lng:85.4202461}}]'>
</div>
I the above code I have stored the array to data-values and when getting the value of data-values I got string. How can I parse that string back to array of an objects.
It is because of invalid JSON format, the keys should be enclosed with double quotes.
[{"title":"My Office","loc":{"lat":27.7081018,"lng":85.3342199}}]
Note: For temporary workaround eval() works fine
I am using angular5.I have a questionOrderList which is a string which contains multidimensional array in string format. What I need to do is to parse the array in string format back to multidimensional array type. Currently I have used the JSON.parse method my code looks like.
console.log("qorder service : "+questionOrderList)
console.log("qorder service parsed:" +JSON.parse(questionOrderList))
The output I am getting is
qorder service : [[3290],[3287],[3289,3293],[3295]]
qorder service parsed: 3290,3287,3289,3293,3295
But in case of array with one row it is correct
qorder service : [[3290,3287,3289,3293,3295]]
qorder service parsed: 3290,3287,3289,3293,3295
What you are going is correct, see this:
var questionOrderList = "[[3290],[3287],[3289,3293],[3295]]";
var parsedList = JSON.parse(questionOrderList);
console.log(parsedList)
The actual issue is how JS interprets string concatination with arrays. It implicitly calls .join(',') on the array as demonstrated here:
var questionOrderList = "[[3290],[3287],[3289,3293],[3295]]";
var parsedList = JSON.parse(questionOrderList);
console.log(parsedList);
console.log("" + parsedList);
console.log(parsedList.join(','))
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
After calling console.log(JSON.stringify(req.params)), I get a string with the following structure:
{"q":"{\"email\":\"mymail#mail.com\"}","apiKey":"1234"}
With console.log(req.params.q), I have this result: {"email":"mymail#mail.com"}.
But I get "undefined" if I try to view the email value with console.log(req.params.q.email) or console.log(req.params.q["email"])
What is the best approach to get that value?
You must JSON.parse that inner part :
var test = {"q":"{\"email\":\"mymail#mail.com\"}","apiKey":"1234"};
alert(JSON.parse(test.q).email);
alerts mymail#mail.com
Why?
Because test holds an javascript object where q holds a string, So you must parse that string if you want to extract the JSON values from that string.
It looks like req.params.q is a string: "{\"email\":\"mymail#mail.com\"}".
You need to parse that json then fetch the value.
req = {params: {"q":"{\"email\":\"mymail#mail.com\"}","apiKey":"1234"}}
JSON.parse(req.params.q)
> Object {email: "mymail#mail.com"}
JSON.parse(req.params.q).email
> "mymail#mail.com"