I want to create JSON data using R's jsonlite package to load to DynamoDB using Python. I want the data to be in the structure shown below. How can I create this in R? I tried creating a data frame where one of the columns is a list and change the data frame to json but the result is not in the required format. I also tried a converting a list which contains a list, but the structure of the output json is not what I want.
[
{
"ID": 100,
"title": "aa",
"more": {
"interesting":"yes",
"new":"no",
"original":"yes"
}
},
{
"ID": 110,
"title": "bb",
"more": {
"interesting":"no",
"new":"yes",
"original":"yes"
}
},
{
"ID": 200,
"title": "cc",
"more": {
"interesting":"yes",
"new":"yes",
"original":"no"
}
}
]
Here is my sample data and what I tried:
library(jsonlite)
ID=c(100,110,200)
Title=c("aa","bb","cc")
more=I(list(Interesting=c("yes","no","yes"),new=c("no","yes","yes"),original=c("yes","yes","no")))
a=list(ID=ID,Title=Title,more=more)
a=toJSON(a)
write(a,"temp.json") # this does not give the structure I want
this will produce what you need:
library(jsonlite)
ID=c(100,110,200)
Title=c("aa","bb","cc")
df <- data.frame(ID, Title)
more=data.frame(Interesting=c("yes","no","yes"),new=c("no","yes","yes"),original=c("yes","yes","no"))
df$more <- more
toJSON(df)
output:
[{
"ID": 100,
"Title": "aa",
"more": {
"Interesting": "yes",
"new": "no",
"original": "yes"
}
}, {
"ID": 110,
"Title": "bb",
"more": {
"Interesting": "no",
"new": "yes",
"original": "yes"
}
}, {
"ID": 200,
"Title": "cc",
"more": {
"Interesting": "yes",
"new": "yes",
"original": "no"
}
}
]
Related
In an overview, let's say I have a CSV file that has 5 entries of data (I will have a large number of entries in the CSV file) that I need to use dynamically while building the JSON payload using python (in Databricks).
test.csv
1a2b3c
2n3m6g
333b4c
2m345j
123abc
payload.json
{
"records": {
"id": "37c8323c",
"names": [
{
"age": "1",
"identity": "Dan",
"powers": {
"key": "plus",
"value": "1a2b3c"
}
},
{
"age": "2",
"identity": "Jones",
"powers": {
"key": "minus",
"value": "2n3m6g"
}
},
{
"age": "3",
"identity": "Kayle",
"powers": {
"key": "multiply",
"value": "333b4c"
}
},
{
"age": "4",
"identity": "Donnis",
"powers": {
"key": "divide",
"value": "2m345j"
}
},
{
"age": "5",
"identity": "Layla",
"powers": {
"key": "power",
"value": "123abc"
}
}
]
}
}
The above payload that I need to construct as a result of multiple names objects in the array and I also would like the value property to read dynamically from the CSV file.
I basically need to append the below JSON object to the existing names array considering the value for the power object from the CSV file.
{
"age": "1",
"identity": "Dan",
"powers": {
"key": "plus",
"value": "1a2b3c"
}
}
Since I'm a newbie in Python, any guides would be appreciated. Thanks to the StackOverflow team in advance.
I've got the response from HTTP GET request as JSON file and I want to use that JSON and pass it to the next HTTP request. I got the following response data
{
"apiInfo": {
"id": "23143",
"name": "bookkeeping",
"state": "used",
"data": "15893712000000"
},
"apiDetails": [
{
"bookName": "abc",
"state": "old",
"noOfTimesUsed": "53"
"additionalParam"{
"name": "abc",
"id": "123"
}
},
{
"bookName": "def",
"state": "new",
"noOfTimesUsed": "5",
"action": "keep"
"additionalParam"{
"name": "def",
"id": "456"
}
},
{
"bookName": "xyz",
"state": "avg",
"noOfTimesUsed": "23"
"additionalParam"{
"name": "ghi",
"id": "789"
}
},
{
"bookName": "pqr",
"state": "old",
"noOfTimesUsed": "75",
"action": "discard"
"additionalParam"{
"name": "jkl",
"id": "012"
}
}
]
}
I want to use "apiInfo" & "apiDetails" part from the JSON response and manipulate its data. As you can notice, some array field have attribute "action" in it and some one doesn't. I want to make sure all the field in the array have this data and is assigned as ' "action":"keep" '. Also, I want to add "id" from apiInfo & "name" from additionalParams from apiDetails itself. The end result I want is somewhat like this
"apiDetails": [
{
"id": "23143",
"bookName": "abc",
"state": "old",
"noOfTimesUsed": "53",
"action": "keep",
"name":"abc"
},
{
"id": "23143",
"bookName": "def",
"state": "new",
"noOfTimesUsed": "5",
"action": "keep",
"name":"def"
},
{
"id": "23143",
"bookName": "xyz",
"state": "avg",
"noOfTimesUsed": "23",
"action": "keep",
"name":"ghi"
},
{
"id": "23143",
"bookName": "pqr",
"state": "old",
"noOfTimesUsed": "75",
"action": "keep",
"name":"jkl"
}
]
I've been trying to use JSR223 sampler and have been struggling with it. It's bit complicated and I need help. P.S.: I've tried using javascript code to manipulate the results as desired but have been unsuccessful.
Please help.
Thanks, Sid
Add JSR223 PostProcessor as a child of the request which returns the above JSON
Put the following code into "Script" area:
def apiDetails = new groovy.json.JsonSlurper().parse(prev.getResponseData()).apiDetails
apiDetails.each { apiDetail ->
apiDetail.put('action', 'keep')
}
vars.put('request', new groovy.json.JsonBuilder(apidetails: apiDetails.collect()).toPrettyString())
That's it, you should be able to refer the generated request as ${request} where required
More information:
Apache Groovy - Parsing and producing JSON
Apache Groovy - Why and How You Should Use It
I am having a trouble to preserve the order of my json and pretty printing it in pyspark.
Below is sample code:
json_out = sqlContext.jsonRDD(sc.parallelize([json.dumps(info)]))
# here info is my ordered dictionary
json_out.toJSON().saveAsTextFile("file:///home//XXX//samplejson")
One more thing is that I want my output as single file and not as partitioned datasets.
Could anyone help in pretty printing and preserving the order of output json in my case?
info sample:
Note:TypeA,TypeB etc is a list meaning there can be more than one product in TypeA or TypeB.
{
"score": {
"right": ,
"wrong":
},
"articles": {
"TypeA": [{
"ID": 333,
"Name": "",
"S1": "",
"S2": "",
"S3": "",
"S4": ""
}],
"TypeB": [{
"ID": 123,
"Name": "",
"T1": "",
"T2": "",
"T3": "",
"T4": "",
"T5": "",
"T6": ""
}]
}
}
( I have tried using json.dumps(info,indent=2),but of no use.
I have a sample backend response json in this format:
I want to extract the comma separated values from the RESPONSEs and RESPONSE_ORDER tag, such that I can distinctly set them in my output JSON. Essentially, my output should be some what like this using xslt transformation.
Using xslt mediator I want to transform it as below:
{
"d": {
"results": [
{
"TYPE_ID": "20",
"QUESTION_ID": "41333",
"RESPONSE_TYPE": "Checkboxes",
"QUESTIONVAL": "xssdsdd",
"RESPONSE": "Yes",
"RESPONSE_ORDER": "1"
},
{
"TYPE_ID": "21",
"QUESTION_ID": "417",
"RESPONSE_TYPE": "Radio buttons",
"QUESTIONVAL": "abc",
"RESPONSE": "Yes,No",
"RESPONSE_ORDER": "1,2"
}
]
}
}
Sample output after transformation
{
"results": [
{
"TYPE_ID": "20",
"QUESTION_ID": "41333",
"RESPONSE_TYPE": "Checkboxes",
"QUESTIONVAL": "xssdsdd",
Response&Order: [
{
"RESPONSE": "Yes",
"RESPONSE_ORDER": "1"
}
]
},
{
"TYPE_ID": "21",
"QUESTION_ID": "417",
"RESPONSE_TYPE": "Radio buttons",
"QUESTIONVAL": "abc",
Response&Order: [
{
"RESPONSE": "Yes",
"RESPONSE_ORDER": "1"
},
{
"RESPONSE": "No",
"RESPONSE_ORDER": "2"
}
]
}
]
}
How can I split the values to generate this kind of output?
I have a JSON example to i would like to transform to Excell file to be able to modify all fields and after that, be able to export Excell file to retrieve the new JSON file updated.
I tryed some online tool like (http://www.convertcsv.com/csv-to-json.htm) but the result is not good : I am able to create a csv file, but not able to convert csv file to json.
Do you know a tool with which i will be able to convert to csv / convert to json ?
JSON example :
[
{
"key": "keyExample",
"type": "typeExample",
"ref": "refExample",
"items": [
{
"itemRef": "aaa",
"count": 1,
"desc": "aaaaaaaaa"
},
{
"itemRef": "bbb",
"count": 2,
"desc": "bbbbbbb"
},
{
"itemRef": "ccc",
"count": 2,
"desc": "ccccccc"
}
]
},
{
"key": "keyExample2",
"type": "typeExample2",
"ref": "refExample2",
"items": [
{
"itemRef": "aaa",
"count": 1,
"desc": "aaaaaaaaa"
},
{
"itemRef": "bbb",
"count": 2,
"desc": "bbbbbbb"
},
{
"itemRef": "ccc",
"count": 2,
"desc": "ccccccc"
}
]
}
]