I have the following object structure and for each record I need to display the attribute name and its value. for the following example, I need to display "Name " = xxx. The attribute name can be different for each json response. These are field names of a table so I cann't use hard coded names.
How do I read the attribute value?
I tried var propname = DataInObj.DataSet[0].Record[0].properties[1] but it didn't work. Pls help
object
REcord
+attributes
-0
Name xxx
Amount 100
+attributes
-1
Name yyy
Amount 200
See this other post: Iterate over an object in Google Apps script
Code goes like this:
var dict = { "foo": "a", "bar": "b" };
function showProperties(){
var keys = [];
for(var k in dict) keys.push(k+':'+dict[k]);
Logger.log("total " + keys.length + "\n" + keys.join('\n'));
}
Related
I converted this object into JSON string.
values of subject1, subject2, subject 3 are coming from input fields in angular frontend
subject = {
"subject1": "A",
"subject2": "B",
"subject3": "C"
}
and now I have a JSON string like this,
const subjects = {"subject1": "A", "subject2": "B","subject3":"c"}
Like this, I saved this as a JSON string in DB. In another place, I wanted to access this string as an object as previously I made.
to output these subjects separately as same as It gets as an object.
example:- First input box - subject1 value as A , Second input box - subject2 value as B
How can I put them back again like separate values into separate variables or any other way to separate them and put back into the text fields?
I just tried to get that JSON string and tried to access subject1 like
subject1 = subjects.subject1 like that I can put subject1 in relevant text field.
But that doesn't work. I checked previous questions like this. But they didn't answer my question. How can I solve this?
You can do it with JSON.parse.
const obj = JSON.parse(subjects);
console.log(obj.subject1); // "A"
You can use destructuring assignment:
The destructuring assignment syntax is a JavaScript expression that makes it possible to unpack values from arrays, or properties from objects, into distinct variables.
const subject = {
"subject1": "A",
"subject2": "B",
"subject3": "C"
}
const {subject1,subject2,subject3} = subject;
console.log(subject1);
console.log(subject2);
console.log(subject3);
You can find more about destructuring assignment here
I am new to QT, QML and JSON. I am trying to write a simple program that saves data into a JSON file. The programs has 3 files:
main.qml has 2 text input fields and 1 button that calls a function in script.js
storage.json {"sets":[{object Object},{object Object}]}
script.js holds this function:
function insertNewSet (name, description){
var newSet = {
setName: "name",
setDescription: "description",
setInput: []
}
var newJson = JSON.stringify(newSet);
}
name and description come from the input fields in main.qml
How can the variable newSet be inserted as a third object in newSet?
example: {"sets":[{object Object},{object Object},{object Object}]}
You need to use ' ' symbols to specify the properties.
These can be called up this way:
var newSet = {
'setName': 'name',
'setDescription': 'description',
'setInput': []
}
var newJson = JSON.stringify(newSet['setName']+newSet['setDescription']+newSet['setInput']);
If you didn't think about it, maybe this page will help you:
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array
var email = req.body.emailId;
connection.query('SELECT name FROM fun WHERE email = "' + email +'" ',function(err, result,fields) {
console.log(result);
});
The console displays "result" in the below format:
[{name:'sunny'}]
Can someone tell me how to display"sunny"?
Sunny should be accessible with the following code
alert(result[0].name); //sunny!
As you can see, the JS object displayed is an array with one element, which is an object with one key (name).
So you simply index the array to the first value result[0] and then fetch that element's name value result[0].name.
I've a function that gets 2 values name and value and now I'd like to turn them into a JSON, so for example lets say ive this function
function addin(name,val)
and then i'll call it
addin("age","28") -> this will return -> {age: 28}
addin("name","Roni") -> {name: Roni}
I've tried many things to find out how to make it done, Im getting many data so I tried this
var full_js = { };
_.forEach(data, function(val,name) {
full_js.name = val;
console.log(JSON.stringify(full_js));
});
again it can be any value and any name both of them are Random Strings.
but its not working, I get it as {"name": "Roni"} and {"name": "55"}.
thanks for the help.
use a different notation:
function addin(name, val) {
var full_js = {};
full_js[name] = val;
return JSON.stringify(full_js);
}
Ex:
at the moment you are always assigning the value to the name key, instead of the dynamic name key, keep in mind that the key will always be the string representation of the variable used.
for example, addin({}, 'rony'), will return {"[object Object]":"test"}
I'm trying to process the following with an JSON Input step:
{"address":[
{"AddressId":"1_1","Street":"A Street"},
{"AddressId":"1_101","Street":"Another Street"},
{"AddressId":"1_102","Street":"One more street", "Locality":"Buenos Aires"},
{"AddressId":"1_102","Locality":"New York"}
]}
However this seems not to be possible:
Json Input.0 - ERROR (version 4.2.1-stable, build 15952 from 2011-10-25 15.27.10 by buildguy) :
The data structure is not the same inside the resource!
We found 1 values for json path [$..Locality], which is different that the number retourned for path [$..Street] (3509 values).
We MUST have the same number of values for all paths.
The step provides Ignore Missing Path flag but it only works if all the rows misses the same path. In that case that step acts as as expected an fills the missing values with null.
This limits the power of this step to read uneven data, which was really one of my priorities.
My step Fields are defined as follows:
Am I missing something? Is this the correct behavior?
What I have done is use JSON Input using $.address[*] to read to a jsonRow field the full map of each element p.e:
{"address":[
{"AddressId":"1_1","Street":"A Street"},
{"AddressId":"1_101","Street":"Another Street"},
{"AddressId":"1_102","Street":"One more street", "Locality":"Buenos Aires"},
{"AddressId":"1_102","Locality":"New York"}
]}
This results in 4 jsonRows one for each element, p.e. jsonRow = {"AddressId":"1_101","Street":"Another Street"}. Then using a Javascript step I map my values using this:
var AddressId = getFromMap('AddressId', jsonRow);
var Street = getFromMap('Street', jsonRow);
var Locality = getFromMap('Locality', jsonRow);
In a second script tab I inserted minified JSON parse code from https://github.com/douglascrockford/JSON-js and the getFromMap function:
function getFromMap(key,jsonRow){
try{
var map = JSON.parse(jsonRow);
}
catch(e){
var message = "Unparsable JSON: "+jsonRow+" Desc: "+e.message;
var nr_errors = 1;
var field = "jsonRow";
var errcode = "JSON_PARSE";
_step_.putError(getInputRowMeta(), row, nr_errors, message, field, errcode);
trans_Status = SKIP_TRANSFORMATION;
return null;
}
if(map[key] == undefined){
return null;
}
trans_Status = CONTINUE_TRANSFORMATION;
return map[key]
}
You can solve this by changing the JSONPath and splitting up the steps in two JSON input steps. The following website explains a lot about JSONPath: http://goessner.net/articles/JsonPath/
$..AddressId
Does in fact return all the AddressId's in the address array, BUT since Pentaho is using grid rows for input and output [4 rows x 3 columns], it can't handle a missing value aka null value when you want as results return all the Streets (3 rows) and return all the Locality (2 rows), simply because there are no null values in the array itself as in you can't drive out of your garage with 3 wheels on your car instead of the usual 4.
I guess your script returns null (where X is zero) values like:
A S X
A S X
A S L
A X L
The scripting step can be avoided same by changing the Fields path of the first JSONinput step into:
$.address[*]
This is to retrieve all the 4 address lines. Create a next JSONinput step based on the new source field which contains the address line(s) to retrieve the address details per line:
$.AddressId
$.Street
$.Locality
This yields the null values on the four address lines when a address details is not available in an address line.