I have an array of JSON:
[{"AvatarURL":"https://avatars1.githubusercontent.com/u/7849225?v=4","Name":"simplenlg","Score":22.82041,"Updatedat":"2018-07-21T10:58:33Z"},{"AvatarURL":"https://avatars1.githubusercontent.com/u/8931462?v=4","Name":"aws-microservices-deploy-options","Score":20.521696,"Updatedat":"2018-07-20T12:22:07Z"},{"AvatarURL":"https://avatars3.githubusercontent.com/u/4046529?v=4","Name":"useful-jenkins-groovy-init-scripts","Score":21.447626,"Updatedat":"2018-07-18T19:52:02Z"},{"AvatarURL":"https://avatars1.githubusercontent.com/u/6755615?v=4","Name":"dnsjava","Score":34.74705,"Updatedat":"2018-06-28T15:16:45Z"},{"AvatarURL":"https://avatars3.githubusercontent.com/u/504773?v=4","Name":"luke","Score":19.239859,"Updatedat":"2018-06-28T07:27:26Z"},{"AvatarURL":"https://avatars3.githubusercontent.com/u/536912?v=4","Name":"Wicket-tutorial-examples","Score":37.265644,"Updatedat":"2018-07-14T04:28:50Z"},{"AvatarURL":"https://avatars0.githubusercontent.com/u/33330803?v=4","Name":"nexus-repository-apt","Score":44.401646,"Updatedat":"2018-07-06T18:28:13Z"},{"AvatarURL":"https://avatars2.githubusercontent.com/u/1321963?v=4","Name":"marc4j","Score":28.282797,"Updatedat":"2018-07-07T15:58:57Z"},{"AvatarURL":"https://avatars0.githubusercontent.com/u/5622390?v=4","Name":"jamonapi","Score":24.564436,"Updatedat":"2018-07-16T07:44:35Z"},{"AvatarURL":"https://avatars2.githubusercontent.com/u/1123352?v=4","Name":"osgi.enroute","Score":7.6444883,"Updatedat":"2018-07-17T08:26:51Z"}]
I want it to be:
{
gitdList: [
{"AvatarURL":"https://avatars1.githubusercontent.com/u/7849225? v=4","Name":"simplenlg","Score":22.82041,"Updatedat":"2018-07-21T10:58:33Z"},
{"AvatarURL":"https://avatars2.githubusercontent.com/u/1123352?v=4","Name":"osgi.enroute","Score":7.6444883,"Updatedat":"2018-07-17T08:26:51Z"}
]
}
I searched and am finding solutions regarding php only , to use json.encode.
How do I do it using JQUERY?
Have you try like this way? just create an empty object like this object = {} and assign your existing value i.e avatar_urls to it with your desired key i.e gitdList. Hope this will work for you.
var avatar_urls = [{"AvatarURL":"https://avatars1.githubusercontent.com/u/7849225?v=4","Name":"simplenlg","Score":22.82041,"Updatedat":"2018-07-21T10:58:33Z"},{"AvatarURL":"https://avatars1.githubusercontent.com/u/8931462?v=4","Name":"aws-microservices-deploy-options","Score":20.521696,"Updatedat":"2018-07-20T12:22:07Z"},{"AvatarURL":"https://avatars3.githubusercontent.com/u/4046529?v=4","Name":"useful-jenkins-groovy-init-scripts","Score":21.447626,"Updatedat":"2018-07-18T19:52:02Z"},{"AvatarURL":"https://avatars1.githubusercontent.com/u/6755615?v=4","Name":"dnsjava","Score":34.74705,"Updatedat":"2018-06-28T15:16:45Z"},{"AvatarURL":"https://avatars3.githubusercontent.com/u/504773?v=4","Name":"luke","Score":19.239859,"Updatedat":"2018-06-28T07:27:26Z"},{"AvatarURL":"https://avatars3.githubusercontent.com/u/536912?v=4","Name":"Wicket-tutorial-examples","Score":37.265644,"Updatedat":"2018-07-14T04:28:50Z"},{"AvatarURL":"https://avatars0.githubusercontent.com/u/33330803?v=4","Name":"nexus-repository-apt","Score":44.401646,"Updatedat":"2018-07-06T18:28:13Z"},{"AvatarURL":"https://avatars2.githubusercontent.com/u/1321963?v=4","Name":"marc4j","Score":28.282797,"Updatedat":"2018-07-07T15:58:57Z"},{"AvatarURL":"https://avatars0.githubusercontent.com/u/5622390?v=4","Name":"jamonapi","Score":24.564436,"Updatedat":"2018-07-16T07:44:35Z"},{"AvatarURL":"https://avatars2.githubusercontent.com/u/1123352?v=4","Name":"osgi.enroute","Score":7.6444883,"Updatedat":"2018-07-17T08:26:51Z"}]
var object = {};
object.gitdList = avatar_urls;
console.log(object);
Edit: pretty neat and better way to do it.
var object = { gitdList: avatar_urls};
console.log(object);
maybe like this:
var data=[{"AvatarURL":"https://avatars1.githubusercontent.com/u/7849225?v=4","Name":"simplenlg","Score":22.82041,"Updatedat":"2018-07-21T10:58:33Z"},{"AvatarURL":"https://avatars1.githubusercontent.com/u/8931462?v=4","Name":"aws-microservices-deploy-options","Score":20.521696,"Updatedat":"2018-07-20T12:22:07Z"},{"AvatarURL":"https://avatars3.githubusercontent.com/u/4046529?v=4","Name":"useful-jenkins-groovy-init-scripts","Score":21.447626,"Updatedat":"2018-07-18T19:52:02Z"},{"AvatarURL":"https://avatars1.githubusercontent.com/u/6755615?v=4","Name":"dnsjava","Score":34.74705,"Updatedat":"2018-06-28T15:16:45Z"},{"AvatarURL":"https://avatars3.githubusercontent.com/u/504773?v=4","Name":"luke","Score":19.239859,"Updatedat":"2018-06-28T07:27:26Z"},{"AvatarURL":"https://avatars3.githubusercontent.com/u/536912?v=4","Name":"Wicket-tutorial-examples","Score":37.265644,"Updatedat":"2018-07-14T04:28:50Z"},{"AvatarURL":"https://avatars0.githubusercontent.com/u/33330803?v=4","Name":"nexus-repository-apt","Score":44.401646,"Updatedat":"2018-07-06T18:28:13Z"},{"AvatarURL":"https://avatars2.githubusercontent.com/u/1321963?v=4","Name":"marc4j","Score":28.282797,"Updatedat":"2018-07-07T15:58:57Z"},{"AvatarURL":"https://avatars0.githubusercontent.com/u/5622390?v=4","Name":"jamonapi","Score":24.564436,"Updatedat":"2018-07-16T07:44:35Z"},{"AvatarURL":"https://avatars2.githubusercontent.com/u/1123352?v=4","Name":"osgi.enroute","Score":7.6444883,"Updatedat":"2018-07-17T08:26:51Z"}];
var new_data={ 'gitdList': data };
console.log(new_data);
I have a query filter in my code, I want to pass these two queries as one like this:
case1
con = {'eq':['case1':dosome]}
case2
con = {'eq':['case1':dosome]}
I need to bind these two at the end like this
{'eq':['case1':dosome],'eq':['case2':dosome]}
the key eq should remain same.
Why not use an object with two properties like
{ eq: { case1: dosome, case2: dosome } }
Sounds like you want an array of objects:
var your_array = new Array();
var con1 = {'eq':{'case1':dosome}}
your_array.push(con1)
var con2 = {'eq':{'case2':dosome}}
your_array.push(con2)
You can then use the methods for accessing parts of the array -> http://www.w3schools.com/js/js_array_methods.asp
Use merge() function. Also this will give error: ['case1':dosome]. Try this
<script>
var obj1 = {'eq':{'case1':dosome}};
var obj2 = {'eq':{'case2':dosome}}
var merge = obj1.merge(obj2);
</script>
Hope this helps
You can try this:
{ eq: [
{ condition: 'case1', action: doSome } ,
{ condition: 'case2', action : doSomeOther }
]}
I've node application and Inside a folder I've txt file(long...) with content like following
BASH=/bin/sh
BASH_ARGC=()
BASH_ARGV=()
BASH_LINENO=([0]="0")
BASH_VERSINFO=([0]="3" [1]="2" [2]="51" [3]="1" [4]="release" )
BASH_VERSION='3.2.2(1)-release'
CF_INSTANCE_ADDR=10.2.7:501
CF_INSTANCE_INDEX=0
CF_INSTANCE_IP=10.97.27.7
CF_INSTANCE_P='[{external:500,internal:501}]'
COLUMNS=80
I read the txt file content with fs.readFile and I need to update some property there so I think to parse it to json but this is not working
my questions is:
Should I parse it to json? in order to modify some property value
such like
from
CF_INSTANCE_ADDR=10.2.7:501
to
CF_INSTANCE_ADDR=11.3.8:702
Or
CF_INSTANCE_P='[{external:500,internal:501}]'
to
CF_INSTANCE_P='[{external:100,internal:200}]'
Etc...
There is other better way?
This is what I tried
fs.readFile(filePath, 'utf8').then(function (response) {
var aa = JSON.stringify(response);
//console.log(aa);
var bb = JSON.parse(aa);
console.log(bb);
return response;
}
You can convert the string to an object at which point you can decide how to proceed/tidy up the data:
var obj = {};
str.split(/\n/g).forEach(function (el) {
var spl = el.split('=');
obj[spl[0]] = spl[1];
});
DEMO
So you should be left with an object called obj:
var obj = {
"BASH": "/bin/sh",
"BASH_ARGC": "()",
"BASH_ARGV": "()",
"BASH_LINENO": "([0]",
"BASH_VERSINFO": "([0]",
"BASH_VERSION": "'3.2.2(1)-release'",
"CF_INSTANCE_ADDR": "10.2.7:501",
"CF_INSTANCE_INDEX": "0",
"CF_INSTANCE_IP": "10.97.27.7",
"CF_INSTANCE_P": "'[{external:500,internal:501}]'",
"COLUMNS": "80"
}
You can now access the values of each key using either dot or bracket notation:
obj.BASH_VERSION // '3.2.2(1)-release'
obj['BASH_VERSION'] // '3.2.2(1)-release'
Here I'll remove those single quotes from BASH_VERSION:
obj.BASH_VERSION = obj.BASH_VERSION.replace("'", "");
JSON
This is my JSON structure. I am pushing data into this on button click.
var arrtopic = {
"info" : []
};
JS
I wrote this is function
arrtopic.info.push({
"Name" :name,
"Number" : num
});
JS for json data display
function displayJson(){
var obj = eval ("(" + arrtopic + ")");
document.getElementById("newme").innerHTML=obj.info.Name;
}
This is not working, please suggest something
Thank you in advance for your help
Try this
function displayJson(){
document.getElementById("newme").innerHTML= arrtopic.info[0].Name;
}
You are doing correct just use
console.log(arrtopic.info[0].Name);
instead of arrtopic.info.Name
function displayJson(){
var obj =JSON.parse(arrtopic);
document.getElementById("newme").innerHTML=obj.info.Name;
}
You can also use alert(obj.count); to fine whether the items are being pushed into
I need to use a variable when selecting data from a json source like this.
The json is retrieved with jquery getJSON().
"prices":[{
"fanta":10,
"sprite":20,
}]
var beverage = fanta;
var beverage_price = data.prices.beverage;
Now beverage_price = 10
var beverage = sprite;
var beverage_price = data.prices.beverage;
Now beverage_price = 20
When I try to do it like in the examples, the script tries to look up the beverage entry in prices.
Thanks a lot!!
You can access it like:
var beverage = 'fanta';
var beverage_price = data.prices[0][beverage];
As VisioN mentioned in the comment, data.prices is an array, you need to access its first element with [0] which contains prices { "fanta":10, "sprite":20}
here is the working example : http://jsfiddle.net/2E8AH/
Or else you can make data.prices an object like below : (if it is in your control)
var data = {
"prices" :
{
"fanta":10,
"sprite":20,
}
};
and can access without [0] like this : http://jsfiddle.net/Y8KtT/1/