Angular 8 Parse Json Object - json

A rather basic Angular 8 question I assume, but here goes:
I am using http.get() to pull a JSON response from the server. If I save the response as a variable, e.g. jsonResponse, and put that response into my HTML component as {{jsonResponse | json}} I get the anticipated nice json object, e.g. { 'hithere': {'this is me' : 'no really'}}.
But I cannot seem to get the same nice Object to loop through in the TS/JS side of things. Can someone point me in the correct direction so that I can do something like. jsonResponse.hithere?

Try passing the object using JSON.parse(obj) and then loop/traverse through its leafs.

Related

node-RED json object - how to extract a value example code

I am trying to extract values from a json file. I can do this with function node when it is posted from a webhook on my github page. But from another site I cant seem to get this working. This is what I use to get the value on github to test.
var msg2 = {payload: msg.payload.comment.user.login };
return msg2;
This is the code I get from the other server. I just want two value from this below. When I save the incomming message directly with a save node this is whats in the json file.
{"payload":"{\"id\":33,\"status\":\"completed\",\"totalAmount\":103,\"tipAmount\":0,\"feeAmount\":4,\"settleAmount\":99,\"requiredAmount\":null,\"date\":\"2017-09-22T06:51:33Z\",\"snapCode\":\"To9tQhiV\",\"snapCodeReference\":\"06ce2405-1688-4cb7-aa26-6df25c053f8c\",\"userReference\":\"\",\"merchantReference\":null,\"statementReference\":null,\"authCode\":null,\"deliveryAddress\":null,\"extra\":{\"merchantReference\":\"AB11FFBC56\"}}"}
I just need totalAmount and merchantReference in the extra field.
Please assist and thank you in advance.
Your payload is a JSON String. To access its contents you must first parse it to a JavaScript Object.
Add a JSON node in front of your Function node - that will convert msg.payload for you.

Angular and JSON, having trouble parsing for ng-repeat

I'm horribly sorry if there is a post for this, I tried to search but didn't find a answer.
Problem:
I'm calling a web service and receiving not so well formed JSON data from a Dynamics Nav service:
JSON:
"[{\"type\":\"2\",\"number\":\"VHT3866\",\"location\":\"Delta\",\"destinationNo\":\"\",\"contactName\":\"Jesus\",\"shipToName\":\"Lord jesus\",\"highPriority\":\"false\",\"hasComment\":\"true\",\"assignedTo\":\"\",\"source\":\"\"},{\"type\":\"2\",\"number\":\"VHT3866\",\"location\":\"Delta\",\"destinationNo\":\"\",\"contactName\":\"Jesus\",\"shipToName\":\"Lord jesus\",\"highPriority\":\"false\",\"hasComment\":\"true\",\"assignedTo\":\"\",\"source\":\"\"}]"
I then take this JSON and use angular.fromJson(json) to get it properly.
It doesn't seem to change into an array of javascript objects, but just plain text.
However if I take the same JSON and just put it manually in like this:
var json = angular.fromJson(stringfromserver);
It turns into a proper javascript object and ng-repeat throws no error.
I found an answer on Quora:
--- Le Batoure,
Angular from json is now strict so assuming that this string is from a trusted source you would have to use "eval()" plus surround the call in parenthesis for it to work
var hatsData = angular.fromJson(eval("(" + hats + ")"))
If you bring your JSON from http request for example you don't need to use the fromJson method.
The JSON is automatically parsed by Angular and you can use it directly.

Pass Json Object from Play! framework to HighCharts JS

http://www.playframework.com/documentation/2.1.x/JavaTodoList
Using the above tutorial as a reference, I have created an application which sends data from the model to view via the Application controller.
I have managed to display the model(Tasks) as a high chart. The code is here.
public static Result format(){
return ok(views.html.frmt.render("Visualize it",Task.all()));
}
This goes to this view page.
http://ideone.com/ycz9ko
Currently, I use scala templating inside the javascript code itself. Refer to lines 9-14 and lines 20-24.This unelegant style of doing things is not really optimal.
I want to be able to accomplish the above using Json instead.
public static Result jsonIt(){
List<Task> tasks = Task.all();
return ok(Json.toJson(tasks));
}
My Qns are how to send the JSON objects to a view template.
And how to parse it into a Highcharts format. Is there some standard procedure to do this ? Or else I have to write my own method to do this ?
It'll great if someone can show me a code snippet. Also I would prefer a post not using Ajax. I would just want to know how to do this first.
I also found this stackoverflow post useful.how to parse json into highcharts. However, it didnt answer the part about converting from Play format to Highcharts format.
Thanks in advance
You don't need to pass a json object to your template, instead you might do an ajax call from your client side javascript (your template) and get json response that you could use futher in javascript code to build a chart. For example :
You have some path that is bind to your controller jsonIt() like so /chartsdata/json
then using jquery shorthand for ajax request:
var chart_data = $.get('/chartsdata/json', function(data) {
return data;
});
now you can use a chart_data that is an array of objects where each object represents a Task, in your further javascript code to build a chart.

MVC4: Include object name in Json

I have a question on a MVC4 (beta) issue I have been struggling with for some time now. The issue is that I want to create a json with the object name added for my webapi. The json has to be created this was as the receiving side needs this. My .NET/MVC knowledge is limited so please bear with me. I tried searching on the subject, but as MVC4 is still in beta it's difficult to find good info on this subject.
I have already imported the JSON.NET formatter in my solution, but this does not add the Object name
The json that is now created in MVC4:
[{"ID":36,"Name":"Test3","Description":"Description Test3"},{"ID": 37,"Name": "Test4","Description": "Description Test4"}]
And I would like the json to look like this:
{"Goal":[{"ID":36,"Name":"Test3","Description":"Description Test3"},{"ID": 37,"Name": "Test4","Description": "Description Test4"}]}
So I like the object name (Goal) to be included in the json.
The code in my api controller I am using for this is:
StoreDBContext db = new StoreDBContext();
//
// GET /api/goals
public IQueryable<Goals> Get()
{
return db.Goals;
}
I assume I need to loop through somewhere to add the object name, but I am not sure how... Hopefully someone can help me out with this!
Return something like this:
new { Goal = db.Goals().AsEnumerable().ToList() }
You can use an anonymous object to add properties and stuff that aren't in the object.
You can also write custom converters for JSON.NET if you want it to deserialize properly, but if you don't care about deserialization of the output, the above solution will work.

Setting a JSON Object from an input field

I basically have the following flow:
XML -> JSON -> Spring MVC -> jsp page, which is displayed as a table with editable fields.
How do make is so that when i edit a field value it correct updates the Json Object?
I have used some nasty hacking at the moment, as i know (testing) the values/object I am going to get, so im just parsing the JSON string in javascript and sending that back.
So i can then just convert the json object (with new values) and post it back.
Cheers.
You could use the serialize method from prototypejs.
http://www.prototypejs.org/api/form/serialize
just by calling .serialize() you'll be able to get the updated object.