I have an application that logs to a text file, which then i convert to a JSON with a script. That JSON is loaded into chrome:://tracing in Chromium.
There is documentation for the tracing format here.
I got the "simple" version with Duration Events working, but i can't figure out to log "Flow Events" (i find the documentation confusing). I've tried several ways and it either refuses the JSON as invalid, or accepts it but doesn't show any lines/arrows.
Can someone explain the format and ideally post a small example JSON of Flow Events ?
I managed in the following JSON to have a flow event created (and drawn) from subfunction ABTh1sF1 (40ms) to enclosing function ABTh1F1 (89ms). These nested functions are within one slice.
The flow event is also shown under "Flow events" menu.
{
"traceEvents":[
{"cpu":0,"args":{"name":"ABProcess"},"cat":"__metadata","name":"process_name","ph":"M","pid":1,"tid":0,"ts":0},
{"args":{"name":"ABThread1"},"cat":"__metadata","name":"thread_name","ph":"M","pid":1,"tid":1,"ts":0},
{"args":{},"cat":"cc2","name":"ABTh1F1","ph":"B","pid":1,"tid":1,"ts":30},
{"args":{},"cat":"cc2","name":"ABTh1sF1","ph":"B","pid":1,"tid":1,"ts":40},
{"cat": "foo", "name": "async_flow", "id":100, "ph": "s", "ts":41, "pid":1,"tid":1},
{"args":{},"cat":"cc2","name":"ABTh1sF1","ph":"E","pid":1,"tid":1,"ts":55},
{"cat": "foo", "name": "async_flow", "id":100, "ph": "f", "ts":89, "pid":1,"tid":1},
{"args":{},"cat":"cc2","name":"ABTh1F1","ph":"E","pid":1,"tid":1,"ts":90}
]
}
Because the other answer didn't work for me, here's another example.
Remember to enable it using the top right Flow events button.
{
"traceEvents": [
{
"cat": "example",
"name": "parent",
"ph": "B",
"pid": 1,
"tid": 1,
"ts": 0
},
{
"cat": "example",
"name": "child",
"ph": "B",
"pid": 1,
"tid": 2,
"ts": 25
},
{
"cat": "example",
"name": "flow",
"id": 100,
"ph": "s",
"ts": 0,
"pid": 1,
"tid": 1
},
{
"cat": "example",
"name": "child",
"ph": "E",
"pid": 1,
"tid": 2,
"ts": 75
},
{
"cat": "example",
"name": "flow",
"id": 100,
"ph": "f",
"bp": "e",
"ts": 25,
"pid": 1,
"tid": 2
},
{
"cat": "example",
"name": "parent",
"ph": "E",
"pid": 1,
"tid": 1,
"ts": 100
}
]
}
Related
I wish to match 2 json files based on common id
I've tried using awk, jq and the npm json package in quite a lot of different ways but nothing have gotten close to working.
The 2 json files are not sorted and do not contain all the same entries. they contain the common networkId, I only want the output to contain the entries from file2.
Hope somebody can help!
Here's an example.
file1.json:
[
{
"customerId": "id1",
"networkId": "L_653021545945744804"
},
{
"customerId": "id2",
"networkId": "L_653021545955724805"
},
{
"customerId": "id3",
"networkId": "L_655051945958724557"
},
{
"customerId": "id4",
"networkId": "L_655567989968735408"
}
]
file2.json:
[
{
"name": "a",
"networkId": "L_653021545945744804"
},
{
"name": "b",
"networkId": "L_655051945958724557"
}
]
Wanted output:
[
{
"customerId": "id1",
"name": "a",
"networkId": "L_653021545945744804"
},
{
"customerId": "id3",
"name": "b",
"networkId": "L_655051945958724557"
}
]
This is a task for INDEX, JOIN and add:
jq '[JOIN(INDEX(.networkId); input[]; .networkId; add)]' file1.json file2.json
[
{
"name": "a",
"networkId": "L_653021545945744804",
"customerId": "id1"
},
{
"name": "b",
"networkId": "L_655051945958724557",
"customerId": "id3"
}
]
Demo
I'm working with a friend on a single page application (in React, but I believe that the framework doesn't really matter, the same question applies to Angular as well).
There is a database with 2 tables:
Feature
Car
Both tables are connected in the database with many-to-many relation.
We differ in how we should pass the data from the backend to the frontend (more precisely, CarManagementComponent that will let user work on cars/features (edit/update/delete etc)). We want to have ability to perform several actions before, actually, sending a request back to the backend to update the database so that the user has desktop application-like interface experience.
Please, keep in mind that there are more tables in the database but for the example's simplicity, we're talking here only about 2 of them.
1) My approach:
{
"Features": [
{
"Id": 0,
"Price": 3000,
"Name": "led lights",
"Color": "transparent",
"Brand": "Valeo",
"Guarantee": 12
},
{
"Id": 1,
"Price": 1000,
"Name": "air conditioning",
"Color": "",
"Brand": "Bosch",
"Guarantee": 12
},
{
"Id": 2,
"Price": 600,
"Name": "tinted windows",
"Color": "",
"Brand": "Bosch",
"Guarantee": 36
}
],
"Cars": [
{
"Id": 0,
"Name": "Ford Mustang GT",
"Weight": 2210,
"Features":[
{
"Id": 0, // id of many-to-many relations record
"FeatureId": 2
},
{
"Id": 1, // id of many-to-many relations record
"FeatureId": 1
}
]
},
{
"Id": 1,
"Name": "Volkswagen Arteon",
"Weight": 1650,
"Features":[
{
"Id": 2, // id of many-to-many relations record
"FeatureId": 2
}
]
}
]
}
2) My friend's approach:
{
"Cars": [
{
"Id": 0,
"Name": "Ford Mustang GT",
"Weight": 2210,
"Features": [
{
"Id": 1,
"Price": 1000,
"Name": "air conditioning",
"Color": "",
"Brand": "Bosch",
"Guarantee": 12
},
{
"Id": 2,
"Price": 600,
"Name": "tinted windows",
"Color": "",
"Brand": "Bosch",
"Guarantee": 36
}
]
},
{
"Id": 1,
"Name": "Volkswagen Arteon",
"Weight": 1650,
"Features": [
{
"Id": 2,
"Price": 600,
"Name": "tinted windows",
"Color": "",
"Brand": "Bosch",
"Guarantee": 36
}
]
}
]
}
I belive that the 1st approach is better because:
it weighs less (no data redundancy)
it would be easier to convert such data into object-oriented structure
eg. we are able to see all Feature records (in 2nd approach, we'd only see records that are being connected with Cars and another backend request would be needed)
eg. unlike the 2nd approach, we're able to obtain all the needed data in just 1 request (less problems with synchronization) and we could be saving modified data in a single request as well
My friend says 2nd approach is better because:
it'd be easier to achieve that using ORM (hibernate)
he's never seen 1st approach in his life (which could lead to a conclusion, that it's being done in a wrong way)
What do you think? Which solution is better? Maybe both of them in some areas? Maybe there's a 3rd solution we didn't think of yet?
I would say the approach I mostly like is yours for 2 main reasons:
Keeping in mind that data duplication is bad in an HTTP request, your approach is avoiding it.
You let the FeatureId inside the car object and it is enough to get the feature in an efficient performance O(N).
To make it even better, you could change your feature structure to this:
"Features": {
0: { // <- If the id is unique, you can use it as a key.
"Id": 0,
"Price": 3000,
"Name": "led lights",
"Color": "transparent",
"Brand": "Valeo",
"Guarantee": 12
},
1: {
"Id": 1,
"Price": 1000,
"Name": "air conditioning",
"Color": "",
"Brand": "Bosch",
"Guarantee": 12
},
2: {
"Id": 2,
"Price": 600,
"Name": "tinted windows",
"Color": "",
"Brand": "Bosch",
"Guarantee": 36
}
},
This way, you can get the Feature in O(1).
I'm want to make my json to csv so that i can upload it on google sheets and make it as json api. Whenever i have change data i will just change it on google sheets. But I'm having problems on converting my json file to csv because it changes the variables whenever i convert it. I'm using https://toolslick.com/csv-to-json-converter to convert my json file to csv.
What is the best way to convert json nested to csv ?
JSON
{
"options": [
{
"id": "1",
"value": "Jumbo",
"shortcut": "J",
"textColor": "#FFFFFF",
"backgroundColor": "#00000"
},
{
"id": "2",
"value": "Hot",
"shortcut": "D",
"textColor": "#FFFFFF",
"backgroundColor": "#FFFFFF"
}
],
"categories": [
{
"id": "1",
"order": 1,
"name": "First Category",
"active": true
},
{
"id": "2",
"order": 2,
"name": "Second Category",
"shortcut": "MT",
"active": true
}
],
"products": [
{
"id": "03c6787c-fc2a-4aa8-93a3-5e0f0f98cfb2",
"categoryId": "1",
"name": "First Product",
"shortcut": "First",
"options": [
{
"optionId": "1",
"price": 23
},
{
"optionId": "2",
"price": 45
}
],
"active": true
},
{
"id": "e8669cea-4c9c-431c-84ba-0b014f0f9bc2",
"categoryId": "2",
"name": "Second Product",
"shortcut": "Second",
"options": [
{
"optionId": "1",
"price": 11
},
{
"optionId": "2",
"price": 20
}
],
"active": true
}
],
"discounts": [
{
"id": "1",
"name": "S",
"type": 1,
"amount": 20,
"active": true
},
{
"id": "2",
"name": "P",
"type": 1,
"amount": 20,
"active": true
},
{
"id": "3",
"name": "G",
"type": 2,
"amount": 5,
"active": true
}
]
}
Using python, this can be easily done or almost done. Maybe this code will help you in some way to understand that.
import json,csv
data = []
with open('your_json_file_here.json') as file:
for line in file:
data.append(json.loads(line))
length = len(data)
with open('create_new_file.csv','w') as f:
writer = csv.writer(f)
writers = csv.DictWriter(f, fieldnames=['header1','header2'])
writers.writeheader()
for iter in range(length):
writer.writerow((data[iter]['specific_col_name1'],data[iter]['specific_col_name2']))
f.close()
A friend wrote a program in VBA, which generates a json data. I am trying to visualize that data via the pack-layout. We extracted the rules by what the json data is being created from the json data here: http://bl.ocks.org/mbostock/7607535
I went through the data many times myself, I just can't seem to find the problem why it is not being visualized. The browser console claims a problem in line 33 with the token "]" but in my eyes the parenthesis are right and I can't seem to find another mistake.
The visualization works properly with the data from where we extracted the rules.
The question now is, which mistake in the json file prevents the code from being visualized?
Would be amazing if somebody can see this, since we cannot see it. Thanks in advance!
The generated json data looks like this:
{
"name": "While",
"children": [
{"name": "While", "size": 27},
{
"name": "If",
"children": [
{"name": "If", "size": 22},
{
"name": "If",
"children": [
{"name": "If", "size": 3}
]
},
{
"name": "If",
"children": [
{"name": "If", "size": 3}
]
},
{
"name": "If",
"children": [
{"name": "If", "size": 3}
]
},
{
"name": "If",
"children": [
{"name": "If", "size": 3}
]
},
]
},
]
}
You have two commas(,) at the end of some arrays within that JSON of yours - that makes it invalid and prone to errors.
Just edit it and it will work. Use https://jsonformatter.curiousconcept.com/ to check.
The error lies with the script that generates it :)
Here's the fixed version of your JSON:
{
"name": "While",
"children": [{
"name": "While",
"size": 27
}, {
"name": "If",
"children": [{
"name": "If",
"size": 22
}, {
"name": "If",
"children": [{
"name": "If",
"size": 3
}]
}, {
"name": "If",
"children": [{
"name": "If",
"size": 3
}]
}, {
"name": "If",
"children": [{
"name": "If",
"size": 3
}]
}, {
"name": "If",
"children": [{
"name": "If",
"size": 3
}]
}]
}]
}
I'm having difficulty processing external JSON data to build/dispaly a tree. The issue is that nodes are not built from received JSON file. The code only looks as at the JSON data as one object and does not create leafs(other nodes)
When adding the same JSON data as array inside HTML file, the same code creates nodes and displays the tree correctly.
Here is an extract of the code in question:
<body onload="myFunction()">
<script type="text/javascript" >
function myFunction()
{
d3.json("/data/mydata.json", function (error, data) {
var root2 = [data];
root = root2[0];
........... other stuff comes here .....
var nodes = tree.nodes(root.reverse());
The browser debugger gives me the following:
nodes
[Array[1]0: Object
childern: Array[2]
name: "Max"
parent: "null"
proto: Object
depth: 0
length: 1
x: 230
y: 0
proto: Array[0]
JOSN file is at the bottom
I appreciate your feedback in this regard.
[{
"name": "Max",
"parent": "null",
"size": 4938,
"childern": [
{
"name": "David",
"parent": "Max",
"Childern": [
{
"name": "Craig",
"parent": "David",
"size": 3938
},
{
"name": "Robin",
"parent": "David",
"size": 3812
},
{
"name": "Anna",
"parent": "David",
"size": 743
}
]
},
{
"name": "Peter",
"parent": "Max",
"Childern": [
{
"name": "Jeff",
"parent": "Peter",
"size": 3534
},
{
"name": "Buffy",
"parent": "Peter",
"size": 5731
}
]
}
]
}
]
Your external JSON file appears to have the 'children' element with a capital 'C' for the lower tiers of data, but with lower case 'c' at the top tier.
Additionally they were misspelled as 'Childern' which may have been causing problems (you will want to check your JavaScript to see how you spelled it there and make it consistent).
I was able to do a quick experiment with an external JSON file and I can confirm that you will get a problem similar to what you describe. So use lower case characters (or at least consistent characters and spelling) for the word 'children' in your data.
Try with the data per below;
[{
"name": "Max",
"parent": "null",
"size": 4938,
"children": [
{
"name": "David",
"parent": "Max",
"children": [
{
"name": "Craig",
"parent": "David",
"size": 3938
},
{
"name": "Robin",
"parent": "David",
"size": 3812
},
{
"name": "Anna",
"parent": "David",
"size": 743
}
]
},
{
"name": "Peter",
"parent": "Max",
"children": [
{
"name": "Jeff",
"parent": "Peter",
"size": 3534
},
{
"name": "Buffy",
"parent": "Peter",
"size": 5731
}
]
}
]
}