How to get Array Json in Ionic2 - angularjs

I want to get the translatedText value. But in the console.log keep show undefined. w
this.getPosts('Hello', 'ja');
getPosts(text, category) {
this.translateService.getPosts(text, category).subscribe(response => {
console.log(response.data.translations.translateText);
this.items = response.data.translations;
console.log(items.translateText)
}
Here's the Json Format.
{
"data": {
"translations": [
{
"translatedText": "Hallo Welt",
"detectedSourceLanguage": "en"
}
]
}
}

Related

Parsing a Multidimensional JSON Array Into Columns in Google Sheets

I am working on a script in Google Sheets to import JSON data via API and need to be able to parse the data into columns in a spreadsheet. The API is returning a multi-dimensional array with a key that is a randomly generated ID.
{
"log":{
"5DUA3NAuET1O7TDhIvmC":{
"log":4440,
"title":"Trade money outgoing",
"timestamp":1649773788,
"category":"Trades",
"data":{
"user":282048,
"trade_id":"[view]",
"money":39562944
},
"params":{
}
}
}
}
How can I
Parse the data into a multidimensional object
Access the values like title, data.cost, etc to be able to import this data to a sheet?
Try
var result = []
function test(){
jsonString = `{
"log":{
"5DUA3NAuET1O7TDhIvmC":{
"log":4440,
"title":"Trade money outgoing",
"timestamp":1649773788,
"category":"Trades",
"data":{
"user":282048,
"trade_id":"[view]",
"money":39562944
},
"params":{
}
}
}
}`
let json= JSON.parse(jsonString.replace(/(\r\n|\n|\r|\t| )/gm,""))
getMyData(json)
SpreadsheetApp.getActiveSpreadsheet().getSheetByName('test').getRange(1,1,result.length,result[0].length).setValues(result)
}
function getMyData(obj) {
for (let p in obj) {
if (obj[p]!=null){
if (typeof obj[p] != 'object' && typeof obj[p] != 'function'){
result.push([p, obj[p]]);
}
if (typeof obj[p] == 'object') {
result.push([p, '']);
getMyData( obj[p] );
}
}
}
}
var response = {
"log": {
"5DUA3NAuET1O7TDhIvmC": {
"log": 4440,
"title": "Trade money outgoing",
"timestamp": 1649773788,
"category": "Trades",
"data": {
"user": 282048,
"trade_id": "[view]",
"money": 39562944
},
"params": { }
}
}
}
var key = Object.keys(response.log)[0];
console.log(key);
var obj = response.log[key];
console.log(obj.title);
console.log(obj.data.user);

array with objects containing array

Sorry guys if my way of asking the question in the title is not correct.
I am working on a project on react js and I am getting the data like this
[
{
"count": [
{
"1": 16
},
{
"1": 149
}
],
"day": "2019-08-27"
}
]
now this is my first time I am dealing with this kind of data and I really have no idea how can I show it like this I am really sorry guys I literally can't even show what I have tried because it does not seem relevant
[
{
count: 165
day:"2019-08-27"
}
}
Assuming the data you're getting is under a variable called data you could use reduce:
The below makes the assumption the count is always an array of objects with just 1 key called '1'.
const newData = data.map(datum => {
datum.count = datum.count.reduce((count, item) => {
return count + item['1']
}, 0)
return datum
})
You can try something like this:
let arr = [
// item
{
count: [
{
"1": 16
},
{
"1": 149
}
],
day: "2019-08-27"
}
];
arr.map(item => {
Object.keys(item).map(key => {
console.log(item[key])
// if item[key] is iterable
if(Array.isArray(item[key])) {
item[key].map(val => {
console.log(item)
})
} else {
console.log(item[key])
}
});
});
The concept is that for Objects you do a Object.keys().something and for an array you do a arr.map(item => ...)

How to update a multiple objects in an array with jq

Using jq I would like to conditionally update the objects in the "folders" array.
For each object which contains "cand_name":"folderA" a new attribute "files" which has the value of compare.files should be added
{
"tmp":{
"folders":[
{
"ref_name":"folderB",
"cand_name":"folderA"
},
{
"ref_name":"folderC",
"cand_name":"folderA"
},
{
"ref_name":"folderC",
"cand_name":"folderE"
}
],
"compare":{
"files":[
{
"candidate":"Z3S1"
}
]
}
}
}
For the above input the expected output should be
{
"tmp":{
"folders":[
{
"ref_name":"folderB",
"cand_name":"folderA"
"files":[
{
"candidate":"Z3S1"
}
]
},
{
"ref_name":"folderC",
"cand_name":"folderA"
"files":[
{
"candidate":"Z3S1"
}
]
},
{
"ref_name":"folderC",
"cand_name":"folderE"
}
],
"compare":{
"files":[
{
"candidate":"Z3S1"
}
]
}
}
}
The key to a simple solution is |=. Using the expected output as a guide to the intended requirements, one could write:
.tmp.compare.files as $f
| .tmp.folders
|= map( if .cand_name=="folderA"
then .files = $f else . end)

Cannot get data from a js array with Node js

I have a js object array like this.
[
{
name:"Japanilainen ravintola Koto",
rating:3.9,
photo:[
{
height:2160,
html_attributions:[
"Hannes Junnila"
],
photo_reference:"CoQBdwAAAMDlivT0nOnYg8jC1txZ3RbfBR59XvKN0WphDbRVUXaUTQclzzaIaXJ8-p7s3x_aG67AUsM_HLNML6pzGl3v_wV2D-eudH_3wy2cB1ROrRgGcGyf4lRuNpE3WwXYbYZu6EK8oEPiJ5B17Lybj-eVbYM2EgVVBgOrUJhsblY1mfxWEhAZ4oHCFakH-hgkbksfGa2uGhQe4aUeOrS2isAir01KUwQ7N3Ce2Q",
width:2269
}
]
},
{
name:"Kin Sushi Helsinki",
rating:4.2,
photo:[
{
height:2988,
html_attributions:[
"Stephan Winter"
],
photo_reference:"CoQBdwAAAN4iMumSbQjtRnJIH1AKRdbSfnI02WGh11r1xaVnZl1ohebKp6zpAS4mmJFqTagrIqUJ39kzulVI0sz2UzzfaVdsAFc5f80PnOCzSLqL5gnpsqv90dVJIqUWD3Bcc9TgYPPs3oGwyekkOsmjQ59o9yqdoF5GzrpaKkojhMNLxpfzEhBKpRkA2CzINpUzAAe3e90TGhQ_KbYCmtJYLfVGIu1kZkzQIAwE4A",
width:5312
}
]
}
]
I get this array above by doing this for each.
response.results.forEach((entry)=>{
var restaurantName = {
"name" : entry.name,
"rating" : entry.rating,
"photo_reference" : entry.photos
}
arr.push(restaurantName);
});
res.send(arr);
And I send the array to my browser so I can see it.
What I am trying to do is to get photo_reference from the entry.photos
I tried entry.photos[0].photo_reference and many more ways and in all of them I am getting a cannot read properly, and now I am not sure how to get that information out.
I edited some of the variable names to make it easier to simulate here, but just map the objects in the photo arrays to their references, and you'll get an array of photo references.
const data = [
{
name:"Japanilainen ravintola Koto",
rating:3.9,
photo:[
{
height:2160,
html_attributions:[
'Hannes Junnila'
],
photo_reference:"CoQBdwAAAMDlivT0nOnYg8jC1txZ3RbfBR59XvKN0WphDbRVUXaUTQclzzaIaXJ8-p7s3x_aG67AUsM_HLNML6pzGl3v_wV2D-eudH_3wy2cB1ROrRgGcGyf4lRuNpE3WwXYbYZu6EK8oEPiJ5B17Lybj-eVbYM2EgVVBgOrUJhsblY1mfxWEhAZ4oHCFakH-hgkbksfGa2uGhQe4aUeOrS2isAir01KUwQ7N3Ce2Q",
width:2269
}
]
},
{
name:"Kin Sushi Helsinki",
rating:4.2,
photo:[
{
height:2988,
html_attributions:[
'Stephan Winter'
],
photo_reference:"CoQBdwAAAN4iMumSbQjtRnJIH1AKRdbSfnI02WGh11r1xaVnZl1ohebKp6zpAS4mmJFqTagrIqUJ39kzulVI0sz2UzzfaVdsAFc5f80PnOCzSLqL5gnpsqv90dVJIqUWD3Bcc9TgYPPs3oGwyekkOsmjQ59o9yqdoF5GzrpaKkojhMNLxpfzEhBKpRkA2CzINpUzAAe3e90TGhQ_KbYCmtJYLfVGIu1kZkzQIAwE4A",
width:5312
}
]
}
]
const arr = []
data.forEach((entry)=>{
var restaurantName = {
"name" : entry.name,
"rating" : entry.rating,
"photo_reference" : entry.photo.map(x => x.photo_reference)
}
arr.push(restaurantName);
});
console.log(arr);
entry.photoes is not defined in your response.results object array.. did you mean to access it as entry.photo (inside your foreach function) ?

logstash filter to modify array field

I am looking for a logstash filter that can modify array fields.
For example, I would like a modifier that can turn this JSON document
{
arrayField: [
{
subfield: {
subsubfield: "value1"
}
},
{
subfield: {
subsubfield: "value2"
}
}
]
}
Into this JSON document
{
arrayField: [
{
subfield: "value1"
},
{
subfield: "value2"
}
]
}
I have tried the following input
input {
mutate {
replace => ["[arrayField][subfield]", "%{[arrayField][subField][subsubField]}"]
}
}
but the input just rewrites the array field instead of operating on each element of the array. How do you set up a modifier to operate on each element of an array?
Thanks Alain Collins for pointing out the ruby filter. The below input did the trick.
input {
ruby {
code => "
event['arrayField'].each{|subdoc| subdoc['subfield'] = subdoc['subfield']['subsubfield']}
"
}
}

Resources