Array of objects to grid/table in Angular - json

I have data as an array of objects like this:
[{
"x":1,
"y":10,
"val": 13
},
{
"x":1,
"y":20,
"val": 15
},
{
"x":2,
"y":10,
"val": 12
},
{
"x":2,
"y":20,
"val": 16
}
]
I want this data to be displayed in an HTML table like this:
1 2
10 | 13 12
20 | 15 16
How to proceed?

I would parse the data so you have a 2d array. Something like this:
[
[13,12],
[15,16],
]
and then use two nested ng-for loops to render the content:
<div *ngFor="let row of matrix">
<span *ngFor="let item of row">{{item}}</span>
</div>
and place the headers appart.

Related

Add array into json for each row of a dataframe

I would like to populate the following json array for each row of the data frame
Json Array
"techniques": [
{
"techniqueID": "",
"tactic": "",
"color": "",
"comment": "",
"enabled": true,
"metadata": [],
"links": [],
"showSubtechniques": false
}
]
Dataframe
techniqueID Value color tactic
0 T1078 13 #74c476 Defense-Evasion
1 T1078 13 #74c476 Initial-Access
2 T1078 13 #74c476 Persistence
3 T1078 13 #74c476 Privilege-Escalation
4 T1110 5 #74c476 Credential-Access
5 T1070 3 #a1d99b Defense-Evasion
6 T1059 3 #a1d99b Execution
7 T1114 3 #a1d99b Collection
8 T1098 3 #a1d99b Persistenc
I have tried to convert the json into a data frame and combine the two but this does not then create a array for each row.
It seems that you are looking for to_dict('records') (possibly need to add missing columns):
res_dict = {
'techniques': df.to_dict('records')
}

JMESPath - How to check if key's value is contained in JSON object and return the value if true? [duplicate]

When I have a simple JSON like:
{
"name": "Tom",
"age": 20
}
Is there any JMESPath query to get age only when name is Tom?
The query should get 20 with the upper JSON.
But, if the name is not Tom like:
{
"name": "Bob",
"age": 31
}
The query should return null.
In order to filter, you will need an array.
And you can get an array from any object with the function to_array.
Then, because you have an unique object, you can stop the projection created by the filter, and, take the first element of the array, using | [0], as explained in the pipe expressions section of the tutorial.
So with the query:
to_array(#)[?name == `Tom`].age | [0]
This will give 20 for the JSON
{
"name": "Tom",
"age": 20
}
This will give null for the JSON
{
"name": "Bob",
"age": 31
}

Extracting multiple values having same path in json using json map in sas

Dose anyone can help me get multiple values in a json having same path using a json map. Any help is appreciated. Thank you.
JSON
{
"totalCount": 2,
"facets": {},
"content": [
[
{
"name": "customer_ID",
"value": "1"
},
{
"name": "customer_name",
"value": "John"
}
]
]
}
JSON MAP
{
"DATASETS": [
{
"DSNAME": "customers",
"TABLEPATH": "/root/content",
"VARIABLES": [
{
"NAME": "name",
"TYPE": "CHARACTER",
"PATH": "/root/content/name"/*output as customer_ID*/
},
{
"NAME": "name",
"TYPE": "CHARACTER",
"PATH": "/root/content/name"/*output as customer_name*/
},
{
"NAME": "value",
"TYPE": "CHARACTER",
"PATH": "/root/content/value"/*output as 1*/
},
{
"NAME": "value",
"TYPE": "CHARACTER",
"PATH": "/root/content/value"/*output as John*/
}
]
}
]
}
When i use the above json map I get the output for name as only "customer_name", but i need both "customer_ID" and "customer_name" in the output.
Similarly i need both values of "value"
JSON is a hierarchy of name-value pairs. The JSON engine in SAS will take the "name" and assign as a variable name, and then populate with the value. In your JSON, there are two sets of name-values, one being the name of an intended variable, and another being its value. This is a common output scheme we find in GraphQL responses -- and these require a little manipulation to turn into 2-D data sets.
For your example, you could use PROC TRANSPOSE:
libname j json fileref=test;
proc transpose
data=j.content
out=want;
id name;
var value;
run;
Output:
customer_ customer_
Obs _NAME_ ID name
1 value 1 John
You can also do more manual seek/assignment by using DATA step to process what you see in the ALLDATA member in the JSON libname. In your example, SAS sees that as:
Obs P P1 P2 V Value
1 1 totalCount 1 2
2 1 facets 0
3 1 content 0
4 1 content 0
5 2 content name 1 customer_ID
6 2 content value 1 1
7 1 content 0
8 2 content name 1 customer_name
9 2 content value 1 John
Processing the ALLDATA member is not as friendly as using the relational data that the JSON engine can create, but I find with GraphQL responses that's what you need to do to get more control over the name, length, and type/format for output variables.

Add elements within array with same field name in jq

Need to add array elements with same field name
Input:
[
{
"all": 1,
"sys": "bus"
},
{
"all": 14,
"sys": "bus"
}
]
I have tried like below:
.[] | (.all +.all)
but got result like
2
28
Expected result: 15 (1 + 14)
First map, then add:
map(.all) | add
C'est tout.

Is there any method by which a parent key can be accessed based on a condition on a key in the child object in jmespath?

I have been working with parsing a big json file. I am unable to use jq as its been tested in our systems where it takes comparitively good amount of time for completion of parsing. We have been using Jmespath as it has been tested fine with json that we use. Sample json given below :-
{
"student": [
{
"rob": {
"roll": 12
}
},
{
"tom": {
"roll": 9
}
},
{
"mary": {
"roll": 21
}
}
]
}
The problem is , I would have to fetch all names of the students along with roll greater than 10.
I have initially worked on jq and the below syntax is what I am looking for in JMESPath.
".student | .[] | select(.[].roll>=10) | { name : keys[], rollno : .[].roll}"
Required output is
{
"name": "rob",
"rollno": 12
}
{
"name": "mary",
"rollno": 21
}
I currently do not have option of dealing this with problem through within program as the software design requires me to write native jmespath query than handling it in program side.
With this code: student[*].{name: keys(#)[0], rollno: *.roll | [0]} | #[?rollno > `10`]
you will get this:
[
{
"name": "rob",
"rollno": 12
},
{
"name": "mary",
"rollno": 21
}
]