Object to JSON angularjs - javascript

I have this method
function submit() {
var JSONObject = {
"name":$rootScope.name,
"surname":$rootScope.surname,
"email":$rootScope.email,
"review":$rootScope.review
};
debugger;
var Results = UniversalService.PostReview(JSONObject);
}
which I want to post to database later and I get error: SyntaxError: Unexpected token o in JSON at position 0
As I understood I get incorrect JSON format as my console.log input shows :
Object {name: "hh", surname: "hh", email: "kal#gmail.com", review: "fre"}
Instead it should show : "name": "hh" info like this right? How do I change that?

Related

convert string property from api to JSON object

i'm making a call to an api which returns data to the front end in this format
{ name: 'Fred',
data: [{'name': '"10\\" x 45\\" Nice Shirts (2-pack)"', 'price': '$30.25'}]
}
the data property is return as a string and i'm trying to use JSON.parse(response.data) to get it as as array so i can use *ngFor to do iteration but i'm always getting an error
Unexpected token ' in JSON at position 2
SyntaxError: Unexpected token ' in JSON at position 2 (at zone.js:1262:1)
at JSON.parse
Your backend is not returning a valid json. It should be:
{"name": "Fred", "data": [{"name": "\\"10\\\\\\" x 45\\\\\\" Nice Shirts (2-pack)\\"", "price": "$30.25"}]}

How to passing an Object from HTML to JavaScript

How to convert html data attribute into JavaScript Object?
I try to use JSON.parse() but it got some error
There is result of my String Data
{
_id: 5f71ea3362749a305427a012,
name: 'House With Beauty Backyard',
__v: 0
}
When i use JSON.parse() it return error
VM5246:2 Uncaught SyntaxError: Unexpected token _ in JSON at position 4
My EJS Code
<i class="fas fa-edit"></i>
My JS Code
<script>
$('#dataTable .btn-update').on('click', function () {
let dataString = $(this).data('category');
console.log(dataString);
let dataJson = JSON.parse(dataString);
console.log(dataJson);
$('#update-modal').modal('show');
})
</script>
Use JSON.stringify. Try this code:
let dataJson = JSON.parse(JSON.stringify(dataString));
console.log(dataJson);
JSON requires quotes ("") for keys and string values.
This would be your "String Data" with valid form:
{
"_id": "5f71ea3362749a305427a012",
"name": "House With Beauty Backyard",
"__v": 0
}
The parsing error will disappear, if you change your data format to valid JSON.(RFC 8259)
Here you can find out more about JSON:
https://tools.ietf.org/pdf/rfc8259.pdf

Fetching JSON Data that doesn't have key in framework 7 v2

My JSON Data
[
{
"ID": 000,
"pass_Word": "abc",
}
]
I want to get the value of ID from this JSON.
My Current Code:
var Data = {"User_ID" : login_id, "password" : password};
JSON.stringify(Data);
app.request.post('http://URL?type=json', Data , function (data) {
console.log(data.ID);
});
From the above code I am getting undefined in response.
Please help me out in this.

JSON.parse: expected ',' or '}' after property value in object

I keep getting this error message when I load my human.json file via AJAX.
The whole error message reads
JSON.parse: expected ',' or '}' after property value
in object at line 2 column 22 of the JSON data.
I looked online for it, and there have been people who had similar error messages, however, they are not calling via AJAX.
In addition to that, they are not nesting arrays within objects within objects. I think that is the reason why I am getting this error message. Are you not allowed to nest that many properties into each other?
Here's my AJAX code:
var request = new XMLHttpRequest();
request.open('GET','human.json');
request.onreadystatechange = function() {
if (request.readyState == 4 && request.status == 200) {
var obj = JSON.parse(request.responseText);
console.log(obj);
}
}
request.send();
and my human.json file:
{
"sex":{
"male":{"fname":["Michael", "Tom"]},
"female"
},
"age":[16, 80],
"job":[]
}
Your object isn't valid JSON. Specifically at the part:
,"female"}
A JSON property must have a value. Maybe that should that be:
,"female":{}}
or:
,"female":null}
Your JSON file has a syntax error. The following is reformatted to highlight the error:
{
"sex":{
"male":{"fname":["Michael","Tom"]},
"female" <----------------- SYNTAX ERROR
},
"age":[16,80],
"job":[]
}
In JSON, objects have the syntax:
{"name" : "value"}
The syntax {"foo"} is invalid according to JSON spec. Therefore you need to provide some value for the female attribute:
{
"sex":{
"male":{"fname":["Michael","Tom"]},
"female":{}
},
"age":[16,80],
"job":[]
}
Your JSON is indeed invalid.
{
"sex": {
"male":{
"fname": ["Michael","Tom"]
},
"female" ## Here is the problem
},
"age": [16,80],
"job": []
}
Perhaps change that line to:
"female": {}
It all depends on what you're wanting to do
your "female" is error, need a key or value
you can change the json file to
{
"sex":{"male":{"fname":["Michael","Tom"]} ,"female":null},
"age":[16,80],
"job":[]
}
JSON file used by you is invalid json. JSON is a collection of name/value pair. Each key in the JSON should contain value. In your case, key "Female" doesn't have any value. Below shown is the valid JSON format.
{
"sex": {
"male": {
"fname": ["Michael", "Tom"]
},
"female": "XXX"
},
"age": [16, 80],
"job": []
}

Update JSON with jquery error

I'm trying to update a JSON file with the value of a textarea using jquery push. I'm receiving the following error: " JavaScript runtime error: Unable to get property 'push' of undefined or null reference"
My jquery:
function submittedMsg(ctx) {
var id = $('.msg-input form').attr('id');
var newMsg = $('.msg-input textarea').val();
var url = "/ajax.aspx?vtl=ajax-conversation-json&cv=" + id;
$.getJSON(url, function (messageString, message) {
var message = [];
message.push({
msgcontent: newMsg,
sendname: sendRname,
mbrhref: mbrUrl,
datetime: ""
});
});
}
My JSON:
{
"messageString" :
[
{ "subject": "hello",
"msgstring": "5",
"unread": "1",
"datetime": "Oct 1 2013 9:59PM",
"orderid": "17",
"recipient": [
{
"mbrname": "Jane Doe",
"mbrhref": "/profile.aspx?mem=1227"
},
{
"mbrname": "John Smith",
"mbrhref": "/profile.aspx?mem=1337"
}
],
"message": [
{
"datetime":"2013-10-01T21:59:33.063",
"sendname":"Jane Doe",
"mbrhref":"/profile.aspx?mem=1227",
"msgcontent": "<p>Hi. I would like to talk with you about Dwarf Beryl Beauty</p>"
},
{
"datetime":"2013-11-26T16:29:17.037",
"sendname":"John Smith",
"mbrhref":"/profile.aspx?mem=1337",
"msgcontent": "Tough luck."
}
]
}
]
}
I don't necessarily need to use push to update the JSON file if there is a better way, I'm open to suggestions. I've verified my URL path is correct. Am I just missing something obvious? I'm new to JSON and only have passable jquery skills. Help!
Thanks in advance for any direction.
Try to use:
data.message.push
instead of:
data.messageString.message.push
Ah I see the issue, you have a local var and parameter of the same name message:
$.getJSON(url, function (messageString, message) { //here is param message
var message = []; //here is a local var parameter
message.push({ //this is probably referencing the parameter which is not an array or object that supports .push
Instead:
$.getJSON(url, function (data) { //I renamed the param to be more consistent with documentation, although it doesn't really matter, just will generate confusion
data.messageString.push({ //modify the json we were passed in the data param

Categories