Get array item by 1-indexed value in another field - json

The Firefox file sessionstore-backups/recovery.jsonlz4 contains information about the current Firefox windows in (compressed) JSON format. This is a simplified format representative of that file:
{
"index": 2,
"entries": [
{
"title": "Foo bar 0",
"url": "https://example.com/bar"
},
{
"title": "Foo bar 1",
"url": "https://example.com/bar"
},
{
"title": "Foo bar 2",
"url": "https://example.com/bar"
}
]
}
The index field represents the currently-displayed history item per tab. Being 1-indexed, in the example above it represents the "Foo bar 1" entry.
How can I use that value in jq to get that tab?
For testing on your own system, the following bash line will get you to this point on most modern Linux systems:
lz4jsoncat ~/.mozilla/firefox/t6e99qbe.default-release/sessionstore-backups/recovery.jsonlz4 | jq '.windows[0].tabs' | jq 'sort_by(.lastAccessed)[-1]'
This returns the most recent tab, but with all history items in the entries array. As the user could have navigated back in the history, we need the index value to determine which entry to return. Piped through gron, it looks like this:
json = {};
json.attributes = {};
json.entries = [];
json.entries[0] = {};
json.entries[0].ID = 504;
json.entries[0].cacheKey = 0;
json.entries[0].docIdentifier = 586;
json.entries[0].docshellUUID = "{b6c1e18a-d0f5-4ba2-996e-ddd9fc28322a}";
json.entries[0].hasUserInteraction = true;
json.entries[0].persist = true;
json.entries[0].principalToInherit_base64 = "eyIxIjp7IjAiOiJtb3otZXh0ZW5zaW9uOi8vOTAzYWU2NDgtMzRlNi00NGQ3LTg0NjYtYzgxMWQyMTg2YjBkLyJ9fQ==";
json.entries[0].resultPrincipalURI = null;
json.entries[0].title = "Tridactyl Top Tips & New Tab Page";
json.entries[0].triggeringPrincipal_base64 = "eyIxIjp7IjAiOiJtb3otZXh0ZW5zaW9uOi8vOTAzYWU2NDgtMzRlNi00NGQ3LTg0NjYtYzgxMWQyMTg2YjBkLyJ9fQ==";
json.entries[0].url = "moz-extension://7ba85bcd-8d63-4e74-a723-e627a31d6274/static/newtab.html";
json.entries[1] = {};
json.entries[1].ID = 506;
json.entries[1].cacheKey = 0;
json.entries[1].docIdentifier = 588;
json.entries[1].docshellUUID = "{b6c1e18a-d0f5-4ba2-996e-ddd9fc28322a}";
json.entries[1].hasUserInteraction = true;
json.entries[1].persist = true;
json.entries[1].referrerInfo = "BBoSnxDOS9qmDeAnom1e0AAAAAAAAAAAwAAAAAAAAEYAAAAAAAEBAAAAAAEA";
json.entries[1].resultPrincipalURI = null;
json.entries[1].title = "Stack Overflow - Where Developers Learn, Share, & Build Careers";
json.entries[1].triggeringPrincipal_base64 = "eyIzIjp7fX0=";
json.entries[1].url = "https://stackoverflow.com/";
json.entries[2] = {};
json.entries[2].ID = 508;
json.entries[2].cacheKey = 0;
json.entries[2].csp = "CdntGuXUQAS/4CfOuSPZrAAAAAAAAAAAwAAAAAAAAEYB3pRy0IA0EdOTmQAQS6D9QJIHOlRteE8wkTq4cYEyCMYAAAAC/////wAAAbsBAAAAGmh0dHBzOi8vc3RhY2tvdmVyZmxvdy5jb20vAAAAAAAAAAUAAAAIAAAAEQAAAAj/////AAAACP////8AAAAIAAAAEQAAABkAAAABAAAAGQAAAAEAAAAZAAAAAQAAABoAAAAAAAAAGv////8AAAAA/////wAAABn/////AAAAGf////8BAAAAAAAAAAAAKHsiMSI6eyIwIjoiaHR0cHM6Ly9zdGFja292ZXJmbG93LmNvbS8ifX0AAAABAAAASwB1AHAAZwByAGEAZABlAC0AaQBuAHMAZQBjAHUAcgBlAC0AcgBlAHEAdQBlAHMAdABzADsAIABmAHIAYQBtAGUALQBhAG4AYwBlAHMAdABvAHIAcwAgACcAcwBlAGwAZgAnACAAaAB0AHQAcABzADoALwAvAHMAdABhAGMAawBlAHgAYwBoAGEAbgBnAGUALgBjAG8AbQAA";
json.entries[2].docIdentifier = 590;
json.entries[2].docshellUUID = "{b6c1e18a-d0f5-4ba2-996e-ddd9fc28322a}";
json.entries[2].hasUserInteraction = true;
json.entries[2].persist = true;
json.entries[2].principalToInherit_base64 = "eyIxIjp7IjAiOiJodHRwczovL3N0YWNrb3ZlcmZsb3cuY29tLyJ9fQ==";
json.entries[2].referrerInfo = "BBoSnxDOS9qmDeAnom1e0AAAAAAAAAAAwAAAAAAAAEYBAAAAGmh0dHBzOi8vc3RhY2tvdmVyZmxvdy5jb20vAAAACAEBAAAAGmh0dHBzOi8vc3RhY2tvdmVyZmxvdy5jb20vAQE=";
json.entries[2].resultPrincipalURI = null;
json.entries[2].title = "Highest scored questions - Stack Overflow";
json.entries[2].triggeringPrincipal_base64 = "eyIxIjp7IjAiOiJodHRwczovL3N0YWNrb3ZlcmZsb3cuY29tLyJ9fQ==";
json.entries[2].url = "https://stackoverflow.com/questions";
json.entries[3] = {};
json.entries[3].ID = 510;
json.entries[3].cacheKey = 0;
json.entries[3].csp = "CdntGuXUQAS/4CfOuSPZrAAAAAAAAAAAwAAAAAAAAEYB3pRy0IA0EdOTmQAQS6D9QJIHOlRteE8wkTq4cYEyCMYAAAAC/////wAAAbsBAAAAI2h0dHBzOi8vc3RhY2tvdmVyZmxvdy5jb20vcXVlc3Rpb25zAAAAAAAAAAUAAAAIAAAAEQAAAAj/////AAAACP////8AAAAIAAAAEQAAABkAAAAKAAAAGQAAAAoAAAAZAAAAAQAAABoAAAAJAAAAGv////8AAAAA/////wAAABn/////AAAAGf////8BAAAAAAAAAAAAMXsiMSI6eyIwIjoiaHR0cHM6Ly9zdGFja292ZXJmbG93LmNvbS9xdWVzdGlvbnMifX0AAAABAAAASwB1AHAAZwByAGEAZABlAC0AaQBuAHMAZQBjAHUAcgBlAC0AcgBlAHEAdQBlAHMAdABzADsAIABmAHIAYQBtAGUALQBhAG4AYwBlAHMAdABvAHIAcwAgACcAcwBlAGwAZgAnACAAaAB0AHQAcABzADoALwAvAHMAdABhAGMAawBlAHgAYwBoAGEAbgBnAGUALgBjAG8AbQAA";
json.entries[3].docIdentifier = 592;
json.entries[3].docshellUUID = "{b6c1e18a-d0f5-4ba2-996e-ddd9fc28322a}";
json.entries[3].hasUserInteraction = true;
json.entries[3].persist = true;
json.entries[3].principalToInherit_base64 = "eyIxIjp7IjAiOiJodHRwczovL3N0YWNrb3ZlcmZsb3cuY29tL3F1ZXN0aW9ucyJ9fQ==";
json.entries[3].referrerInfo = "BBoSnxDOS9qmDeAnom1e0AAAAAAAAAAAwAAAAAAAAEYBAAAAI2h0dHBzOi8vc3RhY2tvdmVyZmxvdy5jb20vcXVlc3Rpb25zAAAACAEBAAAAI2h0dHBzOi8vc3RhY2tvdmVyZmxvdy5jb20vcXVlc3Rpb25zAQE=";
json.entries[3].resultPrincipalURI = null;
json.entries[3].title = "Tags - Stack Overflow";
json.entries[3].triggeringPrincipal_base64 = "eyIxIjp7IjAiOiJodHRwczovL3N0YWNrb3ZlcmZsb3cuY29tL3F1ZXN0aW9ucyJ9fQ==";
json.entries[3].url = "https://stackoverflow.com/tags";
json.entries[4] = {};
json.entries[4].ID = 512;
json.entries[4].cacheKey = 0;
json.entries[4].csp = "CdntGuXUQAS/4CfOuSPZrAAAAAAAAAAAwAAAAAAAAEYB3pRy0IA0EdOTmQAQS6D9QJIHOlRteE8wkTq4cYEyCMYAAAAC/////wAAAbsBAAAAHmh0dHBzOi8vc3RhY2tvdmVyZmxvdy5jb20vdGFncwAAAAAAAAAFAAAACAAAABEAAAAI/////wAAAAj/////AAAACAAAABEAAAAZAAAABQAAABkAAAAFAAAAGQAAAAEAAAAaAAAABAAAABr/////AAAAAP////8AAAAZ/////wAAABn/////AQAAAAAAAAAAACx7IjEiOnsiMCI6Imh0dHBzOi8vc3RhY2tvdmVyZmxvdy5jb20vdGFncyJ9fQAAAAEAAABLAHUAcABnAHIAYQBkAGUALQBpAG4AcwBlAGMAdQByAGUALQByAGUAcQB1AGUAcwB0AHMAOwAgAGYAcgBhAG0AZQAtAGEAbgBjAGUAcwB0AG8AcgBzACAAJwBzAGUAbABmACcAIABoAHQAdABwAHMAOgAvAC8AcwB0AGEAYwBrAGUAeABjAGgAYQBuAGcAZQAuAGMAbwBtAAA=";
json.entries[4].docIdentifier = 594;
json.entries[4].docshellUUID = "{b6c1e18a-d0f5-4ba2-996e-ddd9fc28322a}";
json.entries[4].hasUserInteraction = false;
json.entries[4].persist = true;
json.entries[4].principalToInherit_base64 = "eyIxIjp7IjAiOiJodHRwczovL3N0YWNrb3ZlcmZsb3cuY29tL3RhZ3MifX0=";
json.entries[4].referrerInfo = "BBoSnxDOS9qmDeAnom1e0AAAAAAAAAAAwAAAAAAAAEYBAAAAHmh0dHBzOi8vc3RhY2tvdmVyZmxvdy5jb20vdGFncwAAAAgBAQAAAB5odHRwczovL3N0YWNrb3ZlcmZsb3cuY29tL3RhZ3MBAQ==";
json.entries[4].resultPrincipalURI = null;
json.entries[4].title = "Users - Stack Overflow";
json.entries[4].triggeringPrincipal_base64 = "eyIxIjp7IjAiOiJodHRwczovL3N0YWNrb3ZlcmZsb3cuY29tL3RhZ3MifX0=";
json.entries[4].url = "https://stackoverflow.com/users";
json.hidden = false;
json.index = 3;
json.lastAccessed = 1651407567904;
json.requestedIndex = 0;
json.searchMode = null;
json.userContextId = 0

Just index entries by index minus one?
.entries[.index - 1]
Online demo

The last item in entries is not necessarily the tab that was opened. [.index - 1] only works if user_pref("browser.sessionstore.max_serialize_forward", 0); cf. https://wiki.mozilla.org/Firefox/session_restore#Browser_settings

Related

How to Create Nested Json Using ArduinoJson on ESP8266

I want this Output Json From Esp8266
I am trying with another json lib https://github.com/arduino-libraries/Arduino_JSON
this is lightweight library than arduinojson.org
{
"ID": "0785E7",
"DEVICE": "SINGLE_RELAY",
"Public_Name": "Demo Switch",
"version": 1,
"payloadVersion": 1,
"SW": 1,
"HW": 1,
"CHIPSIZE": 1048576,
"CHIPSPEED": 40000000,
"CHIPMODE": 3,
"capabilities": [
{
"interface": "Demo.Switch",
"type": "ONOFF",
"version": "1.0",
"sync": true,
"control_pin": 2,
"MQTT_SWITCH_TOPIC": "on",
"MQTT_SWITCH_SYNC": "sync"
},
{
"interface": "Demo.Countdown",
"type": "Countdown",
"version": "1.0",
"sync": true,
"control_pin": 2,
"MQTT_SWITCH_TOPIC": "s",
"MQTT_SWITCH_SYNC": "x"
},
{
"interface": "Demo.Schedule",
"type": "Schedule",
"version": "1.0",
"sync": true,
"control_pin": 2,
"MQTT_SWITCH_TOPIC": "s",
"MQTT_SWITCH_SYNC": "x"
}
]
}
Arduino Function
String Device_info()
{
JSONVar json;
json["ID"] = ESPID;
json["DEVICE"] = DEVICE_TYPE;
json["Public_Name"] = DEVICE_CHANNEL;
json["version"] = relaystatus1;
json["payloadVersion"] = RelayPIN;
json["SW"] = userid;
json["HW"] = Trigger;
json["CHIPSIZE"] = rssi_str;
json["CHIPSPEED"] = "1";;
String jsonString = JSON.stringify(json);
}
i want to add "capabilities" objectArray in this Like Above json.
The createNestedObject() method can be used to create the capabilities objects inside a nested array.
The ArduinoJson Assistant suggests the following code:
const size_t capacity =
JSON_ARRAY_SIZE(3) + 3*JSON_OBJECT_SIZE(7) + JSON_OBJECT_SIZE(11);
DynamicJsonDocument doc(capacity);
doc["ID"] = "0785E7";
doc["DEVICE"] = "SINGLE_RELAY";
doc["Public_Name"] = "Demo Switch";
doc["version"] = 1;
doc["payloadVersion"] = 1;
doc["SW"] = 1;
doc["HW"] = 1;
doc["CHIPSIZE"] = 1048576;
doc["CHIPSPEED"] = 40000000;
doc["CHIPMODE"] = 3;
JsonArray capabilities = doc.createNestedArray("capabilities");
JsonObject capabilities_0 = capabilities.createNestedObject();
capabilities_0["interface"] = "Demo.Switch";
capabilities_0["type"] = "ONOFF";
capabilities_0["version"] = "1.0";
capabilities_0["sync"] = true;
capabilities_0["control_pin"] = 2;
capabilities_0["MQTT_SWITCH_TOPIC"] = "on";
capabilities_0["MQTT_SWITCH_SYNC"] = "sync";
JsonObject capabilities_1 = capabilities.createNestedObject();
capabilities_1["interface"] = "Demo.Countdown";
capabilities_1["type"] = "Countdown";
capabilities_1["version"] = "1.0";
capabilities_1["sync"] = true;
capabilities_1["control_pin"] = 2;
capabilities_1["MQTT_SWITCH_TOPIC"] = "s";
capabilities_1["MQTT_SWITCH_SYNC"] = "x";
JsonObject capabilities_2 = capabilities.createNestedObject();
capabilities_2["interface"] = "Demo.Schedule";
capabilities_2["type"] = "Schedule";
capabilities_2["version"] = "1.0";
capabilities_2["sync"] = true;
capabilities_2["control_pin"] = 2;
capabilities_2["MQTT_SWITCH_TOPIC"] = "s";
capabilities_2["MQTT_SWITCH_SYNC"] = "x";
serializeJson(doc, Serial);
JSONVar supported;
JSONVar Countdown;
Countdown["interface"] = interface_countdown;
Countdown["type"] = interface_countdown_type;
Countdown["version"] = version_1;
Countdown["retrievable"] = true;
Countdown["MIN_COUNT"] = 1;
Countdown["MAX_COUNT"] = COUNTDOWNCOUNT;
Countdown["control_pin"] = RELAY_PIN;
Countdown["MQTT_TOPIC"] = MQTT_COUNTDOWN_IN;
Countdown["MQTT_SYNC"] = MQTT_COUNTDOWN_IN;
JSONVar schedule;
schedule["interface"] = interface_Schedule;
schedule["type"] = interface_Schedule_type;
schedule["version"] = version_1;
schedule["retrievable"] = true;
schedule["control_pin"] = RELAY_PIN;
schedule["MIN_COUNT"] = 1;
schedule["MAX_COUNT"] = SCHEDUECOUNT;
schedule["MQTT_TOPIC"] = MQTT_TIMMER_IN;
schedule["MQTT_SYNC"] = MQTT_TIMMER_IN;
supported[0] = Countdown;
supported[1] = schedule;
JSONVar power_control;
power_control["interface"] = interface_power;
power_control["type"] = interface_power_type;
power_control["version"] = version_1;
power_control["retrievable"] = true;
power_control["proactivelyReported"] = true;
power_control["control_pin"] = RELAY_PIN;
power_control["MQTT_SWITCH_TOPIC"] = MQTT_SWITCH_COMMAND_TOPIC1;
power_control["MQTT_SWITCH_SYNC"] = MQTT_SWITCH_PUBLISH_TOPIC1;
power_control["supported"] = supported;
JSONVar capabilities;
capabilities[0] = power_control;
delay(200);
JSONVar json;
json["ID"] = ESPID;
json["DEVICE"] = DEVICE_TYPE;
json["MACADDRESS"] = String(WiFi.macAddress());
json["Public_Name"] = DEVICE_CHANNEL;
json["version"] = version_1;
json["payloadVersion"] = payloadVersion;
json["SW"] = SW;
json["HW"] = HW;
json["CHIPSIZE"] = (int)ESP.getFlashChipRealSize();
json["CHIPSPEED"] = (int)ESP.getFlashChipSpeed();
json["capabilities"] = capabilities;
delay(200);
String jsonString = JSON.stringify(json);
Serial.println(jsonString);

GridView - Editable ComboBox Data become slow

this is the code which i custom the column in gridview to show the data. i try to get the data from editable text that i type from comboBox, but the data output become too slow to appear in list
settings.Columns.Add(column =>
{
column.FieldName = "Food";
column.Caption = "Food List";
column.Width = 350;
column.ColumnType = MVCxGridViewColumnType.ComboBox;
var comboBoxProperties = column.PropertiesEdit as ComboBoxProperties;
comboBoxProperties.Width = new System.Web.UI.WebControls.Unit(350);
comboBoxProperties.DataSource = Food.Shared.ListFood;
comboBoxProperties.TextField = "Value";
comboBoxProperties.ValueField = "Key";
comboBoxProperties.ValueType = typeof(Guid);
comboBoxProperties.IncrementalFilteringMode = IncrementalFilteringMode.Contains;
comboBoxProperties.ItemStyle.Wrap = DefaultBoolean.True;
comboBoxProperties.ClientInstanceName = "foodListTo";
});

Set XML .#<index> based on interator AS3

In AS3 is it possible to export multiple iterations of the same variable as seen in the below example:
var item:String = "obj";
var child:XML = new XML(<{item}/>);
child.#x = String(object.x);
child.#y = String(object.y);
child.#n = String(object.name);
child.#w = String(object.width);
child.#h = String(object.height);
//...instead of:
child.#s = String(object.sprite);
//...is the below possible:
for (i = 0; i < <length>; ++i) {
child.#s[i] = String(object.get_sprite(i));
}
//...desired <filename>.xml output:
obj.s0 = "sprite_0"
obj.s1 = "sprite_1"
obj.s2 = "sprite_2"
obj.s3 = "sprite_3"
etc..
Haven't worked on AS3 for a while now, but if I remember correctly you should be able to do:
for (i = 0; i < <length>; ++i) {
var attrName = "s" + i.toString();
child.#[attrName] = String(object.get_sprite(i));
}
Sorry, I don't have tools available to try it out myself, but should work.

Skip the first line of a CSV file in Yii2

public function actionUpload(){
$model = new CsvForm();
if($model->load(Yii::$app->request->post())){
$file = UploadedFile::getInstance($model,'file');
$filename = 'Data.'.$file->extension;
$upload = $file->saveAs('uploads/'.$filename);
if($upload){
define('CSV_PATH','uploads/');
$csv_file = CSV_PATH . $filename;
$filecsv = file($csv_file);
print_r($filecsv);
foreach($filecsv as $data){
$modelnew = new Customer1();
$hasil = explode(",",$data);
$nim = $hasil[0];
$nama = $hasil[1];
$jurusan = $hasil[2];
$angkatan = $hasil[3];
$modelnew->username = $nim;
$modelnew->firstname = $nama;
$modelnew->lastname = $jurusan;
$modelnew->password = $angkatan;
$modelnew->save();
}
unlink('uploads/'.$filename);
return $this->redirect(['site/index']);
}
}else{
return $this->render('upload',['model'=>$model]);
}
}
This is my CSV data
user1,name,pass,info
user2,name,pass,info
etc..,
So I want to skip Bold content and proceed my execution.
You could shift the array
$filecsvtemp = file($csv_file);
$filecsv = array_shift($filecsvtemp );
in this way the first row of the array is not present in the resulting array
otherwise use a counter
$cnt = 0;
$filecsv = file($csv_file);
print_r($filecsv);
foreach($filecsv as $data){
if ($cnt!=0){
$modelnew = new Customer1();
$hasil = explode(",",$data);
$nim = $hasil[0];
$nama = $hasil[1];
$jurusan = $hasil[2];
$angkatan = $hasil[3];
$modelnew->username = $nim;
$modelnew->firstname = $nama;
$modelnew->lastname = $jurusan;
$modelnew->password = $angkatan;
$modelnew->save();
}
$cnt++;
}

How to create a projection widget programmatically in orchard cms?

I have created a query in my module with this code:
var myName = "something"
var theQuery = _contentManager.Create("Query");
theQuery.As<TitlePart>().Title = myName + "Query";
var filterGroupRecord = new FilterGroupRecord();
var filterRecord = new FilterRecord()
{
Category = "Content", Type = "ContentTypes",
Description = myName , Position = 1,
State = "<Form><Description>" + myName
+ "</Description> <ContentTypes>" + myName + "</ContentTypes></Form>"
};
filterGroupRecord.Filters.Insert(0, filterRecord);
theQuery.As<QueryPart>().FilterGroups.Clear();
theQuery.As<QueryPart>().FilterGroups.Insert(0, filterGroupRecord);
And I know to create a projection widget I should use below code :
var theProjectionWidget = _contentManager.Create("ProjectionWidget");
theProjectionWidget .As<WidgetPart>().Title = myName + "ProjectionWidget";
theProjectionWidget .As<WidgetPart>().RenderTitle = false;
theProjectionWidget .As<WidgetPart>().Zone = "Content";
theProjectionWidget .As<WidgetPart>().Position = "1";
theProjectionWidget .As<WidgetPart>().LayerPart.Name = myName;
But I don't know how to assign the above query to this new projection widget.
How to assign query id to ProjectionPart.QueryLayoutRecordId???!!
I would appreciate any help.
These are links to this discussion in codeproject & codeplex:
http://www.codeproject.com/Questions/876128/How-to-create-a-projection-widget-programmatically
https://orchard.codeplex.com/discussions/580735
After some wrestling with code and debug I found solution for my question.
Anybody with my problem can use this code:
theProjectionWidget.As<ProjectionPart>().Record.QueryPartRecord = new QueryPartRecord(){
ContentItemRecord = theQuery.As<QueryPart>().ContentItem.Record,
FilterGroups = theQuery.As<QueryPart>().FilterGroups,
Id = theQuery.As<QueryPart>().Id,
Layouts = theQuery.As<QueryPart>().Layouts,
SortCriteria = theQuery.As<QueryPart>().SortCriteria
};