I am trying to pull the IncPubKey JSON data from "0", but I'm getting a SyntaxError. In postman, part of the returned data looks like this:
"ShardCommittee": {
"0": [{
"IncPubKey": "12RRMGAEXdtD796sbmyHwNv9oiPPfGYa1r6KssMqXyiUMfT6r86",
"MiningPubKey": {
"bls": "184UyUYNPg1ESgEXzUZY6dWjxohyZSvE4f5XnkssHhonRNopAp2bxqxCAToYGUsgeyrWeKQJmnLkgeCm95XYK5mjD2KjFPKkrrwLyiXscUAJ9mVubTCft4Z1ra3S9Ca4ojx1P5ajMNaS2Yc9C1rh8qr2S7VE6KCk7YMnBpdkyWdiTrmGtUUjx",
"dsa": "16Be2DoUxNMzxidLKBqDC6gMkqMZ9bSijBvpRirvioZiqmVGDkg"
}
},
{
"IncPubKey": "1VVQRGZFhMyp9LRS1yjyXsW6ugMqtHaYyUYMLutRhy6ASF7AWf",
"MiningPubKey": {
"bls": "1ELnsFdz5M4RDvShVvcAKE4bbv6dZED31MeSKgRKhDiYgTiJeWT94zvw2VHeytVxEDExMeemPqXL7DA1CnNBGbjTMarv1vXd1A6oi5JctfM4u8Totjpqr8VNFtQbgHqJ1wSYdFz5hgwEq8QkmDdf71WPPdYEkpG5ow3DYeAAwFkArQZvn1C4F",
"dsa": "18BTfn7mbDBXgysaHZsrGqbN3T2K3gEChsEXXuzkwKEo96eU3Nf"
}
},
But in google, it doesn't seem to like the number "0."
for (let i = 0; i < d.Result.ShardCommittee.0.length; i++) {
sh1.getRange(i + 2, 3).setValue(d.Result.ShardCommittee.0[i].IncPubKey);
}
When I step back and pull all data into one cell:
sh1.getRange(2, 3).setValue(d.Result.ShardCommittee);
This is the returned result:
{ 0=[Ljava.lang.Object;#41d079f7, 1=[Ljava.lang.Object;#7197d9a6, 2=[Ljava.lang.Object;#23a1916a, 3=[Ljava.lang.Object;#657f1624, 4=[Ljava.lang.Object;#c3808bd, 5=[Ljava.lang.Object;#186f5f86, 6=[Ljava.lang.Object;#60f6425c, 7=[Ljava.lang.Object;#6cc43c2c}
Any idea how to re-write that to pull the IncPubKey data?
You can't use a number as a key in "dot" notation, instead use array style notation:
Result.ShardCommittee["0"].length;
and
d.Result.ShardCommittee.["0"][i].IncPubKey
This is a javascript syntax issue, rather than a Google Apps Script specific issue.
in your last example, where you write the entire object to a cell, you are seeing the results of converting the structure to a string. If you want to write raw objects to a cell in the spreadsheet for inspection, you should convert them to JSON first.
sh1.getRange(2, 3).setValue(JSON.stringify(d.Result.ShardCommittee));
Related
I have a JSON body with template to pass to an API endpoint in Postman/Newman as follows:
{
"total": [
{
"var1a": "ABCDE",
"var1b": {
"var2a": "http://url/site/{{yyyy}}/{{mmdd}}/{{yyyymmdd}}.zip",
"var2b": {
"var3a": "{{yyyymmdd}}",
"var3b": "GKG"
},
"var2c": "ABCDE"
"var2d": "{{p}}"
}
},
{
"var1a": "ABCDE",
"var1b": {
"var2a": "http://url/site/{{yyyy}}/{{mmdd}}/{{yyyymmdd}}.zip",
"var2b": {
"var3a": "{{yyyymmdd}}",
"var3b": "GKG"
},
"var2c": "ABCDE"
"var2d": "{{p}}"
}
}
}
Everything enclosed in double braces is a variable that needs to read from an external CSV file, which looks like:
p,yyyymmdd,yyyy,mmdd
1,19991231,1999,1231
2,20001230,2000,1230
Note that the CSV isn't necessarily 2 entries long - I'd ideally like it to take in a dynamic number of entries that append after the last.
Ideally, I'd get rid of the last two columns in the CSV file (yyyy and mmdd) and have the code just do a slice as needed.
How do I even begin coding the pre-request script?
Even if reading from an external CSV is not possible, or even coding within the pre-request script to do this, what is a quick one-liner in Javascript or Python that can quickly give me the output I need so that I can just manually place it in the request body?
I even tried using the Postman runner to do a "fake" run so that I can quickly extract the different JSON bodies, but even with this, there is no clean way that it does the export...
Reading data from CSV is not the good way to solve problem because each record is will be used to one request. You want the whole record into ONE request.
I think write code in pre-request script will work.
In body tab:
In pre-rquest tab:
let data_set = [
[1, 1999, 1231],
[2, 2000, 1230]
];
let total = [];
for (let i = 0; i < data_set.length; i++) {
let p = data_set[i][0];
let yyyy = data_set[i][1];
let mmdd = data_set[i][2];
const item = {
var1a: "ABCDE",
var1b: {
var2a: `http://url/site/${yyyy}/${mmdd}/${yyyy}${mmdd}.zip`,
var2b: {
var3a: `${yyyy}${mmdd}`,
var3b: "GKG"
},
var2c: "ABCDE",
var2d: `${p}`
}
};
total.push(item);
}
pm.environment.set("total", JSON.stringify(total));
I'm trying to load some JSON data from a JSON file and carrying out some data manipulation on it for an app I'm trying to build. However, when looping through the data, I'm getting an undefined error which makes it seem that property is missing from the JSON object when I use a looping variable to access the objects. However, when I index the JSON array with a hardcoded number, the property loads fine. I am wondering if someone can help me out with this. I've attached an example of the code and the JSON to this.
I have tried stringifying the JSON and parsing it again and tried both accessing the JSON using square brackets as well as the full stop and they all lead to the same result.
Code to access:
import ontology from '../../data/ontology.json'
const totalAnswerList = ontology.answers
for (var i = 0; i <= totalAnswerList.length; i++) {
var wordID = totalAnswerList[i] // wordID.id returns undefined
var wordID2 = totalAnswerList[0] // wordID2.id works
alert(JSON.stringify(wordID) + JSON.stringify(wordID2) + '\nWord ID hardcoded: ' + wordID2.id)
}
//ontology.json
{
"answers": [
{
"id": "examination",
"category_id": "examination",
"synonyms": ["examination"]
}, ...
], ...
}
The code you provided works as expected, but the issue is the last element is undefined because of your for loop constraints. You likely want i < totalAnswerList.length and not <=. Because if the array is 5 elements long, you want to loop through 0,1,2,3,4 (and not 5, which will be undefined).
import ontology from "./ontology.json";
const totalAnswerList = ontology.answers;
for (var i = 0; i < totalAnswerList.length; i++) {
// ...
}
I'm trying to return the values of JSON objects which are nested within a complex array of objects. Unfortunately I've not been able to do it easily as the structure of the JSON seems unusual; for example:
var cartContents = {
"contents":
{
"ed2be4abf6cc927dd670b567efd42fc3":
{
"id": "1288785070733722605",
"quantity":2,
"limit":10,
"offset":0,
"order":null,
"created_at":"2016-07-06 12:18:10",
"updated_at":"2016-07-06 12:18:34",
"sku":"006",
"title":"Fishing Rod",
"slug":"fishing-rod-lara-fluorescent-print",
"sale_price":0,
"status":{
"value":"Draft",
"data": {
"key":"0",
"value":"Draft"
}
},
"category":
{
"value":"Bottom",
"data":
{
"1228355758422329063":
{
"id":"1238352758122309063",
"order":null,
"created_at":"2016-07-06 11:23:54",
"updated_at":"2016-07-06 11:38:23",
"parent":
{
"value":"All Sports",
"data":
{
"id":"2288364532150634121",
"order":null,
"created_at":"2016-07-06 11:37:25"...
}
}
}
}
}
}
}
}
The following code produces the correct result ("Fishing Rod"):
var x = cartContents.contents;
console.log(x.ed2be4abf6cc927dd670b567efd42fc3.title);
However the whole point is that the 32 character string ('ed2be4abf6cc927dd670b567efd42fc3') represents one of many products, each with a random 32 bit string and therefore they can't be individually coded into the JS.
I've tried a very great deal of different ways. The latest is the following code (where 'carts' is an array of unique 32 character strings:
var x = cartContents.contents;
$.each(carts, function() {
console.log(x.this.title);
});
The result is "jQuery.Deferred exception: Cannot read property 'title' of undefined TypeError: Cannot read property 'title' of undefined".
Would greatly appreciate help accessing the following keys and their values:
"title":"Fishing Rod"
"id":"1238352758122309063"
"created_at":"2016-07-06 11:37:25"
Also worth mentioning that I can't touch the server, therefore I can't change the data structure at all.
Many thanks in advance.
Given a JSON string as this:
{
"__ENTITIES": [
{
"__KEY": "196",
"__STAMP": 1,
"ID": 196,
"firstName": "a",
"middleName": "b",
"lastName": "c",
"ContactType": {},
"addressCollection": {
"__deferred": {
"uri": "/rest/Contact(196)/addressCollection?$expand=addressCollection"
}
},
"__ERROR": [
{
"message": "Cannot save related entity of attribute \"ContactType\" for the entity of datastore class \"Contact\"",
"componentSignature": "dbmg",
"errCode": 1537
}
]
}
]
}
Is there a method to get just the __ERROR record, I know I can use
var mydata = json.parse(mydata) and then find it from the mydata object. But I was hoping there was a method to only return the ERROR field something like
json.parse(mydata, "__ERROR") and that gets only the information in the __ERROR field without turning the whole JSON string into an object
"Is there a method to get just the __ERROR record, I know I can use var mydata = json.parse(mydata) ... But I was hoping there was ... something like json.parse(mydata, "__ERROR")"
There may be libraries that do this, but nothing built in. You need to write code that targets the data you want.
The closest you'll get will be to pass a reviver function to JSON.parse.
var errors = [];
var mydata = JSON.parse(mydata, function(key, val) {
if (key === "__ERROR")
errors.push(val);
return val
});
without turning the whole json string into an object
That's hardly possible, you would need some kind of lazy evaluation for that which is not suitable with JS. Also, you would need to write your own parser for that which would be reasonable slower than native JSON.parse.
Is there a method to get just the __ERROR record
Not that I know. Also, this is an unusual task to walk the whole object tree looking for the first property with that name. Better access __ENTITIES[0].__ERROR[0] explicitly.
If such a function existed, it would have to parse the whole thing anyway, to find the key you're looking for.
Just parse it first, then get the key you want:
var mydata = JSON.parse(mydata);
var errorObj = mydata.__ENTITIES[0].__ERROR[0];
If you want, you may create your own function:
function parseAndExtract(json, key) {
var parsed = JSON.parse(json);
return parsed[key];
}
I have the response in the following format after doing groupby in solr query. I am using solr version 3.5
"grouped":{
"channel_id":{
"matches":48,
"ngroups":26,
"groups":[{
"groupValue":"204",
"doclist":{"numFound":1,"start":0,"docs":[
{
"channel_name":"ZeeTv",
"channel_num":4,
"title":"The Name",
"channel_id":"204"
}},
{
"groupValue":"166",
"doclist":{"numFound":2,"start":0,"docs":[
{
"channel_name":"Sony",
"channel_num":2,
"title":"The Name",
"channel_id":"166",
{
"channel_name":"Sony",
"channel_num":2,
"title":"The Puzzle",
"channel_id":"166"
}}]}}
I am taking the response in an array in the following way :
for(var chl in data.grouped.channel_id.groups) {
config['playlist'].push(data.grouped.channel_id.groups[chl]['doclist']['docs']);
}
Thus an individual array of each groupValue is formed. The struture of the array is:
"0"=>{"0"=>"value"},"1"=>{"0"=>"result1","1"=>"result2"}
But i want to change the key name i.e. "0","1" to the groupValue from the response while creating an array so that i can do config['playlist']['166'] to check all the shows for this channel_id from the array. Can this be done and if so how. I am expecting the following :
"204"=>{"0"=>"value"},"166"=>{"0"=>"result1","1"=>"result2"}
Also if possible can the solr query be made such that the channel_num in the response comes in ascending order i.e. first result for channel_num 2 and then 4. I have done groupby:channel_id
What about that?
for(var chl in data.grouped.channel_id.groups) {
config['playlist'][data.grouped.channel_id.groups[chl].groupValue] = data.grouped.channel_id.groups[chl]['doclist']['docs'];
}
Push is there to add an element at the end of an array. But any Javascript object is just a hash table, so you can use it that way.
By the way, you can make the code simpler with a for each :
for each(var chl in data.grouped.channel_id.groups) {
config['playlist'][ch1.groupValue] = ch1['doclist']['docs'];
}
Pikrass has answered correctly... as far as ordering with channel_num is concerned try adding the following in your query:
&sort=channel_num asc