kendoui json fill datagrid - json

This works fine, filling grid:
$("#grid").kendoGrid({
dataSource: {
data: [
{'id': 1, 'name': 2, 'author': 3},
{'id': 1, 'name': 2, 'author': 3},
{'id': 1, 'name': 2, 'author': 3},
] ,
},
but when I load list from getJSON:
$.getJSON('/api/notes/', function(data) {
dataSource = data.rows;
});
Pointing data to dataSource array nothing is displayed :(

If received data is in data.rows, you should do:
$("#grid").data("kendoGrid").dataSource.data = data.rows;
But, why do you not use transport.read in the grid.dataSource for loading data instead of using getJSON?

You should use the data method of the dataSource.
e.g.
$.getJSON('/api/notes/', function(data) {
dataSource.data(data.rows);
});

Related

How can I get all of x values from what I think is JSON

Python version: 3.10
Running a function returns this:
[{'type': 1, 'components': [{'type': 2, 'style': 1, 'label': 'She/Her', 'custom_id': 'She/Her'}, {'style': 1, 'label': 'He/Him', 'custom_id': 'He/Him', 'type': 2}]}]
How can I get all values of 'custom_id' within what is returned? Thank you!
You can do it like so:
myList = [{'type': 1, 'components': [{'type': 2, 'style': 1, 'label': 'She/Her', 'custom_id': 'She/Her'}, {'style': 1, 'label': 'He/Him', 'custom_id': 'He/Him', 'type': 2}]}]
for user in list(myList[0]["components"]):
print(user["custom_id"])
You can format your json here
https://jsonformatter.curiousconcept.com/
to see, wich list is in wich :)
#infinity wrote it similar.
[
{
"type":1,
"components":[
{
"type":2,
"style":1,
"label":"She/Her",
"custom_id":"She/Her"
},
{
"style":1,
"label":"He/Him",
"custom_id":"He/Him",
"type":2
}
]
}
]
myList = [{'type': 1, 'components': [{'type': 2, 'style': 1, 'label': 'She/Her', 'custom_id': 'She/Her'}, {'style': 1, 'label': 'He/Him', 'custom_id': 'He/Him', 'type': 2}]}]
for user in myList[0]['components']:
print(user['custom_id'])

remove k,v from http response in python

I am new to python and am struggling with remove a key and value from a json return by an http request. When querying a task I get the following back.
data = requests.get(url,headers=hed).json()['data']
[{
'gid': '12011553977',
'due_on': None,
'name': 'do something',
'notes': 'blalbla,
'projects': [{
'gid': '120067502445',
'name': 'Project1'
}]
}, {
'gid': '12002408815',
'due_on': '2021-10-21',
'name': 'Proposal',
'notes': 'bla',
'projects': [{
'gid': '12314323523',
'name': 'Project1'
}, {
'gid': '12314323523',
'name': 'Project2'
}, {
'gid': '12314323523',
'name': 'Project3'
}]
I am trying to remove 'gid' from all projects so projects look like this
'projects': [{
'name': 'Company'
}]
What is the best way to do this with python3?
You can use recursion to make a simpler function to handle all elements and sub-elements. I haven't done extensive testing, or included any error checking or exception handling; but this should be close to what you want:
def rec_pop(top_level_list,key_to_pop='gid'):
for item in top_level_list:
item.pop(key_to_pop)
for v in item.values():
if isinstance(v,list):
rec_pop(v)
# call recursive fn
rec_pop(data)
Result:
In [25]: data
Out[25]:
[{'due_on': None,
'name': 'do something',
'notes': 'blalbla',
'projects': [{'name': 'Project1'}]},
{'due_on': '2021-10-21',
'name': 'Proposal',
'notes': 'bla',
'projects': [{'name': 'project2'}]}]

Reading sertain set of key-value pair in JSON using python

suppose I have below json data, my requirement is I only want to parse few key-value pair to be print. Like I want name,description,start & end key-value pair will be printed just by calling print once. All other key-value pair should be skipped while printing. Only the asked above information is needed for my work, so I don't want to keep all other key_value pair.
'events': [
{
'name': {
'text': 'Sample Event',
'html': 'Sample Event'
},
'description': {
'text': 'This is a test event',
'html': '<P>This is a test event</P>'
},
'start': {
'timezone': 'Asia/Kolkata',
'local': '2018-10-12T19:00:00',
'utc': '2018-10-12T13:30:00Z'
},
'end': {
'timezone': 'Asia/Kolkata',
'local': '2018-10-12T22:00:00',
'utc': '2018-10-12T16:30:00Z'
},
'organization_id': '269994560152',
'created': '2018-09-02T15:48:49Z',
'changed': '2018-09-02T15:49:00Z',
'capacity': 1,
'capacity_is_custom': False,
'status': 'live',
'currency': 'USD',
'listed': True,
'shareable': True,
'invite_only': False,
'online_event': False,
'show_remaining': True,
'tx_time_limit': 480,
'hide_start_date': False,
'hide_end_date': False,
'locale': 'en_US',
'is_locked': False,
'privacy_setting': 'unlocked',
'is_series': False,
'is_series_parent': False,
'is_reserved_seating': False,
'show_pick_a_seat': False,
'show_seatmap_thumbnail': False,
'show_colors_in_seatmap_thumbnail': False,
'source': 'create_2.0',
'is_free': True,
'version': '3.0.0',
'logo_id': None,
'category_id': '199',
'subcategory_id': None,
'format_id': '16',
'logo': None
}

How to save data in json file?

I need to save JSON data into a .json file after converting an object to a string using JSON.stringify
This is my current code:
const jsonObject: object = {
'countryName': 'Switzerland',
'users': [
{
'id': 1,
'name': 'Basel',
'founded': -200,
'beautiful': true,
'data': 123,
'keywords': ['Rhine', 'River']
},
{
'id': 1,
'name': 'Zurich',
'founded': 0,
'beautiful': false,
'data': 'no',
'keywords': ['Limmat', 'Lake']
}
]
};
const myData = JSON.stringify(jsonObject);
Note: I want to save this data dynamically, and I was used to jsonConverter of jsonTypescript2.
I tried using this method:-
let a = document.createElement('a');
// JSON.stringify(jsonObject), 'ex.json' // another
a.setAttribute('href', 'data:application/json;charset=UTF-8,' + encodeURIComponent(myData));
Okay after 6 days ago I discover best solution, how to connect between Angular 6 and Node.js + json File directly and dynamically .
npm install file-saver --save
import { saveAs } from 'file-saver';
const jsonObject: object = {
'City': [
{
'id': 1,
'name': 'Basel',
'founded': -200,
'beautiful': true,
'data': 123,
'keywords': ['Rhine', 'River']
},
{
'id': 1,
'name': 'Zurich',
'founded': 0,
'beautiful': false,
'data': 'no',
'keywords': ['Limmat', 'Lake']
}
]
};
const blob = new Blob([JSON.stringify(jsonObject)], {type : 'application/json'});
saveAs(blob, 'abc.json');
You can use fs.js NPM module to write data to file
There are a lot of details in the filesystem API. The most common way (as far as I know) is:
var fs = require('fs');
fs.writeFile("/fileName.json", myData, function(err) {
if(err) {
return console.log(err);
}
console.log("The file was saved!");
});

json to load values into extjs checkboxgroup grid editor

I need to set up a CheckboxGroup checkboxes with values loaded with json from some url.
What is a correct format of JSON?
Many thanks for help!
Update: I'll clarify my problem.
I'm using EditorGridPanel. Rows are Periods. There are columns Start_at, Finish_at, Region. First and second are date and everything ok with them. Problem with Region which actually is a CheckboxGroup with a checkbox for each week day - Monday, Tuesday, etc. So:
First I'm loading data from server into store
function setupDataSource() {
row = Ext.data.Record.create([
{ name: 'start_at', type: 'string' },
{ name: 'finish_at', type: 'string' },
{ name: 'region', type: 'string' }
]);
store = new Ext.data.Store({
url: '/country/195/periods',
reader: new Ext.data.JsonReader(
{
root: 'rows',
id: 'id'
},
row
)
});
store.load();
}
URL url: '/country/195/periods' returns JSON:
{"rows": [{"region": {"cbvert_1": 1, "cbvert_2": 0, "cbvert_3": 1, "cbvert_4": 0, "cbvert_5": 1, "cbvert_6": 0, "cbvert_7": 1}, "start_at": "2010-10-17", "id": 1, "finish_at": "2010-10-28"}]}
Then I'm building a grid:
function buildGrid() {
sm = new Ext.grid.RowSelectionModel();
cm = new Ext.grid.ColumnModel([
// ... Start at and Finish at columns definition here ...
{ header: 'Region',
dataIndex: 'region',
width: 150,
editor: new Ext.form.CheckboxGroup({
xtype: 'checkboxgroup',
columns: 7,
vertical: true,
items: [
{boxLabel: 'M', name: 'cbvert_1', inputValue: 1},
{boxLabel: 'T', name: 'cbvert_2', inputValue: 1},
{boxLabel: 'W', name: 'cbvert_3', inputValue: 1},
{boxLabel: 'T', name: 'cbvert_4', inputValue: 1},
{boxLabel: 'F', name: 'cbvert_5', inputValue: 1},
{boxLabel: 'S', name: 'cbvert_6', inputValue: 1},
{boxLabel: 'S', name: 'cbvert_7', inputValue: 1},
]
}),
renderer: function(value) {}
}]);
// ...
}
So, when I'm clicking on a grid cell I need to get checkboxes preselected with values previously stored in database. Now all checkboxes are stay blank but should be 1010101 as its in JSON.
Please point me to errors or maybe a some kind of a solution. Any help will be greatly appreciated.
Many thanks!
Check this out:
http://www.sencha.com/forum/showthread.php?47243-Load-data-in-checkboxgroup
Updated:
Remove the type declaration of the region field. You do not need a string but an object (from JSON). It works just fine that way :)