Hi I have a JSON format of the following -
{
"0":{
"name":"example",
"age":"21"
},
"1":{
"name":"example2",
"age":"22"
}
}
I want to convert it to the following format with jQuery -
{
"name":"example",
"age":"21"
},
{
"name":"example2",
"age":"22"
}
removing numbering from keys. Please suggest.
You really do not need JQuery for this. You can use Object.values() function to get an array of values for each property in the object:
const input = {
"0":{
"name":"example",
"age":"21"
},
"1":{
"name":"example2",
"age":"22"
}
}
const result = Object.values(input)
console.log(result)
I assume your desired result is an array.
var json = JSON.parse("your json");
var keys = Object.getOwnPropertyNames(b);
for(var i=o;i<keys.length;i++){
console.log(b[keys[i]]);
}
Here you go "numbering" removed is over btn Storing parsed objects should be stored in collections, and keys are required to exist in objects. please refer to
Related
Hi I would like to check if there's a way to convert one of my JSON property's value from string to an array of string.
What I have:
const testJSON = {
"name": "Albert"
}
What I want:
const testJSON = {
"name": ["Albert"]
}
Many thanks for any help and direction I could explore!
You can give a try to this dynamic solution with the help of Object.keys() and Array.forEach() method.
const testJSON = {
"name": "Albert"
};
Object.keys(testJSON).forEach(key => {
testJSON[key] = [testJSON[key]]
});
console.log(testJSON);
You can assign new value which can be string or array to json property.
For example:
const testJSON = {
"name": "Albert"
}
// new array which we will assign to previous array property
const newArray = ["Albert", "John"];
// assigning new array to old json property
testJSON.name = newArray;
Don't think so, you only do maybe like the comment above
{name: [testJSON.name]}
if you have array, u can do .map for it
I have this JSON array. I am displaying data in a table. So, How should I put 'transactionData' inside the main object? i.e. I want the object as
{
completeTime: "value",
createTime: "value2",
assigneeName:"value3",
assigneeMap:"value4"
}
Since this is a JSON Array, so I need a way to iterate the array and make each of the objects as required.
I can't use the original JSON array since the object transactionData is not fixed and its keys might change. So, I don't want to hardcode any value like assigneeMap or assigneeName as it might change.
Hence, I want whatever the values in transactionData object are there, I want to insert it into my main object.
use the array.map(), something like this
results.map(x=>{
{
completeTime: x.completeTime,
createTime: x.createTime,
assigneeName: x.assigneeName || x.assigneename,
assigneeMap:x.assigneeMap || x.assigneeMap || x.yourChangedKey,
}
})
Use Array.prototype.map()
const result = arr.map(item => {
const { completeTime, createTime, data: { transactionData } } = item;
const { assigneeName, assigneeMap } = transactionData;
return {
completeTime,
createTime,
assigneeName,
assigneeMap
}
});
I have incoming json objects. What i want to do is to filter the JSON based on its key. For example I have three incoming json objects.
Sending message: {"deviceId":"test","temperature":11,"latitude":50,"longitude":19,"time":1,"id":398}
Sending message: {"deviceId":"temp","temperature":11,"latitude":50,"longitude":19,"time":1,"id":399}
Sending message: {"deviceId":"temp","temperature":11,"latitude":50,"longitude":19,"time":1,"id":400}
Sending message: {"deviceId":"test","temperature":11,"latitude":50,"longitude":19,"time":1,"id":01}
Sending message: {"deviceId":"temp","temperature":11,"latitude":50,"longitude":19,"time":1,"id":402}
I want only those json objects where deviceid is test and not temp. I am trying to filter the JSON. How can i do this?
I'll write you an example
say you have list of
let obj = {
"deviceId":"test",
"temperature":11,
"latitude":50,
"longitude":19,
"time":1,
"id":398
}
Then you can selecte deviceId by obj['deviceId']
for (let index in listOfObj){
let curObj = listOfObj[index];
if(curObj['deviceId'] === 'test'){
//Do whatever you want with the object
}
}
This is a basic example which describes how you can check whether the deviceId is 'test' or 'temp' and based on that you can run a loop on the object keys and apply whatever logic you want to apply on it.
Hope this helps.
document.addEventListener("DOMContentLoaded",function(e){
var obj = {
"deviceId":"test",
"temperature":11,
"latitude":50,
"longitude":19,
"time":1,
"id":398
}
if(obj["deviceId"]==="test"){
Object.keys(obj).forEach(function(e){
alert(e+" "+obj[e]);
})
}
})
I suggest you to use Lodash library.
var messages = [{msg1}, {msg2}];
var filteredMessages = _.filter(messages, {deviceId: "test"});
// filteredMessages is an Array of messages with deviceId equals to `test`
Assuming your objects are in an array, you can achieve this using the native Javascript Array#filter method,
var arr = [
{"deviceId":"test","temperature":11,"latitude":50,"longitude":19,"time":1,"id":398},
{"deviceId":"temp","temperature":11,"latitude":50,"longitude":19,"time":1,"id":399},
{"deviceId":"temp","temperature":11,"latitude":50,"longitude":19,"time":1,"id":400},
{"deviceId":"test","temperature":11,"latitude":50,"longitude":19,"time":1,"id":01},
{"deviceId":"temp","temperature":11,"latitude":50,"longitude":19,"time":1,"id":402}
];
var results = arr.filter(function(obj) {
// Return true to keep the element
return obj.deviceId === "test";
});
console.log(results);
Initially I have variable that contains string data
var stringArray = "[{'middleName':'T','lastName':'TEST5LAST','occupation':'QA','homeAddressZipcode':'85001','entitySequenceNumber':7,'homeAddressCity':'PHOENIX','ssn':'111111115','cellPhone':'4807363273','employer':'ABC','workPhoneExtension':'5555','entityType':'JOINT','homePhone':'4807363272','identificationType':'0','email':'FIRST5.LAST5#TEST.COM','workPhone':'4807363274','firstName':'TEST5FIRST','homeAddressStreet':'8000','homeAddressState':'AZ'}]";
Is there a function in JavaScript or AngularJS that will allow me to build a real JavaScript Array like this?:
var realArray = [
{
"middleName":"T",
"lastName":"TEST1LAST",
"occupation":"HR",
...
},
{
"middleName":"T",
"lastName":"TEST5LAST",
"occupation":"QA",
...
},
...
];
Try this.
realArray = JSON.parse(stringArray);
JSON.parse(stringArray) return JSON object
I have a JSON object like the following:
{"Data": {
"290": {
...
}
"300": {
...
}
"281": {
...
}
}
}
How would I sort this JSON based on the top container keys (i.e. "290", "300", "281")?
Edit: So I used
$.getJSON('current/csf.txt', function(data) { arr = data["Data"]; }
And it sorted them based on the key. Why did this happen?
You've tagged this "JavaScript" so I assume you mean "A JavaScript object generated from this JSON".
In which case:
Loop over the property names (with a for in loop).
Use them to populate an array.
Sort the array.
Use the array as a map.
(You can't store ordered data in an object).
If you want to store the results in JSON, then you will need to change your data structure (and use an array (of objects)). Objects are explicitly unordered.
Your structure is wrong, it should be something like:
{
"Data": [
{
"id": "290"
},
{
"id": "300"
},
{
"id": "282"
}
]
}
Objects are for unordered data. Use arrays for ordered data. And the array is really easy to sort here:
obj.Data.sort(function(a,b){
return a.id - b.id;
});
You can convert to this structure like so:
function copyProps(dest, src) {
for (var key in src) {
dest[key] = src[key];
}
return dest;
}
var newData = [];
for (var key in obj.Data) {
newData.push(copyProps({
id: key
}, obj.Data[key]));
}
I agree with Amberlamps comment, you shouldn't be trying to sort the keys of an object, but if you wanted to for some reason you might take a look at underscore.js's sortBy method
Even if you COULD sort object attributes, there's no guarantee that you could read them back in sorted order. In Javascript, object attributes are not stored in a specific order; an object simply has attributes.
You cannot use array index notation to access object attributes, so the notion of sorting object attributes by key is moot.