Convert String Object to real Object (without quotes) [closed] - javascript

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 months ago.
Improve this question
I'm having some trouble converting an object of type string to a real object.
Here is my example (it's a string):
{ id: true,translations:{id: true,text: true,language:{id: true,languageCode: true}},createdAt: true }
When I JSON.Stringify it I get:
"{ id: true,translations:{id: true,text: true,language:{id: true,languageCode: true}},createdAt: true }"
And when I JSON.Parse it I get the same result as the example above, I also did console log the type of it and I get it's of type string
Any Ideas on how I should convert it to a real object?
Thanks in advance

By far the easiest way to handle this is using the JSON5 library
const string="{ id: true,translations:{id: true,text: true,language:{id: true,languageCode: true}},createdAt: true }";
const object = JSON5.parse(string);
console.log(object)
<script src="https://unpkg.com/json5#2.2.1/dist/index.js"></script>

Related

How to parse object array that has been stringified using backslashes [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 days ago.
Improve this question
I have been searching for the correct regex for converting this string to JSON. It works if there is a single object in the array, but I believe the comma is messing it up somehow.
const json = "{\"userId\":44, \"userName\": \"Jim Coleman\"},{\"userId\":33515, \"userName\": \"Grace Mamaradlo\"}";
const obj = JSON.parse(json.replace(/("[^"]*"\s*:\s*)(\d{17,})/g, '$1"$2"'));
console.log(obj);
There was nothing wrong except that the list of objects was not contained within square brackets
const json = "{\"userId\":44, \"userName\": \"Jim Coleman\"},{\"userId\":33515, \"userName\": \"Grace Mamaradlo\"}";
console.log(JSON.parse(`[${json}]`))

How can I parse a stringified array of arrays (JavaScript)? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 months ago.
Improve this question
I have a string which represents an array. The items in the array can be characters, or arrays. So a string might look like: [a,b,[[],[d]],[],[[[]]],[[c,[t,s]],b]]
And I want to parse that out so it's a proper array with subarrays etc
So "[a,b,[c,[d,e],[]]]" would become:
[ 'a',
'b',
[ 'c',
['d','e'],
[]
]
]
Is there an easy way to do this in JavaScript? Eg some kind of array equivalent of JSON.parse? I tried JSON.parse but it throws an error (not unexpectedly).
You would have to process it to convert it into a format that JSON.parse would be able to handle. Your test case is simple so it is possible with an easy regular expression.
const str = "[a,b,[c,[d,e],[]]]";
const obj = JSON.parse(str.replace(/([a-z]+)/gi,'"$1"'));
console.log(obj);

How do I remove double quote "" from JSON Value [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I am doing my Javascript. And just imported JSON and printed it out with JSON.stringfy
I am using that site because the JSON contains the URL which I want to link.
I am not getting what I really need as the output.
description": "www.site.com"
This is what I get in my console:
"www.site.com"
This is what I actually want, and remove the quotes.
www.site.com
Please help!
Am not sure what your requirement is but you can do this.
Try Replace function;
const obj = {description: "www.site.com"};
JSON.stringify(obj.description).replace(/"/g,"");
var someStr = 'He said "Hello, my name is Foo"';
console.log(someStr.replace(/['"]+/g, ''));
That should do the trick... (if your goal is to replace all double quotes).

Remove First and Last double Quotes from "["Morning Shift","Day Shift"]" [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I want to remove First and Last Quotes from this array Using JavaScript:
"["Morning Shift","Day Shift"]"
After Remove quotes output look like this
["Morning Shift","Day Shift"]
You can try using JSON.parse():
The JSON.parse() method parses a JSON string, constructing the JavaScript value or object described by the string.
Demo:
var data = `["Morning Shift","Day Shift"]`;
data = JSON.parse(data);
console.log(data);

How to check that this empty object caused by empty json output is really empty? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a json web service that returns simply [].
Then, I have a javascript Object variable json_var that contains this json output. When json returns empty [], json_var is undefined. How do I check from the content of json_var that the json output is empty []?
check for the length of the response. As it was mentioned [] refers to an empty array
var test = [];
console.log(test.length); // returns 0
JSFIDDLE

Categories

Resources