I have an string object like the following:
CustomData {
href: 'https://api.stormpath.com/v1/accounts/fdsq/customData',
createdAt: '2017-02-12T21:06:34.086Z',
modifiedAt: '2017-02-14T20:36:45.879Z',
ethereum_contract_address: '485dsq41g52fdsqqds',
ethereum_provider: 'proqxy53fdsq.yoicsfdsqfdsq.net:31gky6736' }
I am trying to convert this string to a JSON object that I can then use properly. But i can't seem to convert this to a simple string that I could then substring and then parse to JSON.
Here is what i have attempted:
var rawString = req.user.customData;
console.log(rawString);
var stringJson = String(rawString).substring(0, 11);
console.log(stringJson.toString());
var customData = JSON.parse(stringJson);
console.log(customData);
I unfortunatly get stcuk on the JSON.parse, it seems like the string String(rawString) does not actually convert it to a string but only retruns [object Object].
You should use
JSON.stringify(jsonData);
then just simply parse
JSON.parse(jsonString)
You need JSON.stringigy(obj); to get a JSON-object of your data. Heres the code:
var customData = {
href: 'https://api.stormpath.com/v1/accounts/fdsq/customData',
createdAt: '2017-02-12T21:06:34.086Z',
modifiedAt: '2017-02-14T20:36:45.879Z',
ethereum_contract_address: '485dsq41g52fdsqqds',
ethereum_provider: 'proqxy53fdsq.yoicsfdsqfdsq.net:31gky6736'
}
console.log(customData);
var stringJson = JSON.stringify(customData);
console.log(stringJson);
var customData = JSON.parse(stringJson);
console.log(customData);
CustomData is a JSON: It contains keys and values. JSON stands for JavaScript Object Notation. You confuse JSON with JSON string. From the former you can achieve the latter via
var JSONString = JSON.stringify(CustomData);
You can parse it via
JSON.parse(JSONString);
However, since your object is already a JSON, you should be able to use it properly, whatever that means in your scenario.
Related
I have a textarea element which takes object types as an input
like
{
name: "root",
backlog: [
{name: "log#1"},
]
}
Accessing the data returns it as a string
Is there a simple way to convert the String to that specific javascript object without using regex filters? Just removing the outer quotation marks?
use json5 or Relaxed JSON library.
here is example using json5 library
let string = ` {
name: "root",
backlog: [
{name: "log#1"},
]
}`;
let object = JSON5.parse(string);
console.log(object)
<script src="https://unpkg.com/json5#^2.0.0/dist/index.min.js"></script>
To convert a string to a JSON, use: JSON.parse([the input]).
And, to convert it back to a string: JSON.stringify([the input]).
If i understand you correctly :
const string = '{ name: "root", backlog: [{name: "log#1"}]}'
const jsonStr = string.replace(/(\w+:)|(\w+ :)/g, function(matchedStr) {
return '"' + matchedStr.substring(0, matchedStr.length - 1) + '":';
});
const result = JSON.parse(jsonStr)
console.log(result)
If you want to use a simple, native approach you could simply stringify and then parse it using the intrinsic JSON object, i.e. something like this:
let yourObject = JSON.parse(JSON.stringify(textareaVariable));
This works because stringify will essentially format the string into a JSON string with the appropriate syntax for a JSON document, which you can then parse into a standard object.
Convert a JavaScript object into a string with JSON.stringify().
Create a JSON string from a JavaScript object.
const obj = {name: "John", age: 30, city: "New York"};
const myJSON = JSON.stringify(obj);
console.log(myJSON);
//Output: {"name":"John","age":30,"city":"New York"}
Parse the data with JSON.parse(), and the data becomes a JavaScript object.
Creating an Object from a JSON String
const txt = {"name":"John", "age":30, "city":"New York"}
const obj = JSON.parse(txt);
console.log(obj.name + ", " + obj.age);
//Output:John, 30
You can simply use a JSON.parse call with some parameters to maintain the indetation.
const text = '{"firstName":"John", "lastName":"Doe", "address":{"city": "Tanger", "country": "Morocco"}}';
const obj = JSON.stringify(JSON.parse(text), null, 2)
console.log(obj);
Here is my problem
const data = "[\"https://i.imgur.com/PAYXC2n.jpg\",\"https://i.imgur.com/bEfjjxA.jpg\"]";
I want to convert this string to array,
expected result will be :
res =["https://i.imgur.com/PAYXC2n.jpg","https://i.imgur.com/bEfjjxA.jpg"]
I use it for mapping data in React JS project, so i need to convert this string to array.
Just parse it from JSON:
const
data = "[\"https://i.imgur.com/PAYXC2n.jpg\",\"https://i.imgur.com/bEfjjxA.jpg\"]",
parsed = JSON.parse(data);
console.log(parsed);
Do it:
const dataArr = JSON.parse(data);. This will parse as to object, which is your array (With out "")
I have JavaScript code below.
var data = "[{"PR_ID":23096,"P_ID":23014},{"PR_ID":33232,"P_ID":23014},{"PR_ID":33308,"P_ID":23014},{"PR_ID":33309,"P_ID":23014}]";
I need convert the string to an data by delete the "" surrounding the array stored as "data" in JavaScript so after convert it suppose like below:
var data = [{"PR_ID":23096,"P_ID":23014},{"PR_ID":33232,"P_ID":23014},{"PR_ID":33308,"P_ID":23014},{"PR_ID":33309,"P_ID":23014}];
How to make the convert?
To convert a JSON object to Javascript object use:
var data = '[{"PR_ID":23096,"P_ID":23014},{"PR_ID":33232,"P_ID":23014},{"PR_ID":33308,"P_ID":23014},{"PR_ID":33309,"P_ID":23014}]';
JSON.parse(data);
But first change the double quote to single quote, otherwise the JSON object wont be a valid JSON.
After this you can walk the array in the following way:
var jsonParsed = JSON.parse(data);
for(var val in jsonParsed) {
if(jsonParsed.hasOwnProperty(val)) {
// do something with the values
}
}
I want the array in an object to be a string. can someone pl help? So im passing an array into an object and Im expecting var expectedResultForObject2 = 'name=bob&age=23&kids=billy&kids=bart&kids=bort'; how can this be attained ?
it("should serialize an object with another object/array in it", function() {
var object2 = {
'name': 'bob',
'age': 24,
'kids': [ 'billy', 'bart', 'bort' ]
};
var expectedResultForObject2 = 'name=bob&age=23&kids=billy&kids=bart&kids=bort';
expect(NUUI.Utils.serializeForQueryString(object2))
.toEqual(expectedResultForObject2);
});
You need serialized result from an object. This can be achieved with jQuery.param()
uri = $.param(object2);
More information on this, you can find in jQuery Manual.
Alternatively, you can make an array object a string simply with .join() method of the array
string = array.join('');
This question is heavily answered in SO
jQuery serialize an object?
I'm stringyfing an object like:
"{'foo': 'bar','task':[{'task1':'task1'}]}"
How can I turn the string back to an object?
Try the following solution to parse the stringified JSONObject.
// Stringified JSON Object
var stringifiedJSONObject = '{'foo': 'bar','task':[{'task1':'task1'}]}';
// Parsing string object to json
var jsonObject = JSON.parse(stringifiedJSONObject);
// Get the inner array. The below object is a JSON Array of Objects
var innerArray = jsonObject.task;
// displays the value of task1
alert(innerArray[0].task1);