JSON equivalent of XML - json

I want to create JSON file equivalent to below mentioned XML file
<Data>
<EE id="1001">
<a1>50</a1>
<a2>100</a2>
<a3>25</a3>
<a4>10</a4>
<a5>1</a5>
<a6>1</a6>
</EE >
<EE id="1002">
<a1>75</a1>
<a2>60</a2>
<a3>35</a3>
<a4>20</a4>
<a5>1</a5>
<a6>1</a6>
</EE >
<EE id="1003">
<a1>100</a1>
<a2>80</a2>
<a3>50</a3>
<a4>40</a4>
<a5>10</a5>
<a6>10</a6>
</EE >
</Data>
What will be the JSON equivalent of above xml?
Also please share the best Guide/Tutorial on JSON

{
"1001": {
"a": [
50,
100,
25,
10,
1,
1
]
},
"1002": {
"a": [
75,
60,
35,
20,
1,
1
]
},
"1003": {
"a": [
100,
80,
40,
50,
10,
10
]
}
}

Related

Mapping an array in inside another array and display into a table in more than one entry

I have data that include the result of student each in an array but also with different subject basing on subject that they undertake. I want to map through them to be able to insert it into a table with respect to the subject and they mark which has to be matching.
This is my API result
{
"status": 200,
"message": "Successfully retrieve",
"data": [
{
"studentNumber": "22001",
"gender": "M",
"result": [
{
"moduleCode": "CHE1163",
"continousAssesment": 42,
"Exam": 6,
"workMaxContinousAssement": 55,
"actualContinousAssesment": 38.18181818181819,
"actualMaxContinousAssesment": 50,
"maxExam": 50,
"total": 44.18181818181819,
"studentNumber": "22001",
"moduleCredit": 10
},
{
"moduleCode": "MEE1162",
"continousAssesment": 10,
"Exam": 40,
"workMaxContinousAssement": 45,
"actualContinousAssesment": 11.11111111111111,
"actualMaxContinousAssesment": 50,
"maxExam": 50,
"total": 51.111111111111114,
"studentNumber": "22001",
"moduleCredit": 15
}
]
},
{
"studentNumber": "1111",
"gender": "M",
"result": [
{
"moduleCode": "CHE1163",
"continousAssesment": 0,
"Exam": 23,
"workMaxContinousAssement": 0,
"actualContinousAssesment": 0,
"actualMaxContinousAssesment": 50,
"maxExam": 50,
"total": 23,
"studentNumber": "1111",
"moduleCredit": 10
},
{
"moduleCode": "MEE1162",
"continousAssesment": 12,
"Exam": 20,
"workMaxContinousAssement": 45,
"actualContinousAssesment": 13.333333333333334,
"actualMaxContinousAssesment": 50,
"maxExam": 50,
"total": 33.333333333333336,
"studentNumber": "1111",
"moduleCredit": 15
}
]
},
{
"studentNumber": "667",
"gender": "F",
"result": [
{
"moduleCode": "CHE1163",
"continousAssesment": 0,
"Exam": 3,
"workMaxContinousAssement": 0,
"actualContinousAssesment": 0,
"actualMaxContinousAssesment": 50,
"maxExam": 50,
"total": 3,
"studentNumber": "667",
"moduleCredit": 10
},
{
"moduleCode": "MEE1162",
"continousAssesment": 17,
"Exam": 40,
"workMaxContinousAssement": 45,
"actualContinousAssesment": 18.88888888888889,
"actualMaxContinousAssesment": 50,
"maxExam": 50,
"total": 58.888888888888886,
"studentNumber": "667",
"moduleCredit": 15
}
]
}
]
}
the result I should be expecting is something like this
I have tried to map from to but it showing nothing when I replace the value as variable
this is the part of the table code
overallResult.map((item) => {
return (
<>
<tr>
<td></td>
<td>{item.studentNumber}</td>
<td className="font-medium">{item.gender}</td>
<td>44.18181818181819</td>
<td>51.111111111111114</td>
<td>45</td>
</tr>
</>
)
}
This should be mapping and then insert the actual value into the for the map which is corresponding to the student as seen in table picture
I updated your sandbox link.
Instead of hard coded data, now your table is populated with the data from overallResult object.
It is not perfect, but at least you can see how to loop through a data and display on the screen.
https://codesandbox.io/s/angry-bardeen-brkzf5?file=/src/App.js

Calculate min and max from json data and store the result as list

I have data in JSON format, the sample is given below. I want to calculate the min and max range for the values inside the variable i.e. align_X, align_Y, align_Z.
{"id": 0, "variable": {"align_X": 41, "align_Y": 51, "align_Z": 80}}
{"id": 1, "variable": {"align_X": 0}}
{"id": 2, "variable": {"align_Y": 1, "align_Z": 0}}
Desired output is:
"align_X": [
0.0,
41.0
],
"align_Y": [
1.0,
51.0
],
"align_Z": [
0.0,
80.0
]
Any help is appropriated.
Thank you
Can you try this
var test = [{
"id": 0,
"variable": {
"align_X": 41,
"align_Y": 51,
"align_Z": 80
}
}, {
"id": 1,
"variable": {
"align_X": 0
}
}, {
"id": 2,
"variable": {
"align_Y": 1,
"align_Z": 0
}
}
]
var result = test.reduce((acc, cobj) => {
for (key in cobj.variable) {
if (acc[key]) {
if(acc[key][0] > cobj.variable[key]){
if(!acc[key][1]){
acc[key][1] = acc[key][0];
}
acc[key][0] = parseFloat(cobj.variable[key]).toFixed(2)
} else if(acc[key][1] == undefined || acc[key][1] < cobj.variable[key]){
acc[key][1] = parseFloat(cobj.variable[key]).toFixed(2)
}
}
else {
acc[key] = [];
acc[key].push(parseFloat(cobj.variable[key]).toFixed(2))
}
}
return acc;
}, []);
console.log(result);
My solution
#Your input
d = [{"id": 0, "variable": {"align_X": 41, "align_Y": 51, "align_Z": 80}},
{"id": 1, "variable": {"align_X": 0}},
{"id": 2, "variable": {"align_Y": 1, "align_Z": 0}}
]
#To store result float(-inf) smallest number in python & float(inf) is largest
output = {
"align_X" : [float('inf'),float('-inf')],
"align_Y" : [float('inf'),float('-inf')],
"align_Z" : [float('inf'),float('-inf')]
}
for item in d:
for each in output.keys():
try:
if item['variable'][each]<output[each][0]:
output[each][0] = item['variable'][each]
if item['variable'][each]>output[each][1]:
output[each][1] = item['variable'][each]
except:
continue
print(output)

Is it possible to output a JSON object row by row in Angular?

I have a field "properties" in my table(data type: text). In this field there is a JSON object. Now I want to output it in Angular in a reasonable way as I have shown below. Currently only the JSON format is displayed.
There must be a way to display the attributes of a JSON format row by row, right?
"properties": "{'width': 4, 'height': 14, 'count': 10, 'qm': 63, 'loc_x': 0, 'loc_y': 0, 'loc_x': 1}",
width: 4
height: 14
count: 10
qm: 63
loc_x: 0
loc_y: 0
loc_x: 1
UDATE 1:
<div *ngFor="let property of properties | keyvalue">
{{property.key}}: {{property.value}}
</div>
UPDATE 2:
This is my JSON from the Backend
{
"id": 1,
"formId": 1,
"properties": "{'width': 4, 'height': 14, 'count': 10, 'qm': 63, 'loc_x': 0, 'loc_y': 0, 'loc_x': 1}",
"components": [
{
"id": 1,
"shapeId": 1,
"properties": "{'width': 4, 'height': 14, 'count': 10, 'qm': 63, 'loc_x': 0, 'loc_y': 0, 'loc_x': 1}",
"shapeComponentMaterials": [
{
"id": 1,
"componentId": 1,
},
{
"id": 15,
"componentId": 1,
}
]
},
{
"id": 11,
"shapeId": 1,
"properties": "{'width': 4, 'height': 14, 'count': 10, 'qm': 63, 'loc_x': 0, 'loc_y': 0, 'loc_x': 1}",
"shapeComponentMaterials": [
{
"id": 11,
"componentId": 11,
}
]
}
]
}
This is my shape.ts
export interface Shape {
id?: number;
formId: number;
properties?: string;
components?: Array<Component>;
}
You can make use of Angular's KeyValuePipe to display the data in the desired format:
<div *ngFor="let property of properties | keyvalue">
{{property.key}}:{{property.value}}
</div>
UPDATE - Extending answer as per request
As you have updated your question with the exact API response, here's the method to access the properties property to display it in HTML as required:
accessingProperties = JSON.parse(this.valueFromAPI.properties.replace(/'/g, '"'));
Here's a Stackblitz with this example in live.

How can I get only the JSON keys without using tilde sign?

I am trying to get (via JSON Path) only key values from the JSON below but with no success.
I cannot use the tilde sign (~) because JMeter's JSON Path Extractor works under JSON PATH 4.0 and ~ is not recognized.
{
"facetCount": {
"designer": {
"4856430": 2,
"7313551": 14,
"7018102": 8,
"306838": 1,
"85146": 146,
"2654979": 11,
"221111": 4,
"180510": 40,
"3344622": 59,
"472718": 73,
"107993": 19,
"166170": 58,
"6908": 2,
"426629": 1,
"1358858": 9,
"9879178": 6,
"55006": 43,
"285396": 2,
"3355": 9,
"215501": 8,
"4968477": 4,
"11349629": 7,
"11229643": 27,
"11355128": 9,
"7093068": 3,
"11098281": 2,
"5833751": 1,
"4741301": 1,
"9198104": 21,
"991324": 4
},
"attributes": {
"135979:77": 290,
"135979:83": 27,
"136227:20": 141,
"136227:78": 670,
"135985:44": 123,
"135985:43": 669,
"135979:62": 700,
"135979:61": 1188,
"136644:176": 2,
"136331:7": 1,
"136331:8": 3,
"136641:190": 13,
"136641:191": 12,
"136061:144": 3
},
"category": {
"136103": 208,
"136105": 147,
"137322": 2,
"136389": 120,
"136215": 236,
"136214": 954,
"136216": 217,
"136217": 352,
"136218": 452,
"136219": 40,
"136480": 4,
"136220": 111,
"136221": 288,
"136222": 58,
"136223": 369,
"136224": 163,
"136986": 3,
"136307": 1125,
"136059": 10,
"136308": 956,
"136315": 984,
"136003": 574,
"136045": 267,
"136035": 1501,
"135985": 1380,
"137134": 27,
"136309": 60,
"137323": 9,
"136390": 1,
"136021": 16,
"136322": 1951,
"137166": 16,
"137317": 7,
"136005": 4,
"135983": 4019,
"136033": 1513,
"136310": 1224,
"136392": 18,
"135981": 2430,
"136031": 16,
"136326": 1312,
"136061": 79
},
"colour": {
"1": 41686,
"7": 14593,
"5": 9596,
"18": 1,
"13": 5185,
"6": 5259,
"3": 6391,
"11": 5715,
"12": 1537,
"4": 8767,
"16": 1466,
"9": 8590,
"15": 1730,
"8": 8333,
"14": 3208,
"2": 13269,
"10": 2730
},
"ninetyminutes": {
"3": 309
},
"sameday": {
"3": 1714,
"42": 254
},
"size": {
"135972:1620": 523,
"136657:2650": 1,
"136657:2850": 1
},
"location": {
"3": 2674,
"4": 7671,
"5": 35808,
"6": 2761,
"7": 11948
},
"labels": {
"1300": 2969
}
}
}
I would like to get the keys that are under facetCount element (designers, attributes, colour, etc.) and also (another JSON Path expression) get the keys that are inside these keys, such as 4856430 from designers, 135979:77 from attributes, and so on.
Could you help me, please?
Thanks in advance!
You can use JSR223 PostProcessor to get key in object JSON
new groovy.json.JsonSlurper().parse(prev.getResponseData()).facetCount.eachWithIndex{ def node, int idx ->
log.info('Key ' + idx + '=' + node.getKey())
vars.put('key_' + idx, node.getKey())
}
And you can get key with a variable like that:
${key_0}, ${key_1}, ...
More detail check here How to extract values from json in jmeter when the keys are unkown?
JMeter's JSON test elements rely on Jayway Jsonpath which doesn't has this tilde operator for querying the keys, you will have to go for:
JSR223 PostProcessor
Groovy language
JsonSlurper
Add JSR223 PostProcessor as a child of the request which returns the above JSON and use the following code:
For direct keys (designer, attributes, etc)
def counter = 1
new groovy.json.JsonSlurper().parse(prev.getResponseData()).facetCount.each { facet ->
vars.put('key_' + counter, facet.key)
counter++
}
vars.put('key_matchNr', counter - 1 as String)
For child keys (4856430, 135979:77, etc)
def counter = 1
new groovy.json.JsonSlurper().parse(prev.getResponseData()).facetCount.each { child ->
child.value.keySet().each { key ->
vars.put('childKey_' + counter, key)
counter++
}
}
vars.put('childKey_matchNr', counter -1 as String)
More information: Groovy - Parsing and producing JSON

Create individual tags from array of data - Node.js

so the output of the variable result from my device is an array with the JSON response below. I'd like to take the first value of each key and make it into an individual object so that I can send it to a mongoose database to save the results. How would I do this? The values in the keys are dynamic there could be 2 or 100 depending on what it finds.
[
{
"Read_Count": [
31,
5,
23,
1
],
"Antenna": [
1,
1,
1,
1
],
"EPC": [
"300833B2DDD9014000000000",
"E280116060000207B6C9E1E2",
"300833B2DDD9014000000000",
"E2801160600002096622B8C0"
],
"Signal": [
224,
196,
212,
194
]
}
I'd like the output to be as follows:
{
"Tags": [
{ "EPC":"300833B2DDD9014000000000", "Read_Count": 31, "Antenna": 1, "Signal": 224 },
{ "EPC":"E280116060000207B6C9E1E2", "Read_Count": 5, "Antenna": 1, "Signal": 196 },
{ "EPC":"E2801160600002096622B8C0", "Read_Count": 23, "Antenna": 1, "Signal": 212 }
]
}
So for each one of the objects in the array, you need to go through its keys and set the value of that key in your result to the first element of the array in that key's value.
var result = {
'Tags': []
}
response.forEach(tag => {
var resultingTag = {}
Object.keys(tag).forEach(key => {
resultingTag[key] = tag[key][0]
})
result.Tags.push(resultingTag);
}
('response' is your input array that you posted in your first code block, if that wasn't clear)