I am taking data from the multiple select that gives me an array data. The data that I am getting is
{provinces: "["1","2"]"}
and when I stringify this data I got
{"provinces":"[\"1\",\"2\"]"}
But what I really want is
{"provinces":["1","2"]}
is there any way ?
use the JSON.parse
var obj = {"provinces":"[\"1\",\"2\"]"}
obj.provinces = JSON.parse(obj.provinces);
console.log(obj)
Related
I need convert following in the EXACT format shown below with javascript, could you please suggest how to achieve this
from: {"healthy":true,"unhealthy_reasons":[]}
to: [{"healthy":true,"unhealthy_reasons":[]}]
If that's all you need to do, you can just wrap array brackets around the variable that contains the object:
let initialObject = {"healthy":true,"unhealthy_reasons":[]};
let arrayedObject = [initialObject];
But I'm wondering if there's more to this. If this is actually part of a more complicated task, just add that to your question and you'll get a more complete answer.
Use JSON.parse() and JSON.stringify()
let data = '{"healthy":true,"unhealthy_reasons":[]}';
let parsed = JSON.parse(data);
//TO get an array
console.log([parsed])
//TO get a string
console.log(JSON.stringify([parsed]))
So i am trying to display a data from an API using JavaScript but got an undefined instead when i console.log(data) what i am getting is like below. its an object but some how its encapsulate as a string? Any idea how to covert this into a actual object i am new with JavaScript api so i am a bit confused.
{"msg":"{\"part\":\"test\",\"station\":\"test2\"}"}
I already tried to de serialize the data using JSON.parse(data) but no luck.
What you have posted is actually an object with a property msg which is a strigified JSON. In order to get proper json from this try obj.msg = JSON.parse(obj.msg); Assuming obj is the response variable you can name it what you want.
See below snippet.
{"msg":"{\"part\":\"test\",\"station\":\"test2\"}"}
const obj = {"msg":"{\"part\":\"test\",\"station\":\"test2\"}"} ;
console.log('Before parsing:' + typeof obj.msg); // string
obj.msg = JSON.parse(obj.msg);
console.log('After Parsing:' + typeof obj.msg); // object
Hope this helps :)
JSON.parse transforms a string-like-object to the object. So, it expects the whole of the object as a string. Which is not happening in your case.
You have an object key msg and its value is a string-like-object. So You need to convert the msg value to the JSON.
a couple of ways to try -
let resp = JSON.parse(data.msg)
or
return JSON.parse(data.msg)
I have a JSON response as :
xxx: ["fsd,das"]
So I need to delete the value "fsd" from the json object but the problem is the response inside json is not the array , it is csv , so how do I delete it.
If anyone can come with the response.
Thanks
You can flatten, split and join the array with a comma, and splice out the value you don't want using indexOf():
var obj = {xxx: ["a,b","c,d","e","f,g,h","i,j,k"]}
var letter = prompt()
obj.xxx = obj.xxx.flat().join(',').split(',')
var index = obj.xxx.indexOf(letter)
obj.xxx.splice(index, 1)
console.log(obj)
I can see, inside the json object it is an array of string.
Take that string use str.replace() to delete that part from it and put it back.
I have the following JSON:
var x = [{"email":"info#test.nl"}]
How do I get the email in javascript? x.email doesn't work for me.
Bart
You need to reference to element of the array, then access the property.
var x = [{"email":"info#test.nl"}]
console.log(x[0].email)
Since the object is inside of an array, you must access the correct array element first, so use: x[0].email
if you want to read the JSON inside an array, use x[0].email.
You need to parse the JSON first:
var arr = JSON.parse(x);
var entry = arr[0].email;
[{"id":7,"message":"This is another test message","taker_id":"131","giver_id":"102","status":"0","stamp":"2016-08-11"}]
That's my response. I try to get a datum. I have tried data.id but it fails and returns undefined.
As I assume that you are working with a JSON string, you first have to parse the string into and JSON object. Else you couldn't reach any of the properties.
parsedData = JSON.parse(data);
Then you can get your property:
parsedData[0].id
This seems to work just fine
var data = [{
"id":7,
"message":"This is another test message",
"taker_id":"131",
"giver_id":"102",
"status":"0",
"stamp":"2016-08-11"
}];
console.log(data[0].id);
https://jsbin.com/jewatakize/
if you just want to get the id from this one object then data[0].id will work just fine.
If you expect to have multiple objects in that same array then you can use a loop.
for example if this is angular you can do:
<div ng-repeat='info in data'>
<p>{{info.id}}</p>
</div>
This will allow you to iterate through multiple objects within the array and get all id's.
The problem here is that you have here an array of objects, and you are trying to access it without indexing.
You should first parse it using and then access the object by indexing
let objects = JSON.parse(data)
console.log(objects[0].id)