Angular JSON pipe with angular-datatables - json

I'm using angular-datatables to display NoSQL de-normalized data in grid for visualization purpose,
I have few complex nested json objects and wanted to display specific cell with prettified json with inbuilt JsonPipe
I'm using datatables with data binding as
<table id="dtTable" datatable [dtOptions]="data"></table>
Sample JSON
[
{
"Id": "1",
"Name": "Test",
"Account": {
"Id": "12",
"Name": "Stackoverflow",
"Contact": {
"Id": "23",
"Name": "stack exchange",
"Phone1": "712426",
"Phone2": "490591",
"Address": {
"Id": "12",
"Name": "Address 1",
"AddressType": "commercial"
}
}
},
"CreatedBy": {
"Id": "123",
"Name": "User 1"
},
"CreatedDate": "2022-04-11T10:42:28.7525823Z",
"ModifiedBy": {
"Id": "124",
"Name": "User 2"
},
"ModifiedDate": "2022-04-11T10:42:28.7525823Z"
},
{
...
},
...
]
want to render as
Id
Name
Account
Created By
1
Test
{ Pretified JSON}
{Json}
Do we have any option to render entire json content in specific column cell of tables using angular-datatables? or do we have any other option other than json pipe to display formatted json content in angular-datatables

Yes, you can use the build in pipe as you mentioned:
In HTML directly:
...
<td>{{accountInfo | json}}</td>
...
Or in TS file using the transform function.
...
this.accountInfo = this.jsonPipe.transform(response.accountInfo)
...
If you use the transform function, be sure that the Pipe is imported properly in the TS file.

Related

Create merged JSON array from multiple files using jq

I have multiple JSON files one.json, two.json, three.json with the below format and I want to create a consolidated array from them using jq. So, from all the files I want to extract Name and Value field inside the Parameters and use them to create an array where the id value will be constructed from the Name value and value field will be constructed using Value field value.
input:
one.json:
{
"Parameters": [
{
"Name": "id1",
"Value": "one",
"Version": 2,
"LastModifiedDate": 1581663187.36
}
]
}
two.json
{
"Parameters": [
{
"Name": "id2",
"Value": "xyz",
"Version": 2,
"LastModifiedDate": 1581663187.36
}
]
}
three.json
{
"Parameters": [
{
"Name": "id3",
"Value": "xyz",
"Version": 2,
"LastModifiedDate": 1581663187.36
}
]
}
output:
[
{
"id": "id1",
"value": "one"
},
{
"id": "id2",
"value": "xyz"
},
{
"id": "id3",
"value": "xyz"
}
]
How to achieve this using jq
You can use a reduce expression instead of slurping the whole file into memory (-s); by iterative manipulation of the input file contents and then appending the required fields one at a time.
jq -n 'reduce inputs.Parameters[] as $d (.; . + [ { id: $d.Name, value: $d.Value } ])' one.json two.json three.json
The -n flag is to ensure that we construct the output JSON data from scratch over the input file contents made available over the inputs function. Since reduce works in an iterative manner, for each of the object in the input, we create a final array, creating the KV pair as desired.

How to display item of nested json array in component using Angular 7

How can I display "empName" and other details of this JSON in my table component in loop?
I'm using a third party API which provides a nested JSON object in return when I send employerID to the API URL.
After subscribing I'm storing the response in a var "AllPostedJobs"
{
"status": "All Jobs",
"result": [
{
"_id": "5c90fd3cfc7f3b0017803319",
"job_title": "Web Designer",
"job_desc": "test description ...",
"location": "Mangalore",
"experiance": "Freshers",
"job_type": "Full-Time",
"salary_package": "3L",
"job_keywords": "Photoshop, Illustrator",
"email_id": "hr#shreemithra.com",
"employerID": "5c7e99c2a7a9eb00174de2b2",
"company_name": "Shreemithra Designs",
"AppliedStudentDetails": [
{
"_id": "5c9393c1a918d60017de7e55",
"empName": "Anup",
"empID": "5c939375a918d60017de7e53",
"job_title": "Web Designer"
}
],
"__v": 1
},
{
"_id": "5c913570cb78a100177ab23a",
"job_title": "Full Stack Developer",
"job_desc": "htjhsv dhsd jh jds fjshgdfkhsdmhfd;lw eiwiwemsd. This is a sample job description.",
"location": "Pune",
"experiance": "2 Years",
"job_type": "Part-Time",
"salary_package": "8L - 10L PA",
"job_keywords": "Angular, Node JS, React, HTML5. CSS3",
"email_id": "info#shreemithra.com",
"employerID": "5c7e99c2a7a9eb00174de2b2",
"company_name": "Shreemithra Designs",
"AppliedStudentDetails": [
{
"_id": "5c9393c9a918d60017de7e56",
"empName": "Anup",
"empID": "5c939375a918d60017de7e53",
"job_title": "Full Stack Developer"
},
{
"_id": "5ca60fa5ba17730017182ca8",
"empName": "Amit Pateriya",
"empID": "5c7795acfd39640017ca4c37",
"job_title": "Full Stack Developer"
}
],
"__v": 2
}
]
}
The simplest way is when you receive data from api then send it to a
function then apply multiples loop alter your data by add keys in
front of data and that values for example
var data = [{"id":1, "name":"smith","applicant":{"roll": 32,"class":10}}];
data[0].applicantRoll = data[0].applicant.roll;
data[0].applicantClass = data[0].applicant.class;
now you can apply *ngfor easily, try this.
You can bind display data by binding controls in an HTML template to properties of your component.
The easiest way to display a component property is to bind the property name through interpolation. With interpolation, you put the property name in the view template, enclosed in double curly braces:{{AllPostedJobs.status}}.
<div id="result-container" *ngFor="let record of AllPostedJobs.result">
<another-component [record]= "record"></another-component>
</div>
Or depending on your need you can hand over entire result data to another-component.
Now your another-component, should have #Input defined to handle the incoming data:
export class AnotherComponent {
#Input() record: Array<any>;
In your another-component template:
<div *ngFor="let student of record?.AppliedStudentDetails">
<span>{{student.empName}}</span>
<span [innerText]="student.job_title"></span>
</div>

JSON is it best practice to give each element in an array an id attribute?

Is it best practice in JSON to give objects in an array an id similar to below?. Im trying to decide on a JSON format for a restful service im implementing and decide include it or not... If it is to be modified by CRUD operations is it a good idea?
{
"tables": [
{
"id": 1,
"tablename": "Table1",
"columns": [
{
"name": "Col1",
"data": "-5767703747778052096"
},
{
"name": "Col2",
"data": "-5803732544797016064"
}
]
},
{
"id": 2,
"tablename": "Table2",
"columns": [
{
"name": "Col1",
"data": "-333333"
},
{
"name": "Col2",
"data": "-44444"
}
]
}
]
}
Client-Generated IDs
A server MAY accept a client-generated ID along with a request to
create a resource. An ID MUST be specified with an "id" key, the value
of which MUST be a universally unique identifier. The client SHOULD
use a properly generated and formatted UUID as described in RFC 4122
[RFC4122].
jsonapi.org

Can we add array of objects in amazon cloudsearch in json format?

I am trying to create a domain and uploading a sample data which is like :
[
{
"type": "add",
"id": "1371964",
"version": 1,
"lang": "eng",
"fields": {
"id": "1371964",
"uid": "1200983280",
"time": "2013-12-23 13:00:26",
"orderid": "1200983280",
"callerid": "66580662",
"is_called": "1",
"is_synced": "1",
"is_sent": "1",
"allcaller": [
{
"sno": "1085770",
"uid": "1387783883.30547",
"lastfun": null,
"callduration": "00:00:46",
"request_id": "1371964"
}
]
}
}]
when I am uploading sample data while creating a domain, cloudsearch is not taking it.
If I remove allcaller array then it takes it smoothly.
If cloudsearch does not allowing object arrays, then how should I format this json??
Just found after searching on aws forums, cloudsearch doesnot allow nested json (object arrays) :(
https://forums.aws.amazon.com/thread.jspa?messageID=405879&#405879
Time to try Elastic search.

How to store a Json File Using Lift Mapper in MySql

I am new to Liftweb. I want to Store a Json File in Mysql database using Lift Mapper
My Json File Like Below:-
[
{
"name": "Root Category",
"Id": "1",
"dispName": "",
"childs": [
{
"name": "Sub Category",
"Id": "",
"dispName": "",
"childs": [
{
"name": "Spec1",
"Id": "",
"dispName": "",
"childs": []
}
]
}
]
},
{
"name": "Root Category",
"Id": "",
"dispName": "",
"childs": [
{
"name": "Sub Category",
"Id": "",
"dispName": "",
"childs": [
{
"name": "Spec1",
"Id": "",
"dispName": "",
"childs": []
}
]
}
]
}
]
Is it Possible to store a Json File in Lift Mapper .Please give me Suggestions. It will be great if some one provide any sample
Best Regards
GSY
At the moment there is no good support for storing JSON in MySQL. I mean it's not going to provide capabilities MongoDB provides for example. However there are some JSON processing functions provided by community if you want. Given all that you can store it in VARCHAR. TEXT or BLOB field type as simple text. Here is a Mapper example:
import net.liftweb.mapper._
import net.liftweb.common._
class SomeDbClass extends LongKeyedMapper[SomeDbClass] with IdPK {
def getSingleton = SomeDbClass
// set limit of chars - can be used in `validate()`
object quota_type extends MappedString(this, 1024)
}
object SomeDbClass extends SomeDbClass with LongKeyedMetaMapper[SomeDbClass]
For one of my projects I store JSON as a string in Postgres similarly because I just need to read and write it without having to parse it in DB and query by fields. Whenever I need efficient JSON storage with query and update support I use MongoDB with Record + ( Casbah or Rogue ).