enter image description herebelow code snippet for getting json data from iron ajax call . i am able to get json object value in mapResponse.
i want to get the value of results array which is in json data and want to pass this results object value in another polymer component as a input attribute
code for loading data from iron ajax
<iron-ajax
id="originalData"
auto
url="{{originalDataURL}}"
handle-as="json"
last-response="{{originalData}}" on-response="mapResponse">
</iron-ajax>
Json file
{
"tags": [
{
"name": "test",
"results": [
{
"groups": [
{
"name": "type",
"type": "number"
}
],
"values": [
[
946890000000,
99.93584833,
3
],
[
946846800000,
99.94809842,
3
],
[
946803600000,
99.96034846,
3
],
[
946760400000,
99.97259848,
3
],
[
946717200000,
99.98484848,
3
]
],
"attributes": {}
}
],
"stats": {
"rawCount": 5
}
}
]
}
<script>
Polymer({
is: 'test-view',
properties: {
results: {
type: Array
},
mapResponse: function (data) {
var dummy = data.detail.response;
console.log("resposne is ",dummy);
results = dummy.results;
console.log("array is ",results);
},
i tried to get results array object value as above in console logs but getting undefined. here i am able to get the value of dummy where i am getting full json object (tags) i want to get only results array object from this (tags )object .
can anyone please suggest me how can i get only results array value ??
Thanks in advance.
It should be results = dummy.tags[0].results;
instead of results = dummy.tags[0].results;.
Or you can do results = dummy.tags.map(x=>x.results); if tags have multiple arrays of results.
Related
I need to get filter data but it works when its only list of data not list in list.
There is some lists of object name starting with number1 with list but need the data inside number and save it in array. Using smart table vuejs so if i get json data it will work since i did similar things.
This is my code
let result = [];
let response = await this.$axios.post("api");
for (item in response.data.list){
result.push(response.data.list[item][0];
}
{
"list": {
"number1": [
{},
],
"number2": [
{}
],
"number3": [
{}
],
etc...
}
<v-table
//it is within thead and
:data="result"
:filters= "filters">
<input v-model="filters.name.value"
<v-table/>
data:function(){
return {
result: [];
filters: {
... //other fields same as below
name: { value: "", keys: ["name"] },
...
},
}
}
I have the following JSON object data stored in a JSON file, which will be passed as a when performing an API call.
I want to replace "it-goes-here" with below {} block.
replaced-data:
{
"parenturl":"xxx.com",
"username":"userId",
"password":"xxx!",
"id":"id",
"url":"xxx.com",
"xxx":"xxx"
}
test.json
{
"details": it-goes-here,
"dbs": [
{
"schemas": [
{
"schemaName": "schemaName",
"tables": [
{
"tableName": "tableName",
"type": "table",
"columns": [
{
"name": "name",
"gender": "F",
"canDonate": true,
"database": "database"
},
etc.,
]
}
]
}
],
}
I have tried the code below, but it keeps giving me SyntaxError: Unexpected token
in JSON at position 28. I'm new to nodeJS, what am I doing here? What else can I try?
let data = await fs.readFileSync('./test/test.json', 'utf8').toString();
data = await JSON.parse(JSON.stringify(data).replace('it-goes-here', 'replaced-data'));
Try read and parse the test.json to JSON Object and run data.details = replacedData to replace the details object to replacedData object.
I'm working with data to create a search tool where the user can search for the name of an individual involved in a legal case; the tool will output a table with the individual's name and role (with some other details about the case) in the following format:
Case name
Person
Role
Status
Case 1
John Smith
Lawyer (Claimant)
Concluded
Case 2
John Smith
Expert (Respondent)
Suspended
I'm making AJAX calls to the API endpoint to fetch this data, and an example of a returned object is as follows:
{
"id": 3,
"institution": [
"Institution X"
],
"party": [
"Test Investments X.Y.",
"Czech Republic"
],
"chairperson": [
"Jane Doe",
"Olive Yew"
],
"arbitratorClaimant": [
"Hugo First"
],
"arbitratorRespondent": [
"Greg Arias"
],
"secretary": [
"Simon Sais"
],
"lawFirmClaimant": [
"Frank & Stein"
],
"lawyerClaimant": [
"John Smith"
],
"lawFirmRespondent": [
"Jen & Tile"
],
"lawyerRespondent": [
"Anita Bath"
],
"expertFirmClaimant": [
"Fran & Tick"
],
"expertClaimant": [
"Laura Biding"
],
"expertFirmRespondent": [
"Rose & Bush"
],
"expertRespondent": [
"Peter Owt"
],
"name": "Case 1",
"caseType": "investment",
"caseStatus": "concluded",
"awardDate": "2021-07-04",
"note": "",
"chairAppointment": "party agreement",
"claimantAppointment": "co-arbitrators",
"respondentAppointment": "other"
}
In order to display the role of the individual that was searched for by the user in the table, I need a way to search through the returned object to find which key the name is associated with. For instance, in Case 1, John Smith was a lawyer for the Claimant, so I need a function that returns "lawyerClaimant" when searching through the object.
Here is the beginning of the function to request data from the API and display it in the table:
function requestCases() {
var requestCase = document.getElementById('case-search-input').value;
var requestConnections = document.getElementById('connections-search-input').value;
var xhr = new XMLHttpRequest();
xhr.open('GET', `api/case-list/?first-person=${requestCase}&second-person=${requestConnections}`, true);
xhr.onload = function() {
if(this.status == 200) {
if (requestCase=='' && requestConnections=='') {
return null;
}
else if (requestCase==requestConnections) {
return null;
}
else if (individualsArray.includes(requestCase) && requestConnections=='') {
var cases = JSON.parse(this.responseText);
var caseTable = '<table class="styled-table"><thead><tr><th>Case Name</th><th>Person (Natural)</th><th>Role</th><th>Status</th></tr></thead><tbody>';
if (cases.length >= 1) {
for (var i in cases.slice(0,3)) {
console.log(cases[i]);
}
I've searched through other posts and found a few variations of functions like this:
function getKeyByValue(object, value) {
return Object.keys(object).find(key => object[key].includes(value));
}
getKeyByValue(cases[i], requestCase);
However, when I attempt to use anything like this, I get a ".includes is not a function" TypeError. Could anyone help guide me in the right direction?
Thank you!
The problem is that some of the properties of the object aren't arrays, e.g. "id": 3. You'll get that error when it tries to use .includes() there. So first check that the value is an array:
function getKeyByValue(object, value) {
return Object.keys(object).find(key => Array.isArray(object[key]) && object[key].includes(value));
}
Can't fetch data from table I've just created in rethinkDB.
I've create a new table in retinkDB - items.
And fill it with data:
r.db('test').table('items').insert([
{name: 'qqq'},
{name: 'www'},
{name: 'eee'}
])
BUT .getList() never returns table's data:
client.record.getList('items') // '.getEntries()' always return []
I don't really understand why .getList('items') didn't return data from items table.
I assume that this is because entries structure: when you are create entry via deepstream, entry's structure be like:
[
...
{
"_d": {
"id": "qqq"
"name": 'qqq'
},
"_v": 0,
"ds_id": "qqq"
}
...
]
But mine structure is just:
[
{
"id": 'qqq'
"name": 'qqq'
}
]
My question is: How to create table with data in rethinkDB (via script) that would we worked with deepstream?
deepstream is designed to manage data for you. Simply by saying
var bmw = ds.record.getRecord( 'cars/bmw' )
deepstream will create a table called cars and store an entry with a primary key of bmw in it. You wouldn't be expected to create the table yourself.
I've find a solition.
First of all table should be created with proper primaryKey === "ds_id":
r.db('test')
.tableCreate('items',
{
primaryKey: 'ds_id'
})
And after that you have to insert data with deepstream-like structure:
r.db('test').table('items').insert([
{
"_d": {
"id": r.uuid(),
"name": "qqq"
},
"_v": 0,
"ds_id": r.uuid()
}
])
P.S. to make _d.id equal to ds_id use this:
r.db('test').table('items').forEach(function(c) {
return r.db('test').table('items').get(c('ds_id')).update(function(row) {
return {_d: {id: row('ds_id')}};
});
})
This is stupid but I don't know how to do it in more elegant way.
I have an JSON array like this
var filter_value_data = [{"Status":[{"name":"Open","id":"1"},{"name":"Pending","id":"2"},{"name":"Resolved","id":"3"},{"name":"Closed","id":"4"},{"name":"Evaluation","id":"5"}]},{"Payment Status":[{"name":"Paid","id":"10"},{"name":"UnPaid","id":"11"},{"name":"Part Paid","id":"12"}]},{"Priority":[{"name":"Low","id":"6"},{"name":"Medium","id":"7"},{"name":"High","id":"8"},{"name":"Urgent","id":"9"}]}]
I have tried filter_value_data["Status"] which is obviously wrong. How do I get the JSON elements for Status using the names like Status,Payment Status?
filter_value_data is an array (having []), so use filter_value_data[0].Status to get the first element-object with property "Status".
It is always good to format your code in order to see the hierarchy of the structures:
var filter_value_data = [
{
"Status": [
{
"name": "Open",
"id": "1"
}, {
"name": "Pending",
"id": "2"
}, ...
]
}, {
"Payment Status": [
{
"name": "Paid",
"id": "10"
}, ...
]
}, {
"Priority": [
{
"name": "Low",
"id": "6"
}, ...
]
}
];
With your current JSON you can't get the elements with the name alone.
You can get Status with filter_value_data[0]['Status'] and Payment status with filter_value_data[1]['Payment Status'].
This is because the keys are in seperate objects in the array.
In order to get them with filter_value_data['Status'] you need to change your JSON to
var filter_value_data = {
"Status":[
{"name":"Open","id":"1"},
{"name":"Pending","id":"2"},
{"name":"Resolved","id":"3"},
{"name":"Closed","id":"4"},
{"name":"Evaluation","id":"5"}
],
"Payment Status":[
{"name":"Paid","id":"10"},
{"name":"UnPaid","id":"11"},
{"name":"Part Paid","id":"12"}
],
"Priority":[
{"name":"Low","id":"6"},
{"name":"Medium","id":"7"},
{"name":"High","id":"8"},
{"name":"Urgent","id":"9"}
]
};
I wrote this on my phone so it's not as well-formatted as usual. I'll change it ASAP.
With your current JSON, created a result which might be helpful for you.
JS:
$.each(filter_value_data,function(ind,val){
var sta = val.Status; // Status Object get displayed
for(var i=0;i<sta.length;i++){
var idVal= sta[i].id;
var nameVal = sta[i].name;
Statusarray.push(idVal,nameVal);
console.log(Statusarray);
}
})
FiddleDemo
You can use below code, it will return status object
filter_value_data[0]['Status']
filter_value_data[0]['Payment Status']
to get Single value you use :
filter_value_data[0]['Status'][0]['name']