How to send json response using plumber R - json

I need to send response from R using plumber package in below format
{
"status": "SUCCESS",
"code": "200",
"output": {
"studentid": "1001",
"name": "Kevin"
}
}
But I am getting below format
[
"{\n \"status\": \"SUCCESS\",\n \"code\": \"200\",\n \"output\": {\n \"studentid\": \"1001\",\n \"name\": \"Kevin\"\n }\n}"
]
Please help me format this json properly
My Code
#* #post /sum
addTwo <- function(){
library(jsonlite)
x <- list(status = "SUCCESS", code = "200",output = list(studentid = "1001", name = "Kevin"))
output<-toJSON(x,pretty = TRUE, auto_unbox = TRUE)
return (output)
}

I added an unboxedJSON serializer to the development version of plumber. Depending on when in the future this is being read, that serializer might have been published to CRAN and might even be the default serializer now (I'm still debating).
But for now, you can install the development version from GitHub (devtools::install_github("trestletech/plumber")) then add the #serializer unboxedJSON annotation to your function like so:
#* #post /sum
#* #serializer unboxedJSON
addTwo <- function(){
list(status = "SUCCESS", code = "200",output = list(studentid = "1001", name = "Kevin"))
}
FYI if you ever do want to force plumber to return some text that you're providing directly, you should be able to set the $body element on res then return the res object from the function.
#* #get /
function(res){
res$body <- "I am raw"
res
}
which will return the unformatted, unserialized text I am raw in its response.

Just remove the toJSON() wrapper.
Plumber already does JSON serialisation so you are doing it twice by adding a toJSON function.
This should work.
addTwo <- function(){
x <- list(status = "SUCCESS", code = "200",output = list(studentid = "1001", name = "Kevin"))
return (x)
}

Related

Unable to get json node values

This is the json response format that I have and need to read the first node value "html". I have tried to few ways to get the value but unable to get it
[
{
"html": "<!DOCTYPE html>\n <html>\n <head>\n <\/html>\n \n",
<\/html>\n \n",
"headers": {},
"subject": "Register new account",
"messageId": "475603953.247.1565607800153#dfrsbdd201.abc.com.au",
"priority": "normal",
"from": [],
"to": [],
"date": "2019-08-12T11:03:20.000Z",
"receivedDate": "2019-08-12T11:09:42.000Z",
"receivedAt": "2019-08-12T11:09:44.900Z"
},
{+},
{+}
]
I tried couple of things
RequestSpecification emailReq = given().with().pathParam("email",emailName);
Response emailResp = emailReq.contentType("application/json").get("https://restmail.net/mail/{email}");
JSONObject value = new JSONObject(emailResp.asString());
I got this error
org.json.JSONException: A JSONObject text must begin with '{' at 1 [character 2 line 1]
at org.json.JSONTokener.syntaxError(JSONTokener.java:505)
and then I removed this last line to use But this does not gives me error. pls refer screenshot for this
JSONArray responseJson = new JSONArray(emailResp.asString());
Tried this as well
List<HashMap<String, Object>> jsonObjectsInArray = emailResp.jsonPath().getList("$");
for (HashMap<String, Object> singleLeg : jsonObjectsInArray) {
String value = (String) singleLeg.get("html");
}
But again array size is 0
Need some suggestion of how to get the value of node - "html". Pls suggest. what mistake am I doing here?
Thanks in advance
RequestSpecification emailReq = given().with().pathParam("email",emailName);
int retries = 5;
List<HashMap<String, Object>> emails = Arrays.asList();
. . .
emailResp = emailReq.get("restmail.net/mail{email}");
if (emailResp.getStatusCode() == 200) { emails = emailResp.jsonPath().getList("$");
String email = emails.get(0).get("html").toString();

JSON.Lua json.encode return nil

I'm new to LUA and tried learning coding this language with Garrys Mod.
I want to get the messages from the Garrys Mod chat and send them into a Discord channel with a webhook.
It works, but I tried expanding this project with embeded messages. I need JSON for this and used json.lua as a library.
But as soon as I send a message I retrieve the following error message:
attempt to index global 'json' (a nil value)
The code that causes the error is the following:
json.encode({ {
["embeds"] = {
["description"] = text,
["author"] = {
["name"] = ply:Nick()
},
},
} }),
The complete code:
AddCSLuaFile()
json = require("json")
webhookURL = "https://discordapp.com/api/webhooks/XXX"
local DiscordWebhook = DiscordWebhook or {}
hook.Add( "PlayerSay", "SendMsg", function( ply, text )
t_post = {
content = json.encode({ {
["embeds"] = {
["description"] = text,
["author"] = {
["name"] = ply:Nick()
},
},
} }),
username = "Log",
}
http.Post(webhookURL, t_post)
end )
I hope somebody can help me
Garry's Mod does provide two functions to work with json.
They are:
util.TableToJSON( table table, boolean prettyPrint=false )
and
util.JSONToTable( string json )
There is no need to import json and if I recall correctly it isn't even possible.
For what you want to do you need to build your arguments as a table like this:
local arguments = {
["key"] = "Some value",
["42"] = "Not always the answer",
["deeper"] = {
["my_age"] = 22,
["my_name"] = getMyName()
},
["even more"] = from_some_variable
and then call
local args_as_json = util.TableToJSON(arguments)
Now you can pass args_as_json to your
http.Post( string url, table parameters, function onSuccess=nil, function onFailure=nil, table headers={} )

NIFI:Json Content Parsing in FlowFile

I have text attribute in GenerateFlowfile processor like this:
[{
"status": {
"runStatus": "STOPPED"
},
"component": {
"state": "STOPPED",
"id": "ea5db028-015d-1000-5ad5-80fd006dda92"
},
"revision": {
"version": 46,
"clientId": "ef592126-015d-1000-bf4f-93c7cf7eedc0"
}
} ]
and related groovy script in my ExecuteScript processor :
import org.apache.commons.io.IOUtils
import java.nio.charset.*
def flowFile = session.get();
if (flowFile == null) {
return;
}
def slurper = new groovy.json.JsonSlurper()
def attrs = [:] as Map<String,String>
session.read(flowFile,
{ inputStream ->
def text = IOUtils.toString(inputStream, StandardCharsets.UTF_8)
text=flowFile.getAttribute('text')
def obj = slurper.parseText(text)
obj.each {k,v ->
attrs[k] = v.toString()
}
} as InputStreamCallback)
flowFile = session.putAllAttributes(flowFile, attrs)
session.transfer(flowFile, REL_SUCCESS)
but my processor still shows me exception like this, what should i change?
the problem that the root element of your json is an array
and you try to iterate it like map .each{k,v-> ... }
probably you want to take the first map to iterate it like in code below
def obj=[ [a:1] ]
//this line will throw exception:
//obj.each{k,v-> println "$k->$v" }
//but this one not
obj[0].each{k,v-> println "$k->$v" }
fyi: there is a EvaluateJsonPath processor that could extract attributes from json flowfile content and put result into attribute

UWP - From Json string to Structure (Classes)

I receive a JSon string from WS. It's so long that I can't use Json2charp to parse it and receive the structurated class.
I want to parse the string with a command. How is it possible?
I don't know the classes so I can't use a command like:
Dim result = JsonConvert.DeserializeObject(Of MyClass.RootObject)(String_From_File)
Is it possible from the string to obtain the class without using json2charp site ?
For example, in vs.net if on the variable 'string_from_file' I choose 'Json Visualizer' and see all classes and data in correct mode.
How can I obtain the same in my code ?
I have installed Newtonsoft.json
If you cannot use the json to class mappers like NewtonSoft.Json. You can use the Windows.Data.Json api. It let you parse and extract the data you want from your JSON string.
JsonValue jsonValue = JsonValue.Parse("{\"Width\": 800, \"Height\": 600, \"Title\": \"View from 15th Floor\", \"IDs\": [116, 943, 234, 38793]}");
double width = jsonValue.GetObject().GetNamedNumber("Width");
double height = jsonValue.GetObject().GetNamedNumber("Height");
string title = jsonValue.GetObject().GetNamedString("Title");
JsonArray ids = jsonValue.GetObject().GetNamedArray("IDs");
You can find a sample in the Windows Universal Sample GitHub.
A complex object parsing is shown here. I've extracted the most relevant parts here. The JSON string is provided to the User constructor which is extracting what it needs and then delegating the parsing to the nested School constructor.
{
"id": "1146217767",
"phone": null,
"name": "Satya Nadella",
"education": [
{
"school": {
"id": "204165836287254",
"name": "Contoso High School"
},
"type": "High School"
},
{
"school": {
"id": "116138758396662",
"name": "Contoso University"
},
"type": "College"
}
],
"timezone": -8,
"verified": true
}
This JSON fragment is parsed with this code:
public User(string jsonString) : this()
{
JsonObject jsonObject = JsonObject.Parse(jsonString);
Id = jsonObject.GetNamedString(idKey, "");
IJsonValue phoneJsonValue = jsonObject.GetNamedValue(phoneKey);
if (phoneJsonValue.ValueType == JsonValueType.Null)
{
Phone = null;
}
else
{
Phone = phoneJsonValue.GetString();
}
Name = jsonObject.GetNamedString(nameKey, "");
Timezone = jsonObject.GetNamedNumber(timezoneKey, 0);
Verified = jsonObject.GetNamedBoolean(verifiedKey, false);
foreach (IJsonValue jsonValue in jsonObject.GetNamedArray(educationKey, new JsonArray()))
{
if (jsonValue.ValueType == JsonValueType.Object)
{
Education.Add(new School(jsonValue.GetObject()));
}
}
}
public School(JsonObject jsonObject)
{
JsonObject schoolObject = jsonObject.GetNamedObject(schoolKey, null);
if (schoolObject != null)
{
Id = schoolObject.GetNamedString(idKey, "");
Name = schoolObject.GetNamedString(nameKey, "");
}
Type = jsonObject.GetNamedString(typeKey);
}
If you cannot use the automatic mapping from NewtonSoft.Json, you have no other way than doing it yourself.
Is not so simple.
The Json i received is very complicated and have many class
So i can't use
double width = jsonValue.GetObject().GetNamedNumber("Width");
Inside class i have more ...

Rendering Json response in grails view

I am querying twitter api to get the tweets with a hashtag in grails 2.1.1.
My Controller is
import grails.converters.*
import org.codehaus.groovy.grails.web.json.*; // package containing JSONObject, JSONArray,...
import groovyx.net.http.*
import static groovyx.net.http.ContentType.*
import static groovyx.net.http.Method.*
class NepTweetController {
def index() {
//def restEndpointUrl = "http://search.twitter.com/search.json?q=%23nepal";
def restEndpointUrl = "http://search.twitter.com/search.json?q=%23nepal";
def http = new HTTPBuilder(restEndpointUrl);
// perform a GET request, expecting JSON response data
http.request( GET, JSON ) {
//uri.path = '/search.json'
//uri.query = [ q: '%23nepal' ]
response.success = { resp, json ->
// def tweetsResult = json.results;
// List parsedList = JSON.parse(json) as List;
// JSONObject tweetJson = JSON.parse(json);
//def tweetJson = JSON.parse(json);
def tweets = new JSON(json) as Map;
render(view: "index", model: [message: "Request sent" , tweets: tweets]);
}
}//end of request
}//end of method
}
I successfully get the json response as follows :
{
"completed_in": 0.017,
"max_id": 271907240007581696,
"max_id_str": "271907240007581696",
"next_page": "?page=2&max_id=271907240007581696&q=%23nepal%2F",
"page": 1,
"query": "%23nepal%2F",
"refresh_url": "?since_id=271907240007581696&q=%23nepal%2F",
"results": [
{
"created_at": "Fri, 23 Nov 2012 09:25:12 +0000",
"from_user": "iBshnu",
"from_user_id": 25051623,
"from_user_id_str": "25051623",
"from_user_name": "Bishnu Bhattarai",
"geo": null,
"id": 271907240007581696,
"id_str": "271907240007581696",
"iso_language_code": "ne",
"metadata": {
"result_type": "recent"
},
"profile_image_url": "http:\/\/a0.twimg.com\/profile_images\/2622309791\/42z33jnw1kak9ttyqkrf_normal.jpeg",
"profile_image_url_https": "https:\/\/si0.twimg.com\/profile_images\/2622309791\/42z33jnw1kak9ttyqkrf_normal.jpeg",
"source": "<a href="http:\/\/twitter.com\/">web<\/a>",
"text": "RT #dipakbhattarai: \u0930\u093e\u0937\u094d\u091f\u094d\u0930\u092a\u0924\u093f \u0921\u093e. \u0930\u093e\u092e\u092c\u0930\u0923 \u092f\u093e\u0926\u0935\u0926\u094d\u0935\u093e\u0930\u093e \u092e\u0919\u094d\u0938\u093f\u0930 \u0967\u096a \u092d\u093f\u0924\u094d\u0930 \u0926\u0932\u0939\u0930\u0941\u0932\u093e\u0908 \u0930\u093e\u0937\u094d\u091f\u094d\u0930\u093f\u092f \u0938\u0939\u092e\u0924\u093f\u0915\u094b \u0938\u0930\u0915\u093e\u0930 \u092c\u0928\u093e\u0909\u0928 \u0906\u0935\u094d\u0939\u093e\u0928\u0964 #Nepal",
"to_user": null,
"to_user_id": 0,
"to_user_id_str": "0",
"to_user_name": null
},
{
"created_at": "Fri, 23 Nov 2012 09:24:58 +0000",
"from_user": "Phanindra07",
"from_user_id": 63104333,
"from_user_id_str": "63104333",
"from_user_name": "Phanindra Dahal",
"geo": null,
"id": 271907179404095488,
"id_str": "271907179404095488",
"iso_language_code": "en",
"metadata": {
"result_type": "recent"
},
"profile_image_url": "http:\/\/a0.twimg.com\/profile_images\/2417859753\/febstuo4p4zltimnpky0_normal.jpeg",
"profile_image_url_https": "https:\/\/si0.twimg.com\/profile_images\/2417859753\/febstuo4p4zltimnpky0_normal.jpeg",
"source": "<a href="http:\/\/twitter.com\/">web<\/a>",
"text": "RT #DeepakAdk: Chef's assault on #Nepal #Maoist reflects grassroots anger, excellent piece by my #AFP colleague #FrankieTaggart http:\/\/t.co\/M47qJeoD",
"to_user": null,
"to_user_id": 0,
"to_user_id_str": "0",
"to_user_name": null
}
[....more....]
My /views/nepTweet/index.gsp is
<meta name='layout' content='main'/>
<b>${message}</b>
<p>Tweets</p>
<g:each in="${tweets}" var="tweet">
<p>${tweet.text}</p>
</g:each>
But, I get error parsing json to grails Map.
Error I get is :
Error 500: Internal Server Error
URI
/WhoTweetsNepal/nepTweet
Class
groovy.lang.MissingMethodException
Message
No signature of method: grails.converters.JSON.entrySet() is applicable for argument types: () values: [] Possible solutions: every()
I went through How do I access A JSON Object(String) from GSP page?, but couldn't get help.
Helpppp!!!
You definitely want
def tweetJson = JSON.parse(json);
(which will return a JSONObject, which is a Map) instead of
def tweetJson = new JSON(json) as Map;
but it looks like you have a bit of a name clash in your code - presumably the JSON in
http.request( GET, JSON )
is intended to be the HTTPBuilder content type rather than grails.converters.JSON. If you can clear up this name clash (i.e. remove the import static and say ContentType.JSON) then things may run more smoothly.
With to the point suggestions from Ian Roberts, I got the solution with my final Controller to be(realising there is no need to parse it) :
import grails.converters.*
import org.codehaus.groovy.grails.web.json.*; // package containing JSONObject, JSONArray,...
import groovyx.net.http.*
import static groovyx.net.http.ContentType.*
import static groovyx.net.http.Method.*
class NepTweetController {
def index() {
def restEndpointUrl = "http://search.twitter.com/search.json?q=%23nepal";
def http = new HTTPBuilder(restEndpointUrl);
// perform a GET request, expecting JSON response data
http.request( GET, ContentType.JSON ) {
//uri.path = '/search.json'
//uri.query = [ q: '%23nepal' ]
response.success = { resp, json ->
def tweetsResult = json.results;
render(view: "index", model: [message: "Request sent" , tweets: tweetsResult]);
}
}//end of request
}//end of method
}