jqgrid read in embedded json objects - json

Hey guys so I am currently using jqgrid with json data and it currently reads in fine but i am having some difficulties getting an embeded object to read into the grid. So my json data looks something like this:
{"total":1,"page":"1","records":1,"rows":[{"Cell1":1,"Cell2":"stuff","Cell3":{"date":"2013-06-02 10:56:00","timezone_type":3,"timezone":"UTC"}}]}
Does anyone know how i can get jqgrid to read in Cell3 as one piece of data and interpret it to just display the date and time?
my current json reader is as follows:
jsonReader : {
root:"rows",
page: "page",
total: "total",
records: "records",
repeatitems: false,
id: "0"
}
Thanks again everyone

First of all the options jsonReader: {repeatitems: false, id: "0"} are incorrect. Integer values of id can be used in case of usage default repeatitems: true setting. In the case the data which represent the row of the grid looks like array ["1", "Cell2"] and not as object with named properties {"Cell1": 1, "Cell2": "stuff"}. You should use jsonReader: {repeatitems: false, id: "Cell1"} if Cell1 contains the value which you want to use as unique id of the row of the grid.
Now back to your main question. I recommend you to change format of the data from
{
"total": 1,
"page": "1",
"records": 1,
"rows": [
{
"Cell1": 1,
"Cell2": "stuff",
"Cell3": {
"date": "2013-06-02 10:56:00",
"timezone_type": 3,
"timezone": "UTC"
}
}
]
}
to
{
"total": 1,
"page": "1",
"records": 1,
"rows": [
{
"Cell1": 1,
"Cell2": "stuff"
}
],
"userdata": {
"1": {
"date": "2013-06-02 10:56:00",
"timezone_type": 3,
"timezone": "UTC"
}
}
}
I want comment my suggestion so that it could be clear for other users too. The column Cell1 contains the id. The structure of userdata which I suggest is the map from rowid (the value of Cell1) and the custom information which you need save as "Cell3" originally.
If you need somewhere in your code to have the "Cell3" value the code will be like below
onSelectRow: function (rowid) {
var cell3 = $(this).jqGrid("getGridParam", "userData")[rowid];
// now you can use cell3 which coniains the object like
// {
// "date": "2013-06-02 10:56:00",
// "timezone_type": 3,
// "timezone": "UTC"
// }
}

Related

getting data from an array of objects inside array JSON data

I have trouble taking data from an API set. The body if viewed in Postman / Insomnia is as follows
{
"responses": {
"log": [
{
"id": 123,
"date": "2022-01-01T01:12:12.000Z",
"type": "online",
"details": [{
"detailId": "123-1",
"note": "success",
}]
},
{
"id": 124,
"date": "2022-01-01T01:12:12.000Z",
"type": "offline",
"details": [{
"detailId": "123-2",
"note": "failed",
}]
}
]
}
}
I want to take all data from log, as well from details. I used
adapt(item: any) {
return {
id: item.id,
date: item.date,
details: {
detailId: item.details.detailId,
note: item.details.note,
},
};
}
this returns id and date just fine. I also have a query to filter it based on type (online or offline), basically adding &type= into the API. It works for the online, but it returns detailId is undefined for offline (I used the same body, adapter and API minus the query for both data)
details is an array of object if you want to adapt it you need to do it iteratively.
adapt(item: any) {
const details = item.details.map(d => {detailId: d.id, note: d.note, …});
return {
id: item.id,
date: item.date,
details
};
}
Found the answer, apparently to make sure that I can get every value is to add ? after the [0], so it should be
details: {
detailId: item.details[0]?.detailId,
note: item.details[0]?.note,
},

Extract values from a parsed json object using a function node or split & switch node in NODE.red

Was trying to extract values from a parsed json object using a function node or split & switch node, tried to many ways and nothing seems to work fro me.
the example below I would like to have 5 outputs for the text and numeric values.
here is my payload in json:
{
"applicationID": "1",
"applicationName": "test_ds18b20",
"deviceName": "arduino_uno",
"devEUI": "1234567890123456",
"rxInfo": [
{
"mac": "aa755a0048050130",
"rssi": -57,
"loRaSNR": 10,
"name": "raspberry_pi",
"latitude": 1.466860686785175,
"longitude": 2.019478797912605,
"altitude": 0
}
],
"txInfo": {
"frequency": 868100000,
"dataRate": {
"modulation": "LORA",
"bandwidth": 125,
"spreadFactor": 7
},
"adr": true,
"codeRate": "4/5"
},
"fCnt": 9,
"fPort": 1,
"data": "Z29vZGJ5ZQ==",
"object": {}
}
first i try with function node to extract "data", but it returns array like this:
0: ""data":"Z29vZGJ5ZQ==""
1: "Z29vZGJ5ZQ=="
i dont need array i need string
function:
var regexsearch = /\"data\":\"(.*?)\"/i;
var my = msg.payload.match(regexsearch);
msg.payload = my;
return msg;
but i need to get only this Z29vZGJ5ZQ==
than i try with split & switch nodes and gets the whole linelike this: ""data":"Z29vZGJ5ZQ==""
but i need to get only this Z29vZGJ5ZQ==
and here is my flow:
[{"id":"d46d38e2.27cc78","type":"inject","z":"ff592a31.cf21a8","name":"","topic":"","payload":"{\"applicationID\":\"1\",\"applicationName\":\"test_ds18b20\",\"deviceName\":\"arduino_uno\",\"devEUI\":\"1234567890123456\",\"rxInfo\":[{\"mac\":\"aa755a0048050130\",\"rssi\":-57,\"loRaSNR\":10,\"name\":\"raspberry_pi\",\"latitude\":48.466860686785175,\"longitude\":35.019478797912605,\"altitude\":0}],\"txInfo\":{\"frequency\":868100000,\"dataRate\":{\"modulation\":\"LORA\",\"bandwidth\":125,\"spreadFactor\":7},\"adr\":true,\"codeRate\":\"4/5\"},\"fCnt\":9,\"fPort\":1,\"data\":\"Z29vZGJ5ZQ==\",\"object\":{}}","payloadType":"json","repeat":"","crontab":"","once":false,"x":90,"y":160,"wires":[["1a34819e.743eee"]]},{"id":"105db6d9.0df1c9","type":"debug","z":"ff592a31.cf21a8","name":"","active":true,"console":"false","complete":"false","x":610,"y":100,"wires":[]},{"id":"1ac8a3e1.8f379c","type":"split","z":"ff592a31.cf21a8","name":"","splt":",","spltType":"str","arraySplt":"1","arraySpltType":"len","stream":false,"addname":"","x":250,"y":340,"wires":[["c10ec515.102d38"]]},{"id":"c10ec515.102d38","type":"switch","z":"ff592a31.cf21a8","name":"","property":"payload","propertyType":"msg","rules":[{"t":"cont","v":"\"data\":","vt":"str"},{"t":"cont","v":"\"latitude\":","vt":"str"}],"checkall":"true","outputs":2,"x":370,"y":340,"wires":[["105db6d9.0df1c9"],["6b2d5d19.7868e4"]]},{"id":"1a34819e.743eee","type":"json","z":"ff592a31.cf21a8","name":"","pretty":false,"x":115.55555555555556,"y":312.22222222222223,"wires":[["1ac8a3e1.8f379c","bae9fa5d.a9f238"]]},{"id":"bae9fa5d.a9f238","type":"function","z":"ff592a31.cf21a8","name":"match","func":"var regexsearch = /\\\"data\\\":\\\"(.*?)\\\"/i;\nvar my = msg.payload.match(regexsearch);\nmsg.payload = my;\nreturn msg;","outputs":1,"noerr":0,"x":310,"y":160,"wires":[["105db6d9.0df1c9"]]},{"id":"6b2d5d19.7868e4","type":"debug","z":"ff592a31.cf21a8","name":"","active":true,"console":"false","complete":"false","x":610,"y":180,"wires":[]}]
Thanks for help

jqGrid: formatter: "actions" doesn't work for remote json url

I tried to use jqGrid formatter: "actions" for remote json url but it shifts rows because number of column names and actual returned data doesn't match (act column is extra one in column names). I did search online and it looks like all examples are for local data (on page) where json is used as key/value, while url:somepage.php returns a csv kind-of-format of columns(no keys, just values). Also I use id as hidden field, while all examples show id. I use one more hidden column as well.
here is my colmodel:
url: "manager_json",
editurl: "manager_edit",
datatype: "json",
//data: mydata,
width:1000,
//jsonReader: {
// repeatitems : false,
// },
colNames: ["","Id","Uid","Custom Id","Image Name","Coord. X","Coord. Y","Gender","Progress","Status","Created","Updated"],
colModel: [{"name":"act","template":"actions","formatoptions":{"editformbutton":true}},{"name":"id","align":"center","width":33,"editable":false,"hidden":true},{"name":"uid","align":"center","width":33,"editable":true,"hidden":true,"editrules":{"edithidden":false,"required":false}},{"name":"ta_id","align":"center","width":100,"editrules":{"required":true}},{"name":"image","width":150,"template":"text","editrules":{"required":true}},{"name":"x","width":100,"align":"center","template":"integer","editrules":{"required":true}},{"name":"y","width":100,"align":"center","template":"integer","editrules":{"required":true}},{"name":"gender","width":100,"align":"center","formatter":"select","stype":"select","editrules":{"required":true},"edittype":"select","editoptions":{"value":"f:Female;m:Male","defaultValue":"m"},"searchoptions":{"sopt":["eq","ne"],"value":":Any;f:Female;m:Male"}},{"name":"progress","width":120,"align":"center","formatter":"select","stype":"select","editrules":{"required":true},"editable":false,"edittype":"select","editoptions":{"value":"-:New;pf:Process Failed;ps:Process Scheduled;pss:Process Success;p:Processing...;s:Staging;tf:Test Failed;ts:Test Scheduled;tss:Test Success;t:Testing...","defaultValue":"m"},"searchoptions":{"sopt":["eq","ne"],"value":":Any;-:New;pf:Process Failed;ps:Process Scheduled;pss:Process Success;p:Processing...;s:Staging;tf:Test Failed;ts:Test Scheduled;tss:Test Success;t:Testing..."}},{"name":"status","width":100,"align":"center","formatter":"select","stype":"select","edittype":"select","editoptions":{"value":"a:Active;n:New","defaultValue":"m"},"searchoptions":{"sopt":["eq","ne"],"value":":Any;a:Active;n:New"}},{"name":"date_created","width":150,"template":"text","editable":false},{"name":"last_updated","width":150,"template":"text","editable":false}]
And here is my actual data from my server (db) as per jqgrid specification:
{"page":1,"total":1,"records":7,"rows":[{"id":32,"cell":[32,"889daf31ff3e49544f52850258439600","2uu","2ok",2,2,"m","-","a","2017-02-10 18:57:05","2017-02-10 23:37:12"]},{"id":30,"cell":[30,"","11","11",1,1,"m","-","a","2017-02-10 18:01:52","2017-02-10 18:01:52"]},{"id":29,"cell":[29,"aaa","ww222111uu","11",1,1,"m","-","a","2017-02-10 18:00:36","2017-02-10 23:37:08"]},{"id":27,"cell":[27,"","11","1",1,1,"m","-","a","2017-02-10 17:57:41","2017-02-10 17:57:41"]},{"id":25,"cell":[25,"","4tt","4img",4,4,"f","-","n","2017-02-10 17:50:21","2017-02-11 00:26:03"]},{"id":24,"cell":[24,"","1","1",1,1,"m","-","a","2017-02-10 17:49:38","2017-02-10 17:49:38"]},{"id":22,"cell":[22,"","bbb","imam 222",2,22,"f","p","n","2017-02-08 20:14:55","2017-02-10 13:27:57"]}]}
As you can see cell row has less columns than colNames and colModel. Also id and uid columns are hidden. I added on extra fake column from db, but it did not help
i am using jqGrid 4.13.7-pre
I'd recommend you to return objects like
{
"id": 32,
"uid": "889daf31ff3e49544f52850258439600",
"ta_id": "2uu",
"image": "2ok",
"x": 2,
"y": 2,
"gender": "m",
"progress": "-",
"status": "a",
"date_created": "2017-02-10 18:57:05",
"last_updated": "2017-02-10 23:37:12"
}
instead of
{
"id": 32,
"cell": [
32, "889daf31ff3e49544f52850258439600", "2uu", "2ok", 2, 2, "m", "-", "a",
"2017-02-10 18:57:05", "2017-02-10 23:37:12"
]
}
as the server response. It will simplify your code.
If you do need to process the current format of the data, then you will have to force repeatitems: false mode by usage the option
jsonReader: {
repeatitems: false
}
and to use jsonmap property of colModel to inform jqGrid from which element of the item one should read the column data. The jsonmap could be either in the string form like
jsonmap: "cell.1"
or in the function form:
jsonmap: function (item) {
return item.cell[1];
}
To make the code better maintainable you can define an object like
var columnOrder = {
id: 0,
uid: 1,
ta_id: 2,
image: 3,
x: 4,
y: 5,
gender: 6,
progress: 7,
status: 8,
date_created: 9,
last_updated: 10
};
which provides the index in cell array by the column name and to use jsonmap like
jsonmap: function (item) {
return item.cell[columnOrder.uid];
}
The demo https://jsfiddle.net/OlegKi/ozzgnaeh/2/ demonstrates the approach.

SAPUI5: Filtering doesn't work

I have two json-Mock Files:
Types.json:
[{
"TypeID": 1
}, {
"TypeID": 2
}]
Items.json:
[{
"ItemID": 12345,
"TypeID": "1",
}, {
"ItemID": 54321,
"TypeID": "2",
}]
My List Binding:
oView.bindElement({
path : "/Items",
filters: [ new sap.ui.model.Filter("TypeID", sap.ui.model.FilterOperator.EQ, oArgs.TypeID) ],
events : {
change: this._onBindingChange.bind(this),
dataRequested: function (oEvent) {
oView.setBusy(true);
},
dataReceived: function (oEvent) {
oView.setBusy(false);
}
}
});
My code ignores the filter and shows both Items although only one of them have the right TypeID (1 resp 2).
Console Output: oArgs.TypeID is defined and contains 1 resp 2.
Element binding is meant for binding UI elements to a specific object in the model data. You are trying to use bindElement to bind UI elements to a set of data.
Because bindElement always binds to a singleton, the filter parameter doesn't make sense and does not exist.

extJS: reading a nested JSON

I have a pretty nested JSON coming from a ldap_search() call. I would like to use this information to populate an ExtJS ComboBox, but I am facing some troubles with the reader. Apparently, I am not able to read the information that I need in the ComboBox, that is the mail address of the people, the uid and the cn
I think the whole problem lies in the store. I was trying the following code:
var store= new Ext.data.JsonStore({
url:'search.php',
root: '',
totalProperty: 'count',
fields: [
{name:'cn', type: 'string', mapping:'cn.0'},
{name:'mail', type: 'string', mapping:'mail.0'},
{name:'uid', type: 'string', mapping:'uid.0'}
]
});
but FireBug told me missing ; before statement return obj.cn.0 in ext-all.js (line 7). I tried with another, easier JSON array and it works, that is why I really think the problem lies in this part of code, especially in the mapping.
an example of JSON returned by search.php is:
{
"count": 2,
"0": {
"mail": {
"count": 1,
"0": "Mail address not registered."
},
"0": "mail",
"uid": {
"count": 1,
"0": "name0.surname0#domain.com"
},
"1": "uid",
"cn": {
"count": 1,
"0": "Surname0 Name0"
},
"2": "cn",
"count": 3,
"dn": "cn=Surname0 Name0,ou=personal,dc=domain,dc=com"
},
"1": {
"mail": {
"count": 1,
"0": "name1.surname1#domain.com"
},
"0": "mail",
"uid": {
"count": 1,
"0": "name1.surname1"
},
"1": "uid",
"cn": {
"count": 1,
"0": "Surname 1 Name 1"
},
"2": "cn",
"count": 3,
"dn": "cn=Surname1 Name1,ou=personal,dc=domain,dc=com"
}
}
Thanks for your time.
Yep, that JSON structure is not going to work straight away with standard ExtJS JSONReader. Take a look at this example taken from the ExtJS API documentation on how the JSON should look like.
{
results: 2000, // Reader's configured totalProperty
rows: [ // Reader's configured root
// record data objects:
{ id: 1, firstname: 'Bill', occupation: 'Gardener' },
{ id: 2, firstname: 'Ben' , occupation: 'Horticulturalist' },
...
]
}
Also, the root config option is required, you cannot leave it empty. In the above example your root would be "rows".
You are probably going to need to parse that JSON of yours into a simpler format at first, before feeding it to the JSONReader.
I was looking to do the same thing, but have one of the nested items be a field in my chart. This post kept coming up, so I thought it might be helpful to see what I did to solve the chart issue. The key to solving it is knowing that the label config exists: http://docs.sencha.com/ext-js/4-0/#!/api/Ext.chart.Label. Using that you can override the default render of what you pass in. In this example the field is "key" (Not shown here, but my model is using the default type for 'key' (ie., not string)). The key object gets passed to renderer. Using function(t), I can now access that object like javascript and pass back the name under the object.
json
key : {
wholePath : "c:/.../fileName.txt",
fileName : "fileName.txt",
}
code:
axes: [
{
title: 'Values',
type: 'Numeric',
position: 'left',
fields: ['value'],
minimum: 0,
maximum: 100,
minorTickSteps: 1
},
{
title: 'File Name',
type: 'Category',
position: 'bottom',
fields: ['key'],
label: {
renderer: function(t) {
var fileName = t.name;
return fileName;
}
}
}