dynamically create variable from json object - json

var a={LogGroupName:'/sam/xxx/ab/xxx/ayb/lob-star',LogStreamName:'tsy98',S3_Bucket:'abc'};
var array=[a];
console.log(array);
[ { LogGroupName: '/sam/xxx/ab/xxx/ayb/lob-star',LogStreamName: 'tsy98',S3_Bucket: 'abc' } ]
Would require to dynamically find the length of the array and variablize the key value from the json object.
Output needed:
LogGroupName=/sam/xxx/ab/xxx/ayb/lob-star
console.log(LogGroupName);
Need help.

Related

iterate json object data using map along with key

I have trying to get collection of data from json object using map and assign key, pair reference in loop but its not working out for me. I can read value in iterations but I need to assign to key and value where I need help. its not recognising "key" and "value"
var j1 = _preDefineAnswerOptions.map(function(item){
return ["key": item.preDefineAnswerId, "value": item.text];
});
data-source structure
You need to return an object. Use {} around the key and value.
var j1 = _preDefineAnswerOptions.map(function(item){
return {"key": item.preDefineAnswerId, "value": item.text};
});

Get json data from var

I gets following data in to a variable fields
{ data: [ '{"myObj":"asdfg"}' ] }
How to get the value of myObj to another variable? I tried fields.myObj.
I am trying to upload file to server using MEANjs and node multiparty
Look at your data.
fields only has one property: data. So fields.myObj isn't going to work.
So, let's start with fields.data.
The value of that is an array. You can see the []. It has only one member, so:
fields.data[0]
This is a string. You seem to want to treat it as an object. It happens to conform to the JSON syntax, so you can parse it:
JSON.parse(fields.data[0])
This parses into an object, so now you can access the myObj property.
JSON.parse(fields.data[0]).myObj
var fields = { data: [ '{"myObj":"asdfg"}' ] };
alert(JSON.parse(fields.data[0]).myObj);

Get value of json string

[
{
"type": "spline",
"name": "W dor\u0119czeniu",
"color": "rgba(128,179,236,1)",
"mystring": 599,
"data": ...
}
]
I am trying to access this json as json['W doręczeniu']['mysting'], and I get no value why is that?
You're trying to access the index "W doręczeniu" but that's not an index it's a value. Also, what you seem to have is an array of JSON objects.
The [ at the start marks the array, the first element of which is your JSON object. The JSON obj begins with the {
You're also trying to use a [ ], but JSON values are accessed with the dot operator.
I'm not sure which index you're actually trying to access, but try something like this:
var x = json[0].mystring;
The value of "W doręczeniu" is not a key, so you cannot use it to get a value. Since your json string is an array you'll have to do json[0].nameto access the first (and only) element in the array, which happens to be the object. Of course, this is assuming json is the variable you store the array into.
var json = [{"type":"spline","name":"W dor\u0119czeniu","color":"rgba(128,179,236,1)","mystring":599}];
console.log(json[0].mystring); //should give you what you want.
EDIT:
To get the last element in a js array, you can simply do this:
console.log( json[json.length -1].mystring ); // same output as the previous example
'length - 1' because js arrays are indexed at 0. There's probably a million and one ways to dynamically get the array element you want, which are out of the scope of this question.

random selection from JSON

I try to convert some JSON string data from JSON object into an array.
When I loop over the JSON is it assign the JSON strings into diffrent array cells, eventually I get all the strings from the JSON but in diffrent order in the array each time I run the program.
for (var i:String in data)
{
// get panel tabs and players for each tab
for (var f:String in data[i].tabs)
{
tabsNames.push(f);
}
}
sometimes tabsNames = [ 1,2,3]
sometimes tabsNames = [ 2,3,1] etc'
I cant use sort because I cant know the type of the information that I will get from the JSON.
A JSON object is an unordered set of name/value pairs:
"obj" : {"propA" : "valueA", "propB":"valueB"}
A JSON array is an ordered collection of values:
"arr" : ["propA":"valueA", "propB":"valueB"]
If your data will be stored in JSON Object as list, you always get data in same order.

Flash: Parsing a JSON file that has a node called Class

Essentially I'm trying to parse JSON and assigning the results to variables, one of which is "var = JSON.class;" with class being what's returned in the JSON. However, flash won't let me parse it because it's called class which it uses to create new classes. Is there any workaround or will I not be able to grab this node?
I'm not sure if this is what you're asking, but if you have a property in your JSON object named "class", you should be able to parse it like as shown below.
// assuming JSON object looks like this, and is stored in a var named 'jsonData'
var jsonData:Object = { "id": 0, "class": "MyClassName", "values": [1,2,3] }
// trying to parse like this won't work w/keywords like 'class':
var parsedValue : String = jsonData.class;
// parse it with this way, using the square brackets:
var parsedValue : String = jsonData["class"];