i have data which is an array like:
arr=["[[name,address,contact],[name1,address1,contact1],[name2,address2,contact2]]"]
How to change this array in json and get those value in a table in react.
Use json.parse
arr=["[[name,address,contact],[name1,address1,contact1],[name2,address2,contact2]]"]
let datax = JSON.parse(arr[0].replace(/(\w+)/g, '"$1"'));
console.log(datax)
Related
i want to convert a json array (string) to javascript array using just some specific values. The json array is :
[{"id":47,"libelle":"famille de test"},{"id":1,"libelle":"GEOLOCALISATION"},{"id":4,"libelle":"OUTILS"},{"id":2,"libelle":"PROPRETE"},{"id":3,"libelle":"URGENCE"}]
and i want to get something like this ["famille de test", "GEOLOCALISATION", ...] using just libelle values.
I tried to use $.map but didn't work out.
The map implementation should work:
var jsonStr = '[{"id":47,"libelle":"famille de test"},{"id":1,"libelle":"GEOLOCALISATION"},{"id":4,"libelle":"OUTILS"},{"id":2,"libelle":"PROPRETE"},{"id":3,"libelle":"URGENCE"}]';
var arr = JSON.parse(jsonStr);
var libelle = arr.map(function(x) { return x.libelle; });
First, you must turn your JSON string into a JavaScript Array by using JSON.parse(yourJSONString). After that, it is a simple JavaScript array and you can use the map method you tried
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
I am trying to use codebird to get some data from twitter. I have a script in JavaScript.
My problem is that codebird's reply is an object and not a JSON. So I can't use eval() to get parse the json text in an array.
I just need to acces the json data.
Thank you in advance
var cb = new Codebird();
cb.setConsumerKey("", "");
cb.setToken('','');
cb.__call(
"search_tweets",
"q=Twitter",
function (reply) {
data = eval(reply) //parse the returned JSON to array
}
}
);
If you need to convert a JavaScript object into a JSON string you can use
data = JSON.stringify(reply)
But usually it's better to deal with object itself - e.g. you can iterate thru it's properties (creating your own array if needed)
I have a form and would like to append the contents of it to an existing array.
I am using JSON.stringify( $('#myForm').serializeObject() ) to convert my form elements into json objects.
The form has user info that i would like to append into myArr then append this list into an existing array.
myArr is populating fine, its just appending that into existingJsonArray i seem to be having problems with.
I saw this but since JSON.stringify creates the full json array would i need to knock out the [{ and }] ?
Is this the correct approach?
var existingJsonArray = [];
var myArr = [];
myArr.unshift( JSON.stringify( $('#myForm').serializeObject() ) );
existingJsonArray.unshift(myArr);
Please notice that JSON is the string representation of objects - and not suited well for manipulating them.
var array = [], // an Array literal in JavaScript code
formObject;
formObject = $('#myForm').serializeObject(); // an object representing the form
array.unshift([formObject]); // not sure why you need the nested array
// create string containing JSON representation of the array:
var jsonString = JSON.stringify(array);
I have the following JSON value pushed from the server.
result=[{"id":1492,"name":"Delhi"},
{"id":109,"name":"Coimbatore"},
{"id":576,"name":"Konni"},
{"id":525,"name":"Kottayam"}
]
I know how to convert JSON Array to Javascript Array.Here is the code below(got from stackoverflow)
var locations = [];
$.each(result, function(i, obj) {
locations.push([obj.id,obj.name]);
});
I want to convert this JSON Array to a JavaScript Object Array so that I can access the values as jarray[0].id which will give me the value 1492. Please advice
You don't need to do anything to the result array. Just use result[0].id and it will evaluate to 1492.