How to parse, and render, json for use in Django template - html

I'm a newbie in Django and trying to learn, but I'm confused about how I can render data pulled from URL in a template in Django and display it in the HTML page.
The json data sample is:
{
"docs":
[
{
"hostIP": "X.X.X.X",
"time": "August 13, 2018 13:43:44",
"site":
[
{
"site": "site1",
"path": "/path/to/site1",
"git_branch": "master",
"git_commit_message": "New changes"
},
{
"site": "site2",
"path": "/path/to/site2",
"git_branch": "master",
"git_commit_message": "add card"
}
]
}
]
}
Also how i can loop it using Jinja2? Please someone help me out of this.

In your view code, parse it with json.loads():
import json
data = json.loads(my_json_data)
Then pass in data as a context variable to the view. Then you can see these variables and loop over them how you want in the template.

Related

Fixing local JSON file for working with structs

I have a local json file like this
{
"scroll": [
{
"id": 0,
"titles": "1",
"course": ["first", "Second"]
},
{
"id": 1,
"titles": "2",
"course": ["Third", "Fourth"]
}
]
}
So, I want that every item of "course" has its own string parameter. For example, "Third" has "third" parameter, "Fourth" has "fourth" parameter and so on. But I don't know how to do it in json file. I want that clicking "first" button in the list it navigate to the text that is written in json, and after clicking in "Second " button it navigate to another text. Please help me to solve this problem. Thanks.

Azure Data Factory - attempting to add params to dynamic content in the body of a REST API request

In Azure Data Factory, I'm attempting to add params to the body of a copy task (connected to a REST API post request as the source). I'm wanting to use dynamic content to do so, but I'm struggling trying to find the real solution for the proper nomenclature. Here's what I have so far.
copy task
dynamic content
{
"datatable":
{
"start":0,
"length": 10000,
"filters": [
{
"name": "Arrival Dates",
"start": "pipeline().parameters.pDate1",
"end": "pipeline().parameters.pDate2"
}
],
"sort": [
{
"name": "start_date",
"order": "ASC"
}
]
}
}
You'll notice that I've added params for dates. Is this the correct nomenclature for trying to add dynamic content? The autocorrect tried to add the # sign in the beginning of the code block, which will cause the entire thing to error out. I've tried adding it before each parameter, but that isn't actually reading the dynamic values either.
This is not correct. You need to use concat to concatenate the different variables. Something like this :
#concat('{ "datatable": { "start":0, "length": 10000, "filters": [ { "name": "Arrival Dates", "start": "',pipeline().parameters.pDate1,'", "end": "',pipeline().parameters.pDate2,'" } ], "sort": [ { "name": "start_date", "order": "ASC" } ] } }')
This is also documented in the SO question.

Optimum Serializing in Django Rest Framework and parsing in Javascript

I am trying to adapt an event calendar vuejs module called dayspan calendar. Current entry object for an event as json is a bit strange and I want to balance the parsing of the payload before the post request and handling of data in DRF serializers. So I can get an optimum and performant client-server rest API communication. Json output is as below before any parsing:
{
"data": {
"title": "YOK",
"description": "",
"location": "",
"color": "#1976d2",
"forecolor": "#ffffff",
"calendar": "",
"busy": true,
"icon": ""
},
"schedule": {
"times": [
"17"
],
"dayOfMonth": [
11
],
"year": [
2021
],
"month": [
5
]
}
}
There are more schedule fields like "dayOFWeek", "duration" etc. that is to be expected for different entries.
What would be the best approach in terms of parsing json payload before posting to DRF and in deserializing stage before saving into database? I appreciate any ideas.

i have reactive form /dynamic form of attendance system for Employe in Angular.how i will generate json like this for backend

i have reactive form /dynamic form of attendance system for Employe .when i click on submit ,i want to generate json like this.
{
"user_id": "1",
"branch_id": "4",
"auth_token": "59a2a9337afb07255257199b03ed6076",
"date": "2019-11-12",
"attendance_log": [
{
"emp_id": "1",
"status": "Preset"
},
{
"emp_id": "1",
"status": "Preset"
},
{
"emp_id": "1",
"status": "Preset"
}]
}
and i want to pass json in backend to API.
how i will do?
You can use
form.value() this will return only enabled control values.
form.getRawValue() this will return JSON object for all of your controls enabled and as well disabled.
Then you can pass this JSON via post, hope that will solve your issue.

Parsing through JSON .. Gives undefined?

I have a very complex JSON and a snippet of it is below:
var designerJSON=
{
"nodes":
[
{
"NodeDefinition": {
"name": "Start",
"thumbnail": "Start.png",
"icon": "Start.png",
"info": "Entry point ",
"help": "Start point in your workflow.",
"workflow ": "Start",
"category": "Basic",
"ui": [
{
"label": "Entry point",
"category": "Help",
"componet": "label",
"type": "label"
}
]
},
"States": [
{
"start": "node1"
}
]
},.......
]
}
I would like to get the value of "start" in States. But I am stuck in the first step of entering into JSON. When I try
console.log(designerJSON["nodes"]);
I am getting Undefined.
I want the value of start. Wich is designerJSON["nodes"]["States"]["start"].
Can you help.
Thanks in advance
designerJSON["nodes"]["States"]["start"] won't do it.
designerJSON["nodes"] is a list, as is States, so you need to access individual items by index (or iteration).
In the example you have given you need to use this:
designerJSON['nodes'][0]['States'][0]['start']
or this (cleaner IMO):
designerJSON.nodes[0].States[0].start
You have an array in JSON.
instead of
designerJSON["nodes"]["States"]["start"]
use
designerJSON["nodes"][0]["States"][0]["start"]
ps. pay attention on how code is formatted in the topic.
pps. using brackets for accessing properties in js is "bad style" (due to js hint recommendations). better access those via dot, e.g:
designerJSON.nodes[0].States[0].start