Modifying the json response - WEbAPI - json

I am returning the object directly in the GET request as following.
Ok(object);
and the response json is given as,
json data-->
{
"id":"1",
"name":"testname"
}
I want to add some more details to this json
-->
{
success:"true",
messageDetails:"The response is returned by the service",
data:{}
}
how to accomplish this?
can i club all the things in Ok(object) ??

You can make use of an anonymous type, for example:
object data = new { id = 1, name = "testname" };
return Ok(new
{
success = "true",
messageDetails = "The response is returned by the service",
data
});

Related

How to JSON parsing to GoogleSignInAccount data at flutter dart

I want to get JSON data from GoogleSignInAccount.
_googleSignIn.onCurrentUserChanged.listen((GoogleSignInAccount account) async {
if (account != null) {
auth = Auth.fromJson(json.decode(account.toString()));
_signInView.onGoogleResponse(auth);
} else {
_signInView.onGoogleResponse(null);
}
});
but
account.toString() returned the response =>
{
displayName: Mert TUTSAK,
email: merttutsak#gmail.com
}
I want to return the JSON object.
{
"displayName": "Mert TUTSAK",
"email": "merttutsak#gmail.com"
}
How to make it?
What you get is just what toString() produces on a Map.
To get a valid JSON string, use json.Encode() or jsonEncode (they are equivalent)
import 'dart:convert';
...
var json = jsonEncode(account);
print(json);

Json Output display from webservice call

I am pretty new to JSON / Jquery world so please bear with my ignorance.
I am trying to read an output from a Json data returned by webservice call like below :
My webservice call is here:
http://example.com/getPortfolioListByContact.json?component=C1&contactId=510297
This return the data as :
{
"data": [
{
"PORTFOLIO_ID": 13495,
"SUBSCRIPTION_ID": 1653,
"STATUS": "ACTIVE",
}
],
"success": true
}
Now I am trying to get alert onthe Json Data returned as string and also want to get this as Parsed /
<script>
var parsed ;
$.getJSON("http://example.com/getPortfolioListByContact.json?component=C1&contactId=510297", function(data){
alert(data.SUBSCRIPTION_ID);
});
parsed = JSON.parse(data);
alert(parsed) ;
</script>
I am getting the response in Alert as "Undefined" . I may not be doing right handling the success handler . I want to get each value and specific value of the json data returned.
Please help.
Thanks
I am getting the response in Alert as "Undefined".
Reason : You are trying to parse the API response out of the scope. As data object is only accessible in the promise returned by the API call.
Try this hope it will work as per your expectation :
$.getJSON("http://example.com/getPortfolioListByContact.json?component=C1&contactId=510297", function(res) {
var data = res.data;
alert(JSON.stringify(data));
});
You are calling data out side the scope.
<script>
var parsed ;
var myData;
$.getJSON("http://example.com/getPortfolioListByContact.json?component=C1&contactId=510297", function(data){
myData = data;
alert(data.SUBSCRIPTION_ID);
});
parsed = JSON.parse(myData);
alert(parsed) ;
</script>

Multi level json with body response in nodjs

I make a request to REST Server with NodeJS and getting this type of JSON in my 'body'
[{
"id": 802,
"created": "2016-10-18 15:22:08",
"test": {
"name": "Fred "
}
}]
log my body with JSON.parse, that is working fine for the 'root' of the JSON, but I'm not getting the 'name' in the 'test' array
var jsonObj = JSON.parse(body);
for(index in jsonObj) {
console.log(jsonObj[index].bag);
How can I make the loop and if there is a 'test' getting the data keys out ??
This code should work given your JSON description
var jsonObj = JSON.parse(body);
for(index in jsonObj) {
console.log(jsonObj[index].test.name);

Extract response json string from akka http javadsl ResponseEntity

I'm trying to make request to facebook REST APIs and in return getting a JSON Response. I'm able to collect the response in REST client, hence I know the requestUrl I'm using while creating HttpRequest in following code is correct. But when I try to mimick the GET using akka-http javadsl, I'm unable to understand how to extract the json from the ResponseEntity.
final HttpRequest request = HttpRequest.GET(requestUrl);
final Materializer materializer = ActorMaterializer.create(this.context.getActorSystem());
final CompletionStage<HttpResponse> responseFuture =
Http.get(this.context.getActorSystem()).singleRequest(request, materializer);
final HttpResponse response = responseFuture.toCompletableFuture().get();
I'm expecting a response something as follows -
{
"data": [
{
"cpc": 9.7938056680162,
"clicks": "247",
"impressions": "15949",
"spend": 2419.07,
"date_start": "2016-06-15",
"date_stop": "2016-08-13"
}
],
"paging": {
"cursors": {
"before": "MAZDZD",
"after": "MAZDZD"
}
}
}
You should get response entity from response by calling ResponseEntity entity = response.entity() and after that call entity.toStrict(timeoutMillis, materialiser).data.decodeString("UTF-8") to get body string
You can lookup signatures of those methods in official API documentation

How to post into the MongoDb collection using postman

I am trying to insert data into the MongoDb collection using postman. How would I approach this; hardcoding the data in JSON format works, but I want to be able to insert using postman in JSON format.
This is the code that allowed me to enter directly, using the post function in postman, with no input:
public async void Insert([FromBody]string value)
{
var client = new MongoClient();
var dbs = client.GetDatabase("test");
var collection = dbs.GetCollection<BsonDocument> ("restaurants");
BsonArray dataFields = new BsonArray { new BsonDocument {
{ "ID" , ObjectId.GenerateNewId()}, { "_id", ""}, } };
var document = new BsonDocument
{
{"borough",value },
{"cuisine","Korean"},
{"name","Bellaa"},
{"restaurant_id","143155"}
};
await collection.InsertOneAsync(document);
}
You can send it as raw data. You will set the post type to application/json.
This comes From the docs.