remove rows with duplicate attribute from json data
Date : March 29 2020, 07:55 AM
it should still fix some issue I am a bit New Here ..I am trying to Learn Jquery .. In Jquery .I want only those rows which have unique Title ..(i.e.) rows 1,2 and 3 only ... , Try this $.ajax({
//parameters
success: function (response) {
result= response.d;
var d= {};
//array with unique objects
var u= [];
$.each(result, function(i, el) {
if (!d[el.Title]) {
d[el.Title] = true;
u.push(el);
}
});
}
});
|
How to generate JSON-API data attribute vs results attribute in Django Rest Framework JSON API?
Date : March 29 2020, 07:55 AM
this one helps. The problem in my case was that I had a viewset derived class in the view like this: class QuestionViewSet(viewsets.ReadOnlyModelViewSet):
"""
API endpoint that allows questions and answers to be read.
"""
resource_name = 'questions'
queryset = Question.objects.all()
serializer_class = QuestionSerializer
renderers = renderers.JSONRenderer
parsers = parsers.JSONParser
class QuestionViewSet(viewsets.ReadOnlyModelViewSet):
"""
API endpoint that allows questions and answers to be read.
"""
resource_name = 'questions'
queryset = Question.objects.all()
serializer_class = QuestionSerializer
renderer_classes = (renderers.JSONRenderer,)
parser_classes = (parsers.JSONParser,)
{"data":{"type":"questions","id":"1","attributes":{"created":"2016-02-10T04:28:50.742000Z","updated":null,"deleted":null,"uuid":"eddfc27d-2677-49e5-bd37-92fecea340bd","text":"Are you dizzy?"},"relationships":{"answers":{"data":[{"type":"Answer","id":"1"},{"type":"Answer","id":"2"},{"type":"Answer","id":"3"}]},"members":{"data":[{"type":"Member","id":"1"},{"type":"Member","id":"2"},{"type":"Member","id":"3"},{"type":"Member","id":"4"}],"meta":{"count":4}}}}}
|
AWK: How to remove data where column count is not the same as header count and data has commas within double quotes
Tag : bash , By : Matt Watson
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further I need to remove lines of data where the column count is not the same as the header column count. The following works except in the case where a field contains data, which has commas in it within double quotes. Any ideas how to fix please? , With GNU awk for FPAT: $ awk -v FPAT='[^,]*|"[^"]+"' 'NR==1{nf=NF} NF==nf' file
timeStamp,elapsed,label,responseCode,responseMessage,dataType,success,bytes,grpThreads,allThreads,Latency,Hostname,IdleTime,Connect
1459774220811,2018,Fizz_Homepage_2,403," transaction : 1,failing samples : 0",,false,12928,2,2,0,HOST1,5008,0
1459774225103,3485,Fizz_Launch_1,200," transaction : 1,failing samples : 0",,true,1138878,2,2,0,HOST1,5022,0
1459774227844,1653,Fizz_Launch_1,200," transaction : 1,failing samples : 0",,true,18792,2,2,0,HOST1,5012,0
$ cat tst.awk
BEGIN { FS=RS; OFS="," }
{
head = ""
tail = $0
while( (tail!="") && match(tail,/[^,]*|"[^"]+"/) ) {
head = head (head==""?"":FS) substr(tail,RSTART,RLENGTH)
tail = substr(tail,RSTART+RLENGTH+1)
}
$0 = head tail
}
NR==1 { nf=NF }
NF==nf { $1=$1; print }
$
$ awk -f tst.awk file
timeStamp,elapsed,label,responseCode,responseMessage,dataType,success,bytes,grpThreads,allThreads,Latency,Hostname,IdleTime,Connect
1459774220811,2018,Fizz_Homepage_2,403," transaction : 1,failing samples : 0",,false,12928,2,2,0,HOST1,5008,0
1459774225103,3485,Fizz_Launch_1,200," transaction : 1,failing samples : 0",,true,1138878,2,2,0,HOST1,5022,0
1459774227844,1653,Fizz_Launch_1,200," transaction : 1,failing samples : 0",,true,18792,2,2,0,HOST1,5012,0
$ cat tst.awk
{
csv2flds()
for (i=0;i<=NF;i++) {
print "NR="NR, "NF="NF, "$"i"="$i
}
print "-----"
}
function csv2flds( head, tail, ofs) {
ofs=OFS; OFS=","; FS=RS
head = ""
tail = $0
while( (tail!="") && match(tail,/[^,]*|"[^"]+"/) ) {
head = head (head==""?"":FS) substr(tail,RSTART,RLENGTH)
tail = substr(tail,RSTART+RLENGTH+1)
}
$0 = head tail # calculates NF and splits into fields using FS="\n"
$1 = $1 # converts "xFSy" into "xOFSy" so "x\ny" becomes "x,y"
FS=OFS; OFS=ofs
}
$ awk -f tst.awk file | head -32
NR=1 NF=14 $0=timeStamp,elapsed,label,responseCode,responseMessage,dataType,success,bytes,grpThreads,allThreads,Latency,Hostname,IdleTime,Connect
NR=1 NF=14 $1=timeStamp
NR=1 NF=14 $2=elapsed
NR=1 NF=14 $3=label
NR=1 NF=14 $4=responseCode
NR=1 NF=14 $5=responseMessage
NR=1 NF=14 $6=dataType
NR=1 NF=14 $7=success
NR=1 NF=14 $8=bytes
NR=1 NF=14 $9=grpThreads
NR=1 NF=14 $10=allThreads
NR=1 NF=14 $11=Latency
NR=1 NF=14 $12=Hostname
NR=1 NF=14 $13=IdleTime
NR=1 NF=14 $14=Connect
-----
NR=2 NF=14 $0=1459774220811,2018,Fizz_Homepage_2,403," transaction : 1,failing samples : 0",,false,12928,2,2,0,HOST1,5008,0
NR=2 NF=14 $1=1459774220811
NR=2 NF=14 $2=2018
NR=2 NF=14 $3=Fizz_Homepage_2
NR=2 NF=14 $4=403
NR=2 NF=14 $5=" transaction : 1,failing samples : 0"
NR=2 NF=14 $6=
NR=2 NF=14 $7=false
NR=2 NF=14 $8=12928
NR=2 NF=14 $9=2
NR=2 NF=14 $10=2
NR=2 NF=14 $11=0
NR=2 NF=14 $12=HOST1
NR=2 NF=14 $13=5008
NR=2 NF=14 $14=0
-----
|
Adding Depth or Parent Count attribute to nested JSON data
Date : March 29 2020, 07:55 AM
it fixes the issue where data is returned from a service, and iteratorDepth is defined as a private variable: Call: this.locations = this.recursivelyTraverseLocations(data,1)
this.recursivelyTraverseLocations(locations, iteratorDepth) {
for (let location of locations) {
if (location.parent_location_id > 0) {
location.level = iteratorDepth
} else {
location.level = 1
}
if (location.SUBLOCATIONS) {
this. this.recursivelyTraverseLocations(location.SUBLOCATIONS, iteratorDepth+1);
}
}
return locations;
}
|
Acquiring the JSON value of a data attribute, and search for matching Json key -> data attribute in a specific DOM el
Date : March 29 2020, 07:55 AM
should help you out What is currently stopping your script in its tracks is the selector inside querySelectorAll(). It should be '[data-trigger]', not ['data-trigger']. <a href="#" data-trigger='{"rem": "albatros"}'>Remove</a>
JSON.parse(elem.getAttribute('data-trigger')) // where elem is the DOM element above
document.querySelectorAll('[data-'+foundKey+'="'+foundVal+'"]').forEach(function(el, i){
console.log(el, i);
})
<x data-rem="albatros"></x>
document.querySelectorAll('[data-trigger]').forEach(function (trigger, index) {
const triggerProp = JSON.parse(trigger.getAttribute('data-trigger')),
foundKey = Object.keys(triggerProp)[0],
foundVal = triggerProp[Object.keys(triggerProp)[0]],
targets = document.querySelectorAll('[data-'+foundKey+']').forEach(function(target, index) {
console.log(target, 'key:'+foundKey, 'val:'+target.getAttribute('data-'+foundKey));
});
})
<a href="#" data-trigger='{"rem": "albatros"}'>Remove</a>
<div data-container>
<p>lorem ipsum<p>
<p data-rem="test"></p>
</div>
|