In a view of django, I want to output the queryset converted to json without the model, pk, and field text.
my view code:
s = serializers.serialize('json', Item.objects.get(id=actuators_id)])
o = s.strip("[]")
return HttpResponse(o, content_type="application/json")
What I get is this:
{"model": "actuators.acutatoritem", "pk": 1, "fields": {"name": "Environment Heater", "order": 1, "controlid": "AAHE", "index": "1", "param1": "", "param2": "", "param3": "", "current_state": "unknown"}}
What I spend all day NOT getting is this:
{"name": "Environment Heater", "order": 1, "controlid": "AAHE", "index": "1", "param1": "", "param2": "", "param3": "", "current_state": "unknown"}
I can I strip the model, pk, and field text from my output????
Use simplejson to convert the qs to a python dict {}
import simplejson
s = serializers.serialize('json', Item.objects.filter(id=actuators_id)])
js = simplejson.loads(s)
//select the key needed and return the response
s = js[0]['fields']
return HttpResponse(str(s), content_type="application/json")
Related
I am trying to read a simple json file into a dataframe. Here is the JSON:
{
"apiName": "bctt-e-card-operations",
"correlationId": "bctt-e-card-operations",
"msg": "{ "MSG_TIP": "1261", "MSG_VER": "02", "LOG_SIS": "32", "LOG_PERN01": "2122", "LOG_NUMN01": "30108916", "MSG_RESTIPA00": "0", "MSG_IDE": "00216452708916", "MSG_REACOD": "000", "SAN_NUM": "000010X01941XXX", "EXT_MOECOD": "978", "SAN_SDIMNT": "00000043830", "LOG_SINMOV": "C", "SAN_SAUMNT": "00000043830", "LOG_SINMOV": "C", "SAN_SCOMNT": "00000043830", "LOG_SINMOV": "C", "SAN_SCODAT": "20220502", "SDF_FORDAD": "0", "CAR_PAGXTR": "0", "CLI_LIMCRE": "0000000", "SDF_AUTCAT": "000000000", "LOG_SINMOV": "C", "SDF_SLDDIV": "000000000", "CLI_SDICSH": "000000000", "LOG_SINMOV": "C", "CLI_SDICPR": "000000000", "LOG_SINMOV": "C", "MSG_DADLGT": "0000" }"
"contentType": "application/json",
"msgFormat": "SIBS"
"step": "response",
"status": "0",
"transTimestamp": "2022-05-02 16:45:28.487",
"operationMetadata": {
"msgType": "PAYMENT",
"msgSubtype": "116100-02300",
"accountId": "10X01941XXX",
"cardNumber": "451344X063617XXX",
"reference": "212230108916",
"originalReference": null,
"reversalReference": null,
"timeout": false
"flow": "PRT"
},
"error": {
"errorCode": null
"errorDescription": null
}
}
Here is my code to read the file
file_location = "/mnt/test/event example.json"
df = spark.read.option("multiline", "true").json(file_location, schema=schema)
display(df)
But as you can see the json file is missing some commas and also the 'msg' key has its value wrapped in quotes. This makes the dataframe return everything with nulls.
Is there a way to format the JSON, (in Pyspark because the file comes like that from the source), for removing the quotes and adding commas so the json is properly formated?
Thanks in advance
You could turn the JSON into a string and use Python RegEx sub() function to reformat the string.
Example:
import re
json_string= " "contentType": "application/json",
"msgFormat": "SIBS"
"step": "response","
x = re.sub("\"SIBS\"", "\"SIBS\"," , json_string)
print(json_string)
" "contentType": "application/json",
"msgFormat": "SIBS",
"step": "response","
In this case, the function is looking for the value "SIBS" and replacing it with "SIBS", .You might need to play around with it and use escape characters.
Lets say I have the following JSON :-
{
"book": [
{
"id": "01",
"language": "Java",
"edition": "third",
"author": "Herbert Schildt"
},
{
"id": "07",
"language": "C++",
"edition": "second",
"author": "E.Balagurusamy"
}
]
}
And, I am passing the value of author from excel sheet to check if that author is present or not. If that author is present inside JSON, then that that particular array node only and remove other from the JSON.
For Example:- I am passing "author" value as "Herbert Schildt" from excel sheet. Now this value is present inside JSON, So, I need this particular array node to be printed and rest all should be removed. Like this:-
{
"book": [
{
"id": "01",
"language": "Java",
"edition": "third",
"author": "Herbert Schildt"
}
]
}
Can it be done using groovy? I have tried with HashMap but couldn't get through.
It's quite easy using groovy:
def text = '''{
"book": [
{
"id": "01",
"language": "Java",
"edition": "third",
"author": "Herbert Schildt"
},
{
"id": "07",
"language": "C++",
"edition": "second",
"author": "E.Balagurusamy"
}
]
}
'''
def result = groovy.json.JsonOutput.toJson(
[book: new groovy.json.JsonSlurper().parseText(text).book.findAll{it.author == "Herbert Schildt"}]
)
println result
You may try this ways json search
var json = '{"book":[{"id":"01","language":"Java","edition":"third","author":"Herbert Schildt"},{"id":"07","language":"C++","edition":"second","author":"E.Balagurusamy"}]}';
var parsed = JSON.parse(json);
var result = {};
result.book = [];
var author = "Herbert Schildt";
parsed.book.map((i, j) => {
if(i.author == author) {
result.book.push(i);
}
});
console.log(result)
Below is my JSON format:
{"copybook": {
"item": {
"storage-length": 1652,
"item": [
{
"storage-length": 40,
"level": "05",
"name": "OBJECT-NAME",
"display-length": 40,
"position": 1,
"picture": "X(40)"
},
{
"storage-length": 8,
"occurs-min": 0,
"level": "05",
"name": "C-TCRMANKEYBOBJ-OFFSET",
"numeric": true,
"display-length": 8,
"position": 861,
"occurs": 99,
"depending-on": "C-TCRMANKEYBOBJ-COUNT",
"picture": "9(8)"
}
],
"level": "01",
"name": "TCRMCONTRACTBOBJ",
"display-length": 1652,
"position": 1
},
"filename": "test.cbl"
}}
How can I parse this json and convert it to CSV format? I am using Scala default JSON parser. The main problem I am facing is that I can not use case class to extract the data as all the item names are not same in item array.
This format is ok for me, please follow this link and paste the JSON - https://konklone.io/json/. Any scala code is appreciated. I am getting the below data:
implicit val formats = DefaultFormats
val json2 = parse(jsonString, false) \\ "item"
val list = json2.values.asInstanceOf[List[Map[String, String]]]
for (obj <- list) {
//println(obj.keys)
//obj.values
println (obj.toList.mkString(","))
}
(name,OBJECT-NAME),(storage-length,40),(picture,X(40)),(position,1),(display-length,40),(level,05)
(name,C-TCRMANKEYBOBJ-OFFSET),(storage-length,8),(occurs-min,0),(occurs,99),(picture,9(8)),(position,861),(numeric,true),(depending-on,C-TCRMANKEYBOBJ-COUNT),(display-length,8),(level,05)
https://circe.github.io/circe/ can work better for you in terms of traversing. Just try and read.
Below is my JSON output received post the HttpGet successful execution.
{
"results": [{
"id": "1310760",
"type": "page",
"status": "current",
"title": "UniversalProfile Release Test",
"extensions": {
"position": 9
},
"_links": {
"webui": "/display/ds/UniversalProfile+Release+Test",
"tinyui": "/x/KAAU",
"self": "http:1310760"
},
"_expandable": {
"container": "/rest/api/space/ds",
"metadata": "",
"operations": "",
"children": "/rest/api/content/1310760/child",
"history": "/rest/api/content/1310760/history",
"ancestors": "",
"body": "",
"version": "",
"descendants": "/rest/api/content/1310760/descendant",
"space": "/rest/api/space/ds"
}
}],
"start": 0,
"limit": 25,
"size": 1,
"_links": {
"self": "UniversalProfile+Release+Test",
"base": "https://alim4azad.atlassian.net/wiki",
"context": "/wiki"
}
}
I am trying to extract the value for "id" but have been unsuccessful so far.
If your JSON is in the variable output in Javascript, it'd be :
output.results[0]["id"]
Like console.log(output.results[0]["id"]).
Your results section contains arrays. You want the first (0), and then the key id.
Looking at that resulting JSON hurts my brain.
Assuming you are using JavaScript, try this: output.results[0]['id']
Try This:
JSONObject jsonObject = new JSONObject(data);
JSONArray jsonArray = jsonObject.getJSONArray("results");
JSONObject jsonObject1 = jsonArray.getJSONObject(0).getString("id");
Finally i found a solution for it . Below is the solution.
JSONArray results = getbodyPage.getJSONArray("results");
JSONObject first = results.getJSONObject(0);
Long id = first.getLong("id"); // Get id of the found page
System.out.println("INFO : Found ID - " + id);
Thank you all for your valuable inputs.
I'm using the next Json
{
"ID": 8,
"MenuItems": [
{
"ID": 38,
"Name": "Home",
"URL": "{\"PageLayout\":\"Home\",\"Icon\":\"home\"}",
"Culture": "en",
"Children": null
},
{
"ID": 534,
"Name": "GUIDE ",
"URL": "{''PageLayout'':''Page A'', ''Icon'':''A''}",
"MenuType": 1,
"PageID": 0,
"Culture": "en",
"Children": [
{
"ID": 6,
"Name": "Form A",
"URL": "[''Type'':''Form A'',''Icon'':''Form'',''ItemID'':\"358\"]",
"Culture": "he",
"RuleID": 0
},
{
"ID": 60,
"Name": "Drama",
"URL": "[''Type'':''Form B'',''Icon'':''Form'',''ItemID'':\"3759\"]",
"Culture": "en",
"RuleID": 0
}
]
}
]
}
i'm using Groovy script in soapUI and i need to:
Assert the exitance of node that has the name GUIDE
Extract a list of all Itemsid
You can parse the JSON content using JsonSlurper and then work with the results like so:
import groovy.json.JsonSlurper
// Assuming your JSON is stored in "jsonString"
def jsonContent = new JsonSlurper().parseText(jsonString)
// Assert node exists with name GUIDE
assert(jsonContent.MenuItems.Name.contains("GUIDE"))
// Get list of ItemIDs
def itemList = jsonContent.MenuItems.Children.URL.ItemID[0].toList()
// List the items
itemList.each {log.info it}
Note that the above will fail given your current example, because of a few issues. Firstly, Name contains "GUIDE " (trailing space) rather than "GUIDE" (so you will need to adapt accordingly). Secondly, it is invalid JSON; the URL nodes have various erroneous characters.
On a side note, if you first need to retrieve your JSON from the response associated with a previous TestStep (say one called "SendMessage") within the existing testCase, this can be done as follows:
def jsonString = context.testCase.getTestStepByName("SendMessage").testRequest.response.getContentAsString()