json value getting it as "[object Object]" - json

In angularjs ng-route since my params are dynamic i'm passing it as json like as shown below
$scope.filmNames= {
'films': {
'film': [{
filmName: 'ABCD',
filmYear: '123'
}, {
filmName: 'BCD',
filmYear: '145'
}, {
filmName: 'DEF',
filmYear: '128'
}]
}
};
'.../index.html#/admin?jsonObj='+$scope.filmNames
ans it succesffully sending as like
http://localhost:8000/index.html#/admin?jsonObj=[object Object]
but at the controller receiver when i tried to get it through using $routeParams like
var jsonObj= $routeParams.jsonObj;
console.log(JSON.stringify(jsonObj));
It is been printing it as "[object Object]", instead of the values
can anyone please tell me solutions for this

When you convert an object to a string using concatentation, you get: [object Object]. What you actually want is to convert the object to json.
'.../index.html#/admin?jsonObj='+JSON.stringify($scope.filmNames)
Then, at the receiver, you'll want to parse it, not stringify it.
console.log(JSON.parse(jsonObj));
I suggest moving away from calling json strings "json objects", it just leads to confusion. json strings don't always represent objects.

Related

how to pass array as an parameter using routing in angular 4

I am working on an angular project I want pass an array object as an parameter to another page through rouuting my function is as follows
Goto(item)
{
console.log(item.numtopics);
this.router.navigate(['/numbersystem'], { queryParams: item.numtopics});
}
and getting the data as follows
constructor(private route: ActivatedRoute) {
this.route.queryParams.subscribe( param => {
console.log(param);
});
}
but in console I am getting data like this
{0: "[object Object]", 1: "[object Object]", 2: "[object Object]"}
I am not getting the array data properly
any help.
Thank You !!
If you want to get this passed array, you should use:
console.log(param.numtopics)
or you can also pass array like this:
this.router.navigate(['/numbersystem'], { queryParams: item.numtopics});
and then you can get this array like you tried:
console.log(param)
Refer the official documentation, how queryParams are passed.
The reason why you get data like {0: "[object Object]", 1: "[object Object]", 2: "[object Object]"} is because it's an object.
Try passing JSON.stringify(item.numtopics) to the queryparams. This will pass a string to the queryParams. Also you can mention the NAME of the query(Refer Doc). For exapmle :
https://www.yourdomain.com/search?NAME=stringifiedString
Before you pass the stringified array you may want to encrypt the data so you can use some encryption algorithm. Thereafter, in the component where you retrieve the params you can again use JSON.parse(paramsData) to get array back from the string.
In case if you've used encryption, you should decrypt first and then use JSON.parse()

Single value from json array using nodejs

so i have this data which is outputted from stringify(doc)
{
"id": "8467fdae-c38c-4b6e-9492-807d7c9eb97e",
"message": "send to nodejs"
}
but im not sure how can i go ahead in getting a single value from it, e.g message. Any help would be appreciated as ive tried different methods and they seem to not be working
After you use stringify method the value you have is string.
So You can not get single property from it, except you convert it become object again and get property it. Like this:
object = {
"id": "8467fdae-c38c-4b6e-9492-807d7c9eb97e",
"message": "send to nodejs"
};
// JSON string is value which you get after use Stringify method
jsonString = JSON.stringify(object);
// Convert jsonString to object again and get message property
message = JSON.parse(jsonString).message
stringify() will convert json to string.
Before running stringify(), doc is already json and you can get message by doc.message.
after running stringify(), the result will be string, if you want to get json back, you can use JSON.parse()

How to pass a JSON string to API Gateway as query Parameter

How can i pass a JSON object like {val: 1} to my Lambda function as a query parameter?
Following Standardized way to serialize JSON to query string? i URL-encoded my JSON object and requested the following: mysite.com/path?json=%7B%22val%22%3A%201%7D
As requestTemplates i tried the following two options
"json": "$input.params().querystring.json"
"json": "$util.parseJson($input.params().querystring.json)"
But i got this error message:
{"message": "Could not parse request body into json: Unexpected
character (\'v\' (code 118)): was expecting comma to separate OBJECT
entries\n at [Source: [B#37a2970e; line: 1, column: 47]"}
If i do not encode the query string so: mysite.com/path?json={"val":1} i get a 400 error
Your mapping template is producing no valid JSON, you have to wrap the key/value pair in curly braces
I guess you do not want to wrap the value in quotes, it will be a string and no object otherwise
You can use $util.urlDecode to decode URL encoded strings
Your mapping template should look like this:
{"json": $util.urlDecode($input.params().querystring.json)}
For mysite.com/path?json=%7B%22val%22%3A%201%7D this mapping template will result in the following JSON:
{
"json": {
"val": 1
}
}
If you want the querystring JSON to be passed on the root level to your Lambda function use this as a mapping template:
$util.urlDecode($input.params().querystring.json)

Parsing JSON value with SwiftyJSON (and Alamofire)

I am trying to parse a single value from a REST web service that I am testing.
I understand how to make the call and I see the JSON response in the Output window.
let request = Alamofire.request(.GET, "http://IP:PORT/jsonTest", parameters: ["s": "Ping?"])
.responseJSON{(_,_,data,_) in
var json = JSON(data!)
println(json)
The Console Output shows me:
{"NewDataSet":[
{"Table1":[
{"Column-A":"FirstA",
"Column-B":"FirstB"
},
{"Column-A":"SecondA",
"Column-B":"SecondB"
},
{"Column-A":"ThirdA",
"Column-B":"ThirdB"
}
]}
]}
What I would like to do now, is to display only the first value from Column-A - which in this example would be "FirstA".
I've been trying to use a code like this, but so far I am not getting anywhere...
println(json[0][0]["Column-A"].stringValue)
Any pointers much appreciated!
json["NewDataSet"][0]["Table1"][0]["Column-A"].stringValue
This is what you want. This is because your json starts with a dictionary and is formatted as dictionary>array>dictionary>array>dictionary. Note that json dictionaries are noted by { : , : } while arrays are noted as [ , ].

Convert JSON object to array?

I have this on my console on firebug,
[Object { fa_id="1167535", f_id="1000", loc_type="6", more...}, Object { fa_id="1167535", f_id="1000", loc_type="6", more...}]
it is data from the server side. Now, how would I convert this into array such that this data can be used on another file. I tried JSON.parse and jQuery.parseJSON but both did not work.
That isn't JSON it's a Javascript array of objects, not a string. My guess is that you've received this from a jQuery ajax call and you had the dataType : 'json' set so that jQuery has automatically parsed the JSON into this array.
To send it to a PHP script you can convert it back to JSON using:
var myString = JSON.stringify(data);
and then fire off an ajax call to the PHP script with that as the POST data:
var myString = JSON.stringify(data);
$.post('page.php', { data : myString }, function(){
console.log( "sent" );
});
In PHP you can decode it using:
$data = json_decode($_POST['data']); // <-- or whatever your post variable is named
foreach($data as $obj)
{
echo $obj->fa_id;
}
if you want to get an php array use this
http://php.net/manual/en/function.json-decode.php
The string you provided is not valid JSON.
[Object { fa_id="1167535", f_id="1000", loc_type="6", more...},
Object { fa_id="1167535", f_id="1000", loc_type="6", more...}]
In particular, the "Object" and "more..." strings cannot be interpreted by a JSON parser.
Assuming the object you are inspecting is a variable named foo:
console.log(JSON.stringify(foo));
Should print a valid JSON representation of your object to the Javascript console.