Create & Format JSON objects in R - json

I want to create and format a json object in R(from a dataframe).
Output Format --
{"base":{K1:V1,K2:V2},"Criteria":{C1:v1,C2:v2}}
Code I'm using currently --
K1=c("V1")
K2=c("V2")
dataframe1=data.frame(K1,K2)
C1=c("v1")
C2=c("v2")
dataframe2=data.frame(C1,C2)
toJSON(list(base=dataframe1,Criteria=dataframe2))
Current Output --
{"base":[{"K1":"V1","K2":"V2"}],"Criteria":[{"C1":"v1","C2":"v2"}]}
I basically want to get rid of the square brackets "[]" . This output goes into an API which doesn't accept lists as a part of values in the JSON. Any direction is appreciated!
P.S: I have unsuccessfully looked for related threads on Stackoverflow.

Related

Cannot identify proper format for a json request body stored and used in csv file for use in a karate scenario

Am having trouble identifying the propert format to store a json request body in csv format, then use the csv file value in a scenario.
This works properly within a scenario:
And request '{"contextURN":"urn:com.myco.here:env:booking:reservation:0987654321","individuals":[{"individualURN":"urn:com.myco.here:env:booking:reservation:0987654321:individual:12345678","name":{"firstName":"NUNYA","lastName":"BIDNESS"},"dateOfBirth":"1980-03-01","address":{"streetAddressLine1":"1 Myplace","streetAddressLine2":"","city":"LANDBRANCH","countrySubdivisionCode":"WV","postalCode":"25506","countryCode":"USA"},"objectType":"INDIVIDUAL"},{"individualURN":"urn:com.myco.here:env:booking:reservation:0987654321:individual:23456789","name":{"firstName":"NUNYA","lastName":"BIZNESS"},"dateOfBirth":"1985-03-01","address":{"streetAddressLine1":"1 Myplace","streetAddressLine2":"","city":"BRANCHLAND","countrySubdivisionCode":"WV","postalCode":"25506","countryCode":"USA"},"objectType":"INDIVIDUAL"}]}'
However, when stored in csv file as follows (I've tried quite a number other formatting variations)
'{"contextURN":"urn:com.myco.here:env:booking:reservation:0987654321","individuals":[{"individualURN":"urn:com.myco.here:env:booking:reservation:0987654321:individual:12345678","name":{"firstName":"NUNYA","lastName":"BIDNESS"},"dateOfBirth":"1980-03-01","address":{"streetAddressLine1":"1 Myplace","streetAddressLine2":"","city":"LANDBRANCH","countrySubdivisionCode":"WV","postalCode":"25506","countryCode":"USA"},"objectType":"INDIVIDUAL"},{"individualURN":"urn:com.myco.here:env:booking:reservation:0987654321:individual:23456789","name":{"firstName":"NUNYA","lastName":"BIZNESS"},"dateOfBirth":"1985-03-01","address":{"streetAddressLine1":"1 Myplace","streetAddressLine2":"","city":"BRANCHLAND","countrySubdivisionCode":"WV","postalCode":"25506","countryCode":"USA"},"objectType":"INDIVIDUAL"}]}',
and used in scenario as:
And request requestBody
my test returns an "javascript evaluation failed: " & the json above & :1:63 Missing close quote ^ in at line number 1 at column number 63
Can you please identify correct formatting or the usage errors I am missing? Thanks
We just use a basic CSV library behind the scenes. I suggest you roll your own Java helper class that does whatever processing / pre-processing you need.
Do read this answer as well: https://stackoverflow.com/a/54593057/143475
I can't make sense of your JSON but if you are trying to fit JSON into CSV, sorry - that's not a good idea. See this answer: https://stackoverflow.com/a/62449166/143475

Unable to get data from JSON in Applescript

So I'm able to get the values from one api but I'm unable to get the second value, which is a currency conversion rate (from an entirely different source). I've pasted my code below, any help would be much appreciated.
set json to (do shell script "curl https://www.bitstamp.net/api/v2/ticker/xrpusd")
tell application "JSON Helper"
set result to read JSON from json
set price to |last| of result as number
end tell
set json to (do shell script "curl https://api.fixer.io/latest?symbols=INR")
tell application "JSON Helper"
set result to read JSON from json
set inr to |INR| of result as number
end tell
set result_string to "₹" & price * inr & ""
If I open the second URL in a browser, here’s the JSON that gets returned (formatted for easier reading):
{"base":"EUR",
"date":"2018-02-05",
"rates":{
"INR":79.744
}
}
The INR value is inside the rates value. Therefore, you need to reference it like so:
set inr to (INR of rates of result) as number
I don’t see any reason to enclose INR inside vertical bars, as there’s no conflicting AppleScript commands or variables with that name. You can even get away with defining inr as a user-defined variable, and AppleScript will be able to distinguish when you’re referencing that from when your referencing INR of rates.
However, result is a pre-defined AppleScript property, so I would avoid using this in the way that you have. What you can do is change this line:
set result to read JSON from json
to this:
read JSON from json
and keep the subsequent line as it is (with my suggested correction from above). Now you’ll be using the AppleScript result property correctly.

Python: Dump JSON Data Following Custom Format

I'm working on some Python code for my local billiard hall and I'm running into problems with JSON encoding. When I dump my data into a file I obviously get all the data in a single line. However, I want my data to be dumped into the file following the format that I want. For example (Had to do picture to get point across),
My custom JSON format
. I've looked up questions on custom JSONEncoders but it seems they all have to do with datatypes that aren't JSON serializable. I never found a solution for my specific need which is having everything laid out in the manner that I want. Basically, I want all of the list elements to on a separate row but all of the dict items to be in the same row. Do I need to write my own custom encoder or is there some other approach I need to take? Thanks!

AWS Lambda output format - JSON

I trying to format my output from a lambda function into JSON. The lambda function queries my Amazon Aurora RDS instance and returns an array of rows in the following format:
[[name,age,town,postcode]]
which gives the an example output:
[["James", 23, "Maidenhead","sl72qw"]]
I understand that mapping templates are designed to translate one format to another but I don't understand how I can take the output above and map in to a JSON format using these mapping templates.
I have checked the documentation and it only covers converting one JSON to another.
Without seeing the code you're specifically using, it's difficult to give you a definitely correct answer, but I suspect what you're after is returning the data from python as a dictionary then converting that to JSON.
It looks like this thread contains the relevant details on how to do that.
More specifically, using the DictCursor
cursor = connection.cursor(pymysql.cursors.DictCursor)

Spline graph not showing in highchart

I am using highchart with codeigniter. My json data is like below,
[{"name":"zreadactivity","data":["E-Juice","Tank Kit & Accessories","Clearomizers","Cartomisers & Accessories","Starter Kits"]},{"name":"TotalValue","data":["11,320.00","6,629.52","5,184.43","2,691.75",353.85]}]
Data is coming but spline graph is not created. Only last value is showing in graph that is 353.85, others are not displaying.
Is there any thing for higher value? or where I stuck?
In data you should have array of points with number values, not strings as you have. So you need to convert your json to correct form, during preprocessing or return json in correct form.