I'm trying to figure out how to format this json in angular this is the result from extending all models from multiple forms.
{
"identifications": {},
"insurances": [
{
"insurances": [
{
"category": "",
"compName": "",
"planType": "",
"note": ""
}
]
}
],
"medical": {
"doctors": [
{
"doctors": [
{
"firstname": "James"
"note": "James Bond Note"
},
{
"firstname": "Robin",
"note": "Lorem Ipsum Dolor"
}
]
}
],
"records": {}
}
Here's I need to achieve to insert it into the API.
{
"parent_id": 17,
"insurances": [{
"insurance_type": "",
"notes": ""
}],
"medical": {
"doctors": {},
"blood": {},
},
"records": {}
}
If you're really looking at just pretty-printing stuff, investigate JSON.stringify:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify
If you're trying to change the object from one data representation to another (I'll note that the second object is structurally different from the first, not just a matter of whitespace), then you just need to write some javascript that does stuff like new.doctors = old[0].medical[1].doctors (example, not actual code)
This question is pretty old, but nowadays you should use the json filter. For example:
var data = {a: 1, b: 2};
Then in the template:
{{ data|json:2 }}
Should output
{
a: 1,
b: 2
}
Related
I'm currently working on a service that returns the following payload:
{
"account1": {
"app1": {
"status": "Online",
"comments": "blah blah",
"events": [
{
"date": "some date",
"generated_by": "some user"
}
]
}
},
"account2": {
"app1": {
"status": "Offline",
"comments": "blah blah bleh",
"events": [
{
"date": "some date",
"generated_by": "some user"
}
]
},
"app2": {
"status": "Online",
"comments": "blah blah",
"events": [
{
"date": "some date",
"generated_by": "some user"
}
]
}
}
}
I'm trying to render a table with the following fields:
-------------------------------
Application | Account | Status
-------------------------------
app1 | account1 | Online
app1 | account2 | Offline
app2 | account2 | Online
Normally this would be easy to do if my payload would be something like a list of objects but I'm kinda stuck here.
I tried to normalize this payload by extracting each of the fields and creating a new payload by doing something like the following:
const extractAccountNumber = Object.values(payload).map(account => ({account: account}))
which would return:
[
{
"account": "account1"
},
{
"account": "account2"
}
]
I wanted to move on to app name the same way and once I get all my fields I would merge the payload. This has proven to be super cumbersome and I'm sure there is a better, more efficient way to achieve this which I'm probably missing. Any feedback would help me a ton to understand how this can be achieved using javascript with or without lodash.
Iterating by first level and then by second level:
table = [];
for (accountName in apiResponse) {
account = apiResponse[accountName];
for (appName in account) {
app = account[appName];
table.push({
application: appName,
account: accountName,
status: app.status,
});
}
}
Then table is something like this:
[
{
"application": "app1",
"account": "account1",
"status": "Online"
},
{
"application": "app1",
"account": "account2",
"status": "Offline"
},
{
"application": "app2",
"account": "account2",
"status": "Online"
}
]
Can try something like
Object.entries(o).map(([accountName, value]) => ({
account: accountName,
apps: Object.entries(value)
.map(([appName, value]) => ({name: appName, ...value }))
}))
Not sure about structure. Where to put app1 from account2 in that table?
I'm a beginner and would like to know how I can get a specific object from an array
I have an Array that looks like this:
data {
"orderid": 5,
"orderdate": "testurl.com",
"username": "chris",
"email": "",
"userinfo": [
{
"status": "processing",
"duedate": "" ,
}
]
},
To get the data from above I would do something like this:
return this.data.orderid
But how can I go deeper and get the status in userinfo?
return this.data.orderid.userinfo.status
doesn't work... anyone have any ideas?
A few points:
data is not an array, is an Object (see the curly braces, arrays have squared brackets). To be really precise, your syntax is invalid, but I assume you wanted to type data = { ... }, as opposed to data { ... }
Your syntax is almost correct, the only mistake you are making is that userinfo is an array, and arrays have numeric indexes (I.e. array[0], array[1]). What you are looking for is this.data.orderid.userinfo[0].status
Use data.userinfo[0].status to get the value (in your case this.data.userinfo[0].status)
var data = {
"orderid": 5,
"orderdate": "testurl.com",
"username": "chris",
"email": "",
"userinfo": [
{
"status": "processing",
"duedate": "" ,
}
]
};
console.log(data.userinfo[0].status);
User Info is an array, so you would need to access it using indexer like so:
return this.data.userinfo[0].status
MDN on arrays: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
You need to iterate over data.userinfo (it's an array)
var data = {
"orderid": 5,
"orderdate": "testurl.com",
"username": "chris",
"email": "",
"userinfo": [
{
"status": "processing",
"duedate": "" ,
}
]
};
data.userinfo.forEach(function(element) {
console.log(element.status);
});
My ajax call returns data like this:
{"result":[{"stats":{"min":{"caller_id.name":"Person1"},"count":"5"},"groupby_fields":[{"field":"caller_id","value":"ce4ddid73hes6e806d7070e21f961987"}]},{"stats":{"min":{"caller_id.name":"Person2"},"count":"2"},"groupby_fields":[{"field":"caller_id","value":"36ffbcfedbf9ba406d7070e21f96199c"}]},{"stats":{"min":{"caller_id.name":"Person3"},"count":"1"},"groupby_fields":[{"field":"caller_id","value":"714dd540dbefbe806d7070e21f96196a"}]},{"stats":{"min":{"caller_id.name":"Person4"},"count":"1"},"groupby_fields":[{"field":"caller_id","value":"ce4dd540dbefbe806d7070e21f961987"}]}]}
I then use var json = $.parseJSON(data); and get this (from the console log):
{result: Array(4)}
result:Array(4)
0:groupby_fields:Array(1)
0:{field: "caller_id", value: "ce4ddid73hes6e806d7070e21f961987"}
length:1
__proto__:Array(0)
stats:count:"5"
min:{caller_id.name: "Person1"}
__proto__:Object
__proto__:Object
1:{stats: {…}, groupby_fields: Array(1)}
2:{stats: {…}, groupby_fields: Array(1)}
3:{stats: {…}, groupby_fields: Array(1)}
length:4
__proto__:Array(0)
__proto__:Object
In previous calls I loop through the data to pull the values using something like this
var callerName = json[i]["caller_id.name"];
I'm not sure how to do it in this scenario.
I tried a few variations of the line above but didn't have any success.
I need to get the caller_id, count, and caller_id.name.
There are two types of structures in JSON: arrays [value1, value2] and objects {"key1":"value1", "key2":"value2"}.
In Javascript, you access:
array elements via array[index].
object properties via object.property or object['property']. The latter is required if the property name includes special characters.
You can chain these, so for instance you could write:
json.result[0].stats.min['caller_id.name']
json.result[0].stats.count
json.result[0].groupby_fields[0].field
json.result[0].groupby_fields[0].value
and so on.
This should't be difficult. Assuming the value of i is 0, here is the way by which you can get corresponding values:
let data = {
"result": [{
"stats": {
"min": {
"caller_id.name": "Person1"
},
"count": "5"
},
"groupby_fields": [{
"field": "caller_id",
"value": "ce4ddid73hes6e806d7070e21f961987"
}]
}, {
"stats": {
"min": {
"caller_id.name": "Person2"
},
"count": "2"
},
"groupby_fields": [{
"field": "caller_id",
"value": "36ffbcfedbf9ba406d7070e21f96199c"
}]
}, {
"stats": {
"min": {
"caller_id.name": "Person3"
},
"count": "1"
},
"groupby_fields": [{
"field": "caller_id",
"value": "714dd540dbefbe806d7070e21f96196a"
}]
}, {
"stats": {
"min": {
"caller_id.name": "Person4"
},
"count": "1"
},
"groupby_fields": [{
"field": "caller_id",
"value": "ce4dd540dbefbe806d7070e21f961987"
}]
}]
};
let i = 0;
console.log(data.result[i].stats.min['caller_id.name']);
console.log(data.result[i].stats.count);
console.log(data.result[i].groupby_fields[0].value);
I hope you can loop through data.result using a loop and provide specific values of i to the expressions.
To get the 'caller_id.name' key use
var json = {"result":[{"stats":{"min":{"caller_id.name":"Person1"},"count":"5"},"groupby_fields":[{"field":"caller_id","value":"ce4ddid73hes6e806d7070e21f961987"}]},{"stats":{"min":{"caller_id.name":"Person2"},"count":"2"},"groupby_fields":[{"field":"caller_id","value":"36ffbcfedbf9ba406d7070e21f96199c"}]},{"stats":{"min":{"caller_id.name":"Person3"},"count":"1"},"groupby_fields":[{"field":"caller_id","value":"714dd540dbefbe806d7070e21f96196a"}]},{"stats":{"min":{"caller_id.name":"Person4"},"count":"1"},"groupby_fields":[{"field":"caller_id","value":"ce4dd540dbefbe806d7070e21f961987"}]}]}
//json['result'][0]["stats"]["min"]['caller_id.name']
for (x in json['result']){ console.log(r['result'][x]["stats"]["min"]['caller_id.name'])}
result:
"Person1"
"Person2"
"Person3"
"Person4"
I wrote a quick script to parse two fairly large json file (~17k records) to do a comparison of the two. I have confirmed they are both valid json (via jsonlintpro) and the same format. (The source is the same so this should be a given. But, I always assume the mistake is mine. And I still do. Just somewhere else.) However, the parsed file just outputs [object, Object]. I'm wondering what the cause could possibly be?
The json format is like this small snippet (anonymized of course):
[
{
"id": "1234",
"name": "Name1",
"url": "https://localhost/Name1",
"date_created": "2013-07-05T18:47:05Z",
"date_cancelled": "",
"props": [
{
"id": "54321",
"type": "Client",
"value": "General Store"
},
{
"id": "65432",
"type": "Contact_Name",
"value": "Joe Smith"
}
]
},
{
"id": "23456",
"name": "Name2",
"url": "https://localhost/Name2",
"date_created": "2014-02-27T17:46:43Z",
"date_cancelled": "",
"props": [
{
"id": "34567",
"type": "Client",
"value": "Bait Shop"
}
]
}]
And here is the pertinent code:
var _ = require('underscore');
var recs = require('./prod.json');
printArr(recs);
console.log(recs.length);
function printArr(arr) {
arr.forEach(function(item) {
console.log(item + ", ");
});
}
Any guidance would be greatly appreciated.
UPDATE:
Ok, so apparently the issue is with my printArr function. I'm not sure what I'm doing wrong there. I'd like to figure it out because I want to expand upon that so I can print selectively.
the parsed file just outputs [object, Object].
This is the expected behavior BECAUSE you are concatenating an object with a string.
Try console.log(item) instead
console.log(item); should indeed print [object, Object], did you try to output its properties instead?
function printArr(arr) {
arr.forEach(function(item) {
console.log( item.id, item.name, item.url, item.date_created, item.date_cancelled, item.props, ';');
});
}
Just export the value from the prod.json file.
prod.json file
module.exports = [
{
"id": "1234",
"name": "Name1"
},
{
"id": "1234",
"name": "Name1"
}]
elsewhere
var recs = require('./prod.json')
console.log(recs)
I am new to json. I have json output that looks like this
[
{
"employees": {
"education": "BE\/B.Tech"
},
"0": {
"count": "1"
}
},
{
"employees": {
"education": "MBA"
},
"0": {
"count": "3"
}
}
]
I want to retrieve the employee's education and the count. I have tried but i am not able to retrieve the values.
I appreciate any help.
Thanks.
Assuming your JSON string is in a variable $json, it goes like this:
var employees_list = JSON.parse($json);
Then you can access the information via:
employees_list[0].employees.education // gives you "BE\/B.Tech"
// and
employees_list[0]["0"].count // gives you 1.
You can also loop over the array and access all the different education this way.
Update:
To better demonstrate which expression accesses which information:
[ // employees_list
{ // employees_list[0]
"employees": { // employees_list[0].employees
"education": "BE\/B.Tech" // employees_list[0].employees.education
},
"0": { // employees_list[0]["0"]
"count": "1" // employees_list[0]["0"].count
}
},
{ // employees_list[1]
"employees": { // employees_list[1].employees
"education": "MBA" // employees_list[1].employees.education
},
"0": { // employees_list[1]["0"]
"count": "3" // employees_list[1]["0"].count
}
}
]
Generally employees_list[0].employees is the same as employees_list[0]["employees"] but this does not work for numbers, because properties and variables are not allowed to start with numbers. So you can only use employees_list[0].["0"] and not employees_list[0].0.
The structure of your JSON string looks a bit strange though. You should consider to structure it differently if you can.
For example:
[
{
"education": "BE\/B.Tech",
"count": "1"
},
{
"education": "MBA"
"count": "3"
}
]
The "0" key in your original JSON string seems to serve no purpose and just complicates the access.