How to convert JavaScript object containing json string - javascript

I am having problem parsing JSON string into JS object .Please tell how to convert JavaScript object :
Object {d: "[{"worker_id":1,"worker_name":"Shivank"}]"}
into
Object { d: [{ "worker_id": 1, "worker_name": "Shivank" }] }
I have tried using
JSON.parse(data)
and
var dataFinal = JSON.stringify(data);
var d1 = eval('(' +dataFinal+ ')');

You have an object where one property value contains JSON so you only need to convert that value
Try
data.d= JSON.parse(data.d);

Assuming your data is as below, which d is having stringified json data
var data = {d: "[{\"worker_id\":1,\"worker_name\":\"Shivank\"}]"}
console.log(data);
You can parse JSON and assign to d key
data.d = JSON.parse(data.d)
console.log(data); // required output

Related

How to pass variable to JSON without printing it as String

How to pass variable to JSON object and print it like JSON object?
I simply want to pass variable value in JSON and print it like JSON which can also be used in console.table(obj)
With Stringify:
var name = "someName";
const json = JSON.stringify('{"result":'+name+', "count":42}');
const obj = JSON.parse(json);
console.log(obj);
Without stringify
var name = "someName";
const json = '{"result":'+name+', "count":42}';
const obj = JSON.parse(json);
console.log(obj);
Using \"variableName\" it gets value in \"...\" and not the variable value
var name = "someName";
const json = '{"result":\"name\", "count":42}';
const obj = JSON.parse(json);
console.log(obj);
Solution:
var debugJSON = [];
var section_number = 1;
var i = 25;
var x = section_number-i;
tempJSON = {
"section" : section_number,
"index" : i,
"fieldname" : x,
"isValid" : "not required"
};
debugJSON.push(tempJSON);
console.log(debugJSON);
//console.table(debugJSON); //Problematic on Chrome Browser and Stack Overflow
JSON is a text representation of some data structure.
Unless you write a JSON encoder (and you don't), you don't have any reason to produce JSON manually. Attempting to generate JSON using string concatenation fails if the variable strings (name in this case) contain quotes or backslash. Escaping them could produce valid results but the code becomes bloated and difficult to read without any gain.
JavaScript provides the method JSON.stringify() to produce a JSON from any data and this is the best way to generate JSONs.
All you have to do is to build the data structure that you need to encode. In your example, the data structure is an object:
let name = "someName";
const original = {
result: name,
count: 42,
}
// encode
const json = JSON.stringify(original);
console.log(json);
// decode
data = JSON.parse(json);
console.log(data);
Running the code snippet here in page is not relevant because the output that it produces looks similar for both calls of console.log().
Run the code using Node.js or run it in the browser's console to see the values of json (it is a string) and data (it is an object).
A JSON is quite literally a JavaScript object, so you don't treat it as a string, you work with objects, which you can later stringify if need be.
In your case, you don't want to pass a variable in a string, you should pass it in an object like so:
// Declaration of name
let name = 'someName';
// Create object and store name
const json = {
result: name,
count: 42
};
// Log object as JSON and as string
console.log(json);
console.log(JSON.stringify(json));
The first log returns your object as exactly that, an object, the second log, converts it into a string to do so as you please with it!

Fetch key inside a json object in php jquery

I am returning a json data to jquery using php. what i want is to access the keys inside the json. i followed a tutorial and used json stringify but im not able to access the keys.
json data :
[
{"id":"1","movie_name":"spiderman","releases_on":"20th August 2018"},
{"id":"2","movie_name":"batman","releases_on":"21st August 2018"},
{"id":"3","movie_name":"fast and furious 6","releases_on":"22nd August 2018"}
]
code in jquery:
var json = data;
var obj = JSON.stringify(json);
console.log(obj[1].id);
you have to use JSON.parse(json).
then, you can do it.
JSON.stringify() will take your JSON object and convert it to a JSON string (here which is response).
JSON.parse() will take stringified JSON and convert it to object so that you can access via . operator
for example,
var a = { name: "John", age: 20 }
console.log(typeof a) // object
var out = JSON.stringify(a)
console.log(typeof out) // string
var res = JSON.parse(out)
console.log(typeof res) // object
I hope you got it....

Store Javascript Delta object in database

im using the Quill text editor which returns a Delta object representing the content.
https://quilljs.com/
i want to store this in a database but am unable to convert it to a string/json type format
deltaobject.toString
The above returns
[object object]
Use the native JSON converter...
var json = JSON.stringify(deltaobject);
then to convert it back to an object...
var obj = JSON.parse(json);
var json = JSON.stringify({ prop1: "Value1", property2: 123.456 });
console.log(json);
var obj = JSON.parse(json);
console.log(obj);

Why I can't parse JSON in JavaScript?

JSON contains one object:
results[0] = { 'MAX(id)': 1 }
And this code doesn't work:
var text = results[0];
var obj = JSON.parse(text);
console.log(obj.MAX(id));
results[0] is already an object type
You can parse only from string to object like this:
JSON.parse('{ "MAX(id)": 1 }');
Your object is already a JSON. You don't need to parse it.
To access MAX(id) property, you can use [] notation as follows:
results[0] = { 'MAX(id)': 1 };
console.log(results[0]['MAX(id)']);
Your result[0] is a real javascript object. JSON.parse transforms text into objects, so you can't parse other objects with it.
var results = { 'MAX(id)': 1 };
//var text = results;
//var obj = JSON.parse(text);
alert(results['MAX(id)']);

how to convert a json array into javascript array

the server response data is:
{virus: "[["0家":1907],["1~3家":618],["4~10家":436],["11~20家":1046],["20家以上":924]]"};
then I get this object value by key virus
how to convert this value to javascript array
var value = '"[["0家":1907],["1~3家":618],["4~10家":436],["11~20家":1046],["20家以上":924]]"';
Use JSON.parse(); to convert the XHR response to a JavaScript array.
More info: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse
If you use jQuery, :
var response = your data;
var obj = $.parseJSON(response);
alert(obj. virus); //true
alert(obj.count);

Categories