how to insert text into json using jquery/javascript [duplicate] - javascript

This question already has answers here:
Serializing to JSON in jQuery [duplicate]
(11 answers)
Closed 9 years ago.
How to insert text into json array using jquery/javascript .
supposing I have data as
some numbercodes in a text file format 123, 456, 789
I want to get them in a json array format using javascript/jquery.
var nuumbercodes = [ "123","456","789" ];

If you have a well formatted text string with comma separated numbers, like this '123,456,789'
This should have no spaces or tabs,
Then you can convert it simply into a JavaScript array.
var myTextwithNuumbercodes='123,456,789';
var numbercodes=myTextwithNuumbercodes.split(',');
returns ['123','456','789']
if you have a JSON string like this '[123,456,789]' then you get a javascript array by calling JSON.parse(theJSONString)
var numbercodes=JSON.parse('[123,456,789]');
returns [123,456,789]
notice the "[]" in the string ... that is how you pass a JSON array
toconvert it back to a string you can use JSON.stringify(numbercodes);
if you have a total messed up text then it's hard to convert it into a javascript array
but you can try with something like that
var numbercodes='123, 456, 789'.replace(/\s+/g,'').split(',');
this firstly removes the spaces between the numbers and commas and then splits it into a javascript array
in the first and last case you get a array of strings
u can transform this strings into numbers by simply adding a + infront of them if you call them like
mynumbercode0=(+numbercodes[0]);// () not needed here ...
in the 2nd case you get numbers
if you want to convert an array to a string you can also use join();
[123,456,789].join(', ');

Assuming your data is in a string, then split it on commas, use parseInt in a for loop to convert the string numbers into actual Numbers and remove the whitespace, then JSON.stringify to convert to JSON.

You could use .push() push values at the end of an array. After that you could use JSON.stringify(nuumbercodes) to make a JSON string representation of your Array.

Related

parse a string that contains array with strings

I am struggling to parse a string that contains array. And the array also contains list of arrays.
Each of the array contains string.
code here
let a = "[[48934, 'Danial Brendon', 'developer'],[48934, 'Nicki Lopez', 'developer']]";
console.log(JSON.parse(a))
I tried using JSON.parse() but did not work, may be because JSON.parse() also want to parse the string.
I am having difficulty with this even this looks simple. I could not find any similar question/answer like this.
Thanks.
To JSON parse , you need double quotes instead of single. like this ...
let a = '[[48934, "Danial Brendon", "developer"],[48934, "Nicki Lopez", "developer"]]';
console.log(JSON.parse(a));

How to convert JSON Parse object property into array?

In mysql database, "diagID" is saved as json_encoded(array). Now i need it to retrieve in ajax success.
How to convert JSON parse data into array, as it's showing string?
var ajaxResponse= {
"id": "123",
"diagID" : "['101','125','150','230']"
}
typeof(ajaxResponse.diagID)
= string
In javascript typeof(ajaxResponse.diagID) shows string. How to convert it into array?
Decoding it in php would make most sense
$diagID = json_decode($diagID, true);
Then when you json_encode() the whole response it won't have the extra wrapping quotes.
Note however that the strings in array have single quotes which are not valid json and need to be replaced with double quotes before they can be parsed in either language

How to convert a string to an array in javascript?

I have an input on a form which stores values as an object.
jQuery('#inputId').val()
returns something like
'[{"Id":"123","Name":"A","PathOfTerm":"A","Children":[],"Level":0,"RawTerm":null},{"Id":"234","Name":"B","PathOfTerm":"B","Children":[],"Level":0,"RawTerm":null}]'
as one single string. Is there any way to either prevent this from automatically converting to a string (maybe not using .val?) or to convert this from a string to something I could work with?
Here you go
var array = JSON.parse(jQuery('#inputId').val());

Convert String of certain format to JSON in javascript

I have a string of the format
var str = "{key1=value1, Key2=value2}"
I need to convert this into a json object to be able to iterate through it.
Any suggestions on how this can be done? there can be any number of keys
You need first to "JSONize" this string you are getting so it can be converted to a JavaScript object using the JSON class. My guess, if the string has always this format ({key=value, ...}), is that you could parse it first like this:
var parsedString = yourString.replace(/(\b\S+\b)=(\b\S+\b)/g, '"$1":"$2"')
This way, from this: "{key1=value1, Key2=value2}" you get this: '{"key1":"value1", "Key2":"value2"}'.
Then, as someone suggested, just use JSON.parse(parsedString) to get your JS object.

get JSONS from a single string

i get a single String value as
"{"Link":"","DefaultValue":"","Content":"LONDON"}, {"Link":"","DefaultValue":"","Content":"United Kingdom"}"
which contains two jsons . how can i get each json and put in into an array or something in javascript/jquery ?
Please suggest the best way.
The String that you posted isn't valid JSON format. If it is an array of two objects, it should read:
'[{"Link":"","DefaultValue":"","Content":"LONDON"}, {"Link":"","DefaultValue":"","Content":"United Kingdom"}]'
Note the single quotes at the beginning and end, so that Javascript doesn't confuse the double quotes in the JSON and can parse them correctly.
Also note the brackets [] around the whole thing, that tells the parser that it is an array of objects.
You can read the new string into an object array like this:
var str = '[{"Link":"","DefaultValue":"","Content":"LONDON"}, {"Link":"","DefaultValue":"","Content":"United Kingdom"}]';
var arr = JSON.parse(str);

Categories

Resources