Split an array of nested JSON objects using jq - json

I'm trying to split an array of nested json objects using jq. Could someone please help to resolve this issue and provide a better solution?
Sample input is:
{
"storeId": "1412",
"templateCheck": [
{
"rom": 37,
"updateDate": "2021-05-09 07:53:17",
"lhb": "2021-05-09 06:32:41",
"templateCode": "REGULAR",
"lId": "50-19-78-5C",
"sk": "830066",
"skUpdateDate": "2020-07-21 05:37:07",
"battery": 30,
"status": 1
},
{
"rom": 37,
"updateDate": "2021-05-09 07:54:02",
"lhb": "2021-05-09 06:32:41",
"templateCode": "REGULAR",
"lId": "50-1B-FE-6E",
"sk": "740541",
"skUpdateDate": "2021-03-22 05:59:00",
"battery": 30,
"status": 1
},
{
"rom": 37,
"updateDate": "2021-05-09 07:52:05",
"lhb": "2021-05-09 06:32:41",
"templateCode": "REGULAR",
"lId": "50-1C-22-6E",
"sk": "846760",
"skUpdateDate": "2021-05-08 03:34:22",
"battery": 29,
"status": 1
}
]
}
Expecting output as:
{
"storeId": "1412",
"templateCheck": [
"rom": 37,
"updateDate": "2021-05-09 07:53:17",
"lhb": "2021-05-09 06:32:41",
"templateCode": "REGULAR",
"lId": "50-19-78-5C",
"sk": "830066",
"skUpdateDate": "2020-07-21 05:37:07",
"battery": 30,
"status": 1
]
}
{
"storeId": "1412",
"templateCheck": [
"rom": 37,
"updateDate": "2021-05-09 07:54:02",
"lhb": "2021-05-09 06:32:41",
"templateCode": "REGULAR",
"lId": "50-1B-FE-6E",
"sku": "740541",
"skUpdateDate": "2021-03-22 05:59:00",
"battery": 30,
"status": 1
]
}
{
"storeId": "1412",
"templateCheck": [
"rom": 37,
"updateDate": "2021-05-09 07:52:05",
"lhb": "2021-05-09 06:32:41",
"templateCode": "REGULAR",
"lId": "50-1C-22-6E",
"sk": "846760",
"skUpdateDate": "2021-05-08 03:34:22",
"battery": 29,
"status": 1
]
}

Sure it's possible. Assuming there are no other top-level keys that you care about,
jq '.storeId as $storeId | .templateCheck[] | { $storeId, templateCheck: [ . ] }'
does just fine to reproduce your example output, or maybe a little more generically (but more confusingly):
jq '(. | del(.templateCheck)) as $o | .templateCheck[] | $o + { templateCheck: [.] }'

Related

openwathermap json array looping data

I am working with some JSON data, which produces an array for 7 days worth of weather data. I have created a loop foreach day:
$url="https://api.openweathermap.org/data/3.0/onecall?lat=51.5012751&lon=2.6800418&exclude=current,minutely,hourly,alerts&appid=apikey&units=metric";
$json=file_get_contents($url);
$data=json_decode($json, true);
foreach($data['daily'] as $day => $value) {
echo "Values for arrayID " . $day . " (".date("Y-m-d", $value['dt']).")"."<BR>";
echo "Max Temp : ".$value['temp']['max']."<BR>";
echo "Min Temp : ".$value['temp']['min']."<BR>";
echo "UVI: ".$value['uvi']."<BR>";
The above is working, but I cannot get the element for [weather] to work using:
echo "Description: ".$value['weather']['description']."<BR>";
An export of the JSON array is below, I have seen that on the "weather": [ there is an { after opening square bracket, so i am assuming this needs extra syntax to get into that array
{
"daily": [
{
"dt": 1646326800,
"sunrise": 1646306882,
"sunset": 1646347929,
"moonrise": 1646309880,
"moonset": 1646352120,
"moon_phase": 0.03,
"temp": {
"day": 281.63,
"min": 271.72,
"max": 282.21,
"night": 271.72,
"eve": 277.99,
"morn": 280.92
},
"feels_like": {
"day": 277.83,
"night": 264.72,
"eve": 273.35,
"morn": 277.66
},
"pressure": 1016,
"humidity": 55,
"dew_point": 273.12,
"wind_speed": 9.29,
"wind_deg": 3,
"wind_gust": 16.48,
"weather": [
{
"id": 500,
"main": "Rain",
"description": "light rain",
"icon": "10d"
}
],
"clouds": 49,
"pop": 0.25,
"rain": 0.11,
"uvi": 3.38
How would I call the values for anything in the weather array please?

Array in array: jq: error (at test.json:91): object ({"locations...) is not valid in a csv row

I want to convert json to csv.
I use tool "jq" for this
jq
Here json
{
"total": 2040,
"created_at": "2022-01-27T09:50:59+0200",
"project": "my project",
"issues": [
{
"key": "key_1",
"component": "my_component",
"textRange": {
"startLine": 35,
"endLine": 35,
"startOffset": 46,
"endOffset": 84
},
"flows": [],
"status": "OPEN",
"creationDate": "2022-01-24T06:42:58+0200",
"updateDate": "2022-01-24T06:42:58+0200",
"type": "BUG",
"scope": "MAIN"
},
{
"key": "key2",
"component": "my component 2",
"textRange": {
"startLine": 34,
"endLine": 34,
"startOffset": 3,
"endOffset": 52
},
"flows": [
{
"locations": [
{
"component": "some component",
"textRange": {
"startLine": 35,
"endLine": 35,
"startOffset": 3,
"endOffset": 50
},
"msg": "any message"
}
]
},
{
"locations": [
{
"component": "another component",
"textRange": {
"startLine": 36,
"endLine": 36,
"startOffset": 3,
"endOffset": 71
},
"msg": "message custom"
}
]
},
{
"locations": [
{
"component": "Alarm.java",
"textRange": {
"startLine": 37,
"endLine": 37,
"startOffset": 3,
"endOffset": 76
},
"msg": "message number 2"
}
]
},
{
"locations": [
{
"component": "Alarm.java",
"textRange": {
"startLine": 38,
"endLine": 38,
"startOffset": 3,
"endOffset": 50
},
"msg": "message number 3"
}
]
}
]
},
{
"key": "my_key3",
"component": "my component 3",
"textRange": {
"startLine": 548,
"endLine": 548,
"startOffset": 14,
"endOffset": 15
},
"flows": [],
"status": "OPEN",
"creationDate": "2022-01-21T17:16:06+0200",
"updateDate": "2022-01-21T17:16:06+0200",
"type": "CODE_SMELL",
"scope": "LOCAL"
}
]
}
And here convert to csv only some flelds:
jq -r '.issues[] | [.key ,.component, .textRange[], .status] | #csv' test.json
And here result:
"1","my_component",35,35,46,84,"OPEN"
"2","my component 2",34,34,3,52,
Nice.
Now I want to convert field (array) flows. I try this:
jq -r '.issues[] | [.key ,.component, .textRange[], .flows[], .status] | #csv' test.json
And here result:
"1","my_component",35,35,46,84,"OPEN"
jq: error (at test.json:91): object ({"locations...) is not valid in a csv row
How fix convert? Array (locations) in array (flows)
The result must be like this (first line are fields's names):
total,created_at,project,issues,key,component,textRange,startLine,endLine,startOffset,endOffset,status,creationDate,updateDate,type,scope,flows,locations,component,textRange,startLine,endLine,startOffset,endOffset,msg
2040,2022-01-27T09:50:59+0200,my project,,key_1,my_component,,35,35,46,84,OPEN,2022-01-24T06:42:58+0200,2022-01-24T06:42:58+0200,BUG,MAIN,,,,,,,,,
,,,,key2,my component 2,,34,34,3,52,,,,,,,,some component,,35,35,3,50,any message
,,,,,,,,,,,,,,,,,,another component,,36,36,3,71,message custom
,,,,,,,,,,,,,,,,,,Alarm.java,,37,37,3,76,message number 2
,,,,,,,,,,,,,,,,,,Alarm.java,,38,38,3,50,message number 3
,,,,my_key3,my component 3,,548,548,14,15,OPEN,2022-01-21T17:16:06+0200,2022-01-21T17:16:06+0200,CODE_SMELL,LOCAL,,,,,,,,,
It's important to show values not only issues but also fields's values: total,created_at,project
Do you want the objects in .flows[].locations[] to be appended as separate rows?
jq -r '.issues[]
| .key as $key | ., .flows[].locations[]
| [$key ,.component, .textRange[], .status]
| #csv
'
"1","my_component",35,35,46,84,"OPEN"
"2","my component 2",34,34,3,52,
"2","some component",35,35,3,50,
"2","another component",36,36,3,71,
"2","Alarm.java",37,37,3,76,
"2","Alarm.java",38,38,3,50,
Demo
Seems you need stringify, then you can use tostring such as
jq -r '.issues[]
| [.key ,.component, .textRange[], (.flows[].locations[]|tostring), .status]
| #csv'
Demo

Can't parse array in another array - jq: error (at test.issues.json:100): object ({"component...) is not valid in a csv row

File test.issues.json
{
"issues": [
{
"key": "key_1",
"component": "my_component",
"textRange": {
"startLine": 1,
"endLine": 11,
"startOffset": 111,
"endOffset": 1111
},
"flows": [],
"status": "OPEN",
"type": "BUG",
"scope": "MAIN"
},
{
"key": "key2",
"component": "my component 2",
"textRange": {
"startLine": 2,
"endLine": 22,
"startOffset": 222,
"endOffset": 2222
},
"flows": [
{
"locations": [
{
"component": "some component",
"textRange": {
"startLine": 35,
"endLine": 35,
"startOffset": 3,
"endOffset": 50
},
"msg": "any message"
}
]
},
{
"locations": [
{
"component": "another component",
"textRange": {
"startLine": 36,
"endLine": 36,
"startOffset": 3,
"endOffset": 71
},
"msg": "message custom"
}
]
},
{
"locations": [
{
"component": "Alarm.java",
"textRange": {
"startLine": 37,
"endLine": 37,
"startOffset": 3,
"endOffset": 76
},
"msg": "message number 2"
}
]
},
{
"locations": [
{
"component": "Alarm.java",
"textRange": {
"startLine": 38,
"endLine": 38,
"startOffset": 3,
"endOffset": 50
},
"msg": "message number 3"
}
]
}
]
},
{
"key": "my_key3",
"component": "my component 3",
"textRange": {
"startLine": 548,
"endLine": 548,
"startOffset": 14,
"endOffset": 15
},
"flows": [],
"status": "OPEN",
"type": "CODE_SMELL",
"scope": "LOCAL"
}
]
}
I need to convert to csv.
I try this:
jq -r '.issues[] | [.key,.component,.textRange[], (.flows[].locations[]), .status, .type, .scope] | #csv' test.issues.json
But I get error:
"key_1","my_component",1,11,111,1111,"OPEN","BUG","MAIN"
jq: error (at test.issues.json:100): object ({"component...) is not valid in a csv row
I need to get smt like this:
key,component,textRange,startLine,endLine,startOffset,endOffset,status,type,scope,flows,locations,component,textRange,startLine,endLine,startOffset,endOffset,msg
key_1,my_component,,1,11,111,1111,OPEN,BUG,MAIN,,,,,,,,,
key2,my component 2,,2,22,222,2222,,,,,,some component,,35,35,3,50,any message
,,,,,,,,,,,,another component,,36,36,3,71,message custom
,,,,,,,,,,,,Alarm.java,,37,37,3,76,message number 2
,,,,,,,,,,,Alarm.java,,38,38,3,50,message number 3
my_key3,my component 3,,548,548,14,15,OPEN,CODE_SMELL,LOCAL,,,,,,,,,
Try this for starters
jq -r '
.issues[]
| (.flows |= first), (.flows[1:][] | {flows:.})
| [
.key, .component, (
.textRange | null, .startLine, .endLine, .startOffset, .endOffset
), .status, .type, .scope, (
.flows.locations[]? // {} | null, null, .component, (
.textRange | null, .startLine, .endLine, .startOffset, .endOffset
), .msg
)
]
| join(",")
'
key_1,my_component,,1,11,111,1111,OPEN,BUG,MAIN,,,,,,,,,
key2,my component 2,,2,22,222,2222,,,,,,some component,,35,35,3,50,any message
,,,,,,,,,,,,another component,,36,36,3,71,message custom
,,,,,,,,,,,,Alarm.java,,37,37,3,76,message number 2
,,,,,,,,,,,,Alarm.java,,38,38,3,50,message number 3
my_key3,my component 3,,548,548,14,15,OPEN,CODE_SMELL,LOCAL,,,,,,,,,
Demo
Note: You may want to use #csv instead of join(",") but it'll wrap the strings in quotes.
Also, to add a header line, prepend the filters before the join/#csv with a simple array of strings.

jq: group objects by string

I have some json from eurostat, which looks like this:
{
"version": "2.0",
"label": "Principaux agrégats des administrations publiques, y compris recettes et dépenses",
"href": "http://ec.europa.eu/eurostat/wdds/rest/data/v2.1/json/fr/gov_10a_main?unit=PC_GDP&na_item=TE&sector=S13&time=2008&time=2009&time=2010&time=2011&time=2012&time=2013&time=2014&time=2015&time=2016&time=2017&geo=DE&geo=AT&geo=BE&geo=BG&geo=CY&geo=HR&geo=FI",
"source": "Eurostat",
"updated": "2018-10-26",
"status": {
"57": "b"
},
"extension": {
"datasetId": "gov_10a_main",
"lang": "FR",
"description": null,
"subTitle": null,
"status": {
"label": {
"b": "rupture de série"
}
}
},
"class": "dataset",
"value": {
"0": 49.9,
"1": 54.1,
"2": 52.8,
"3": 50.9,
"4": 51.2,
"5": 51.6,
"6": 52.4,
"7": 51.1,
"8": 50.3,
"9": 49.2,
"10": 50.3,
"11": 54.2,
"12": 53.3,
"13": 54.5,
"14": 55.9,
"15": 55.8,
"16": 55.3,
"17": 53.7,
"18": 53,
"19": 52.2,
"20": 37.1,
"21": 39.4,
"22": 36.2,
"23": 33.8,
"24": 34.5,
"25": 37.7,
"26": 43.1,
"27": 40.5,
"28": 35.1,
"29": 35.1,
"30": 38.4,
"31": 42.1,
"32": 42,
"33": 42.3,
"34": 41.9,
"35": 41.9,
"36": 48.8,
"37": 40.6,
"38": 38,
"39": 37.5,
"40": 43.6,
"41": 47.6,
"42": 47.3,
"43": 44.7,
"44": 44.3,
"45": 44.7,
"46": 44,
"47": 43.7,
"48": 43.9,
"49": 43.9,
"50": 48.3,
"51": 54.8,
"52": 54.8,
"53": 54.4,
"54": 56.2,
"55": 57.5,
"56": 58.1,
"57": 57.1,
"58": 55.9,
"59": 54,
"60": 45.3,
"61": 48.3,
"62": 48,
"63": 48.5,
"64": 47.8,
"65": 47.6,
"66": 48.1,
"67": 48.3,
"68": 46.9,
"69": 45
},
"dimension": {
"unit": {
"label": "unit",
"category": {
"index": {
"PC_GDP": 0
},
"label": {
"PC_GDP": "Pourcentage du produit intérieur brut (PIB)"
}
}
},
"sector": {
"label": "sector",
"category": {
"index": {
"S13": 0
},
"label": {
"S13": "Administrations publiques"
}
}
},
"na_item": {
"label": "na_item",
"category": {
"index": {
"TE": 0
},
"label": {
"TE": "Total des dépenses des administrations publiques"
}
}
},
"geo": {
"label": "geo",
"category": {
"index": {
"AT": 0,
"BE": 1,
"BG": 2,
"CY": 3,
"DE": 4,
"FI": 5,
"HR": 6
},
"label": {
"AT": "Autriche",
"BE": "Belgique",
"BG": "Bulgarie",
"CY": "Chypre",
"DE": "Allemagne (jusqu'en 1990, ancien territoire de la RFA)",
"FI": "Finlande",
"HR": "Croatie"
}
}
},
"time": {
"label": "time",
"category": {
"index": {
"2008": 0,
"2009": 1,
"2010": 2,
"2011": 3,
"2012": 4,
"2013": 5,
"2014": 6,
"2015": 7,
"2016": 8,
"2017": 9
},
"label": {
"2008": "2008",
"2009": "2009",
"2010": "2010",
"2011": "2011",
"2012": "2012",
"2013": "2013",
"2014": "2014",
"2015": "2015",
"2016": "2016",
"2017": "2017"
}
}
}
},
"id": [
"unit",
"sector",
"na_item",
"geo",
"time"
],
"size": [
1,
1,
1,
7,
10
]
}
I would like to produce a csv file.
First, I need to concatenate .status with .value by string (sorry for my poor json knowledge) --> "status":{"57": "b"} with "value":{"57": 57.1}.
Second, I need to produce the same table as the original one (downloaded from eurostat).
I try many jq commands, like:
.status,.value | to_entries
I'm far from finding a solution.
Any help? I think map or map_values/group_by command are needed, but I don't really understand these functions.
EDIT :
I download data from eurostat.
I use their web service here, where I can download data in json format.
I would like to reproduce in shell same table as original, with jq. In my exemple, it should look like :
GEO/TIME,2010,2011,2012,2013,2014,2015,2016,2017
Belgique,"53,3","54,5","55,9","55,8","55,3","53,7","53,0","52,2"
Bulgarie,"36,2","33,8","34,5","37,7","43,1","40,5","35,1","35,1"
"Allemagne (jusqu'en 1990, ancien territoire de la RFA)","47,3","44,7","44,3","44,7","44,0","43,7","43,9","43,9"
Croatie,"48,0","48,5","47,8","47,6","48,1","48,3","46,9","45,0"
Chypre,"42,0","42,3","41,9","41,9","48,8","40,6","38,0","37,5"
Finlande,"54,8","54,4","56,2","57,5","58,1","57,1","55,9","54,0"
But json contain metadata, and Finlande must have 57,1b value.
I hope it's more clear with this edit.
And many thanks for your help.
Your question doesn't indicate very precisely what output you want, but hopefully you'll be able to adapt the following:
.value as $dict
| .status
| to_entries
| map( [.key, .value, $dict[.key]] )
| .[]
| #csv
With your input, and invoking jq with the -r option, this produces:
"57","b",57.1

How to do ngFor loop on nested json object?

It seems to be simple thing to do, but somehow i did not get inner array element to do ngFor loop on Angular 2.
I have json array as below, and i need to iterate through available 'routes' in my response array which is nested element.
Now can any one let me know how can i get route which should be simple and i am trying like this.routes=respondeJson[0].routes Or this.routes=resonseJson[0]['routes'] but no luck. Please help considering my entry level here on Angular 2 or say working with JSON.
[
{
"routes": {
"0": {
"budget": 326,
"toCity": "United States",
"QuoteIds": [
1,
2
],
"options": 2
},
"1": {
"budget": 374,
"toCity": "Thailand",
"QuoteIds": [
3,
4
],
"options": 2
},
"2": {
"budget": 382,
"toCity": "Singapore",
"QuoteIds": [
5,
6
],
"options": 2
},
"3": {
"budget": 451,
"toCity": "Taiwan",
"QuoteIds": [
7
],
"options": 1
},
"5": {
"budget": 112,
"toCity": "Turkey",
"QuoteIds": [
8
],
"options": 1
},
"6": {
"budget": 314,
"toCity": "Saudi Arabia",
"QuoteIds": [
9
],
"options": 1
},
"8": {
"budget": 518,
"toCity": "Indonesia",
"QuoteIds": [
10
],
"options": 1
},
"10": {
"budget": 384,
"toCity": "Hong Kong",
"QuoteIds": [
11,
12
],
"options": 2
},
"11": {
"budget": 232,
"toCity": "Qatar",
"QuoteIds": [
13
],
"options": 1
},
"22": {
"budget": 254,
"toCity": "Algeria",
"QuoteIds": [
14
],
"options": 1
},
"23": {
"budget": 241,
"toCity": "Kuwait",
"QuoteIds": [
15
],
"options": 1
},
"24": {
"budget": 175,
"toCity": "Israel",
"QuoteIds": [
16,
17
],
"options": 2
},
"25": {
"budget": 266,
"toCity": "Oman",
"QuoteIds": [
18,
19
],
"options": 2
},
"28": {
"budget": 762,
"toCity": "Argentina",
"QuoteIds": [
20,
21
],
"options": 2
},
"30": {
"budget": 78,
"toCity": "Iceland",
"QuoteIds": [
22
],
"options": 1
},
"37": {
"budget": 327,
"toCity": "Canada",
"QuoteIds": [
23,
24
],
"options": 2
},
"39": {
"budget": 238,
"toCity": "Iran",
"QuoteIds": [
25
],
"options": 1
},
"40": {
"budget": 129,
"toCity": "Cyprus",
"QuoteIds": [
26
],
"options": 1
},
"41": {
"budget": 267,
"toCity": "United Arab Emirates",
"QuoteIds": [
27,
28
],
"options": 2
},
"42": {
"budget": 252,
"toCity": "Lebanon",
"QuoteIds": [
29
],
"options": 1
},
"43": {
"budget": 427,
"toCity": "Iraq",
"QuoteIds": [
30
],
"options": 1
},
"50": {
"budget": 142,
"toCity": "Montenegro",
"QuoteIds": [
31
],
"options": 1
},
"55": {
"budget": 418,
"toCity": "Cuba",
"QuoteIds": [
32
],
"options": 1
},
"56": {
"budget": 492,
"toCity": "China",
"QuoteIds": [
33,
34
],
"options": 2
},
"57": {
"budget": 444,
"toCity": "India",
"QuoteIds": [
35
],
"options": 1
},
"62": {
"budget": 477,
"toCity": "Japan",
"QuoteIds": [
36,
37
],
"options": 2
},
"63": {
"budget": 3377,
"toCity": "Costa Rica",
"QuoteIds": [
38
],
"options": 1
},
"65": {
"budget": 210,
"toCity": "Georgia",
"QuoteIds": [
39
],
"options": 1
},
"66": {
"budget": 391,
"toCity": "Sri Lanka",
"QuoteIds": [
40,
41
],
"options": 2
},
"67": {
"budget": 258,
"toCity": "Russia",
"QuoteIds": [
42
],
"options": 1
},
"68": {
"budget": 82,
"toCity": "Poland",
"QuoteIds": [
43
],
"options": 1
},
"71": {
"budget": 104,
"toCity": "Finland",
"QuoteIds": [
44
],
"options": 1
},
"72": {
"budget": 150,
"toCity": "Austria",
"QuoteIds": [
45,
46
],
"options": 2
},
"73": {
"budget": 68,
"toCity": "Spain",
"QuoteIds": [
47
],
"options": 1
},
"75": {
"budget": 106,
"toCity": "Portugal",
"QuoteIds": [
48
],
"options": 1
},
"77": {
"budget": 37,
"toCity": "Romania",
"QuoteIds": [
49
],
"options": 1
},
"78": {
"budget": 66,
"toCity": "Sweden",
"QuoteIds": [
50
],
"options": 1
},
"79": {
"budget": 77,
"toCity": "Denmark",
"QuoteIds": [
51
],
"options": 1
},
"86": {
"budget": 440,
"toCity": "South Africa",
"QuoteIds": [
52,
53
],
"options": 2
},
"87": {
"budget": 176,
"toCity": "Morocco",
"QuoteIds": [
54,
55
],
"options": 2
},
"88": {
"budget": 231,
"toCity": "Serbia",
"QuoteIds": [
56
],
"options": 1
},
"89": {
"budget": 279,
"toCity": "Bosnia and Herzegovina",
"QuoteIds": [
57
],
"options": 1
},
"90": {
"budget": 35,
"toCity": "Italy",
"QuoteIds": [
58
],
"options": 1
},
"92": {
"budget": 72,
"toCity": "Hungary",
"QuoteIds": [
59
],
"options": 1
},
"93": {
"budget": 155,
"toCity": "Croatia",
"QuoteIds": [
60
],
"options": 1
},
"94": {
"budget": 160,
"toCity": "Malta",
"QuoteIds": [
61,
62
],
"options": 2
},
"95": {
"budget": 163,
"toCity": "Greece",
"QuoteIds": [
63,
64
],
"options": 2
},
"96": {
"budget": 67,
"toCity": "Ireland",
"QuoteIds": [
65
],
"options": 1
},
"97": {
"budget": 60,
"toCity": "Netherlands",
"QuoteIds": [
66
],
"options": 1
},
"98": {
"budget": 68,
"toCity": "Norway",
"QuoteIds": [
67
],
"options": 1
},
"99": {
"budget": 71,
"toCity": "Latvia",
"QuoteIds": [
68
],
"options": 1
},
"100": {
"budget": 261,
"toCity": "Belgium",
"QuoteIds": [
69
],
"options": 1
},
"101": {
"budget": 60,
"toCity": "Bulgaria",
"QuoteIds": [
70
],
"options": 1
},
"102": {
"budget": 130,
"toCity": "Switzerland",
"QuoteIds": [
71
],
"options": 1
},
"103": {
"budget": 140,
"toCity": "Ukraine",
"QuoteIds": [
72
],
"options": 1
},
"104": {
"budget": 72,
"toCity": "France",
"QuoteIds": [
73
],
"options": 1
},
"105": {
"budget": 56,
"toCity": "Lithuania",
"QuoteIds": [
74
],
"options": 1
},
"106": {
"budget": 51,
"toCity": "United Kingdom",
"QuoteIds": [
75
],
"options": 1
},
"109": {
"budget": 116,
"toCity": "Czech Republic",
"QuoteIds": [
76
],
"options": 1
},
"110": {
"budget": 59,
"toCity": "Germany",
"QuoteIds": [
77
],
"options": 1
},
"113": {
"budget": 272,
"toCity": "Bahrain",
"QuoteIds": [
78,
79
],
"options": 2
},
"128": {
"budget": 634,
"toCity": "New Zealand",
"QuoteIds": [
80
],
"options": 1
},
"147": {
"budget": 610,
"toCity": "Australia",
"QuoteIds": [
81
],
"options": 1
},
"167": {
"budget": 187,
"toCity": "Moldova",
"QuoteIds": [
82
],
"options": 1
},
"168": {
"budget": 87,
"toCity": "Slovakia",
"QuoteIds": [
83
],
"options": 1
},
"175": {
"budget": 119,
"toCity": "Gibraltar",
"QuoteIds": [
84
],
"options": 1
},
"178": {
"budget": 545,
"toCity": "Laos",
"QuoteIds": [
85
],
"options": 1
}
},
"misc": {
"QuoteDateTime": "2017-04-21T13:56:00",
"MinPrice": 326,
"Direct": false,
"QuoteId": 1,
"skyLink": "http://partners.api.skyscanner.net/apiservices/referral/v1.0/AU/AUD/en-US/anywhere/amd/2017-05-18/2017-05-22?apiKey=wc161029621991497683276175998396"
},
"price": [
326,
392,
374,
434,
382,
470,
451,
112,
314,
518,
384,
553,
232,
254,
241,
175,
199,
422,
266,
1125,
762,
78,
403,
327,
238,
129,
290,
267,
252,
427,
142,
418,
492,
641,
444,
477,
937,
3377,
210,
713,
391,
258,
82,
104,
160,
150,
68,
106,
37,
66,
77,
624,
440,
176,
287,
231,
279,
35,
72,
155,
210,
160,
163,
200,
67,
60,
68,
71,
261,
60,
130,
140,
72,
56,
51,
116,
59,
374,
272,
634,
610,
187,
87,
119,
545
],
"outbound": {
"DepartureDate": "2017-05-18T00:00:00",
"Carrier": "WOW air",
"DestinationId": "EWR",
"OriginId": "LGW"
},
"airline": [
"WOW air",
"Lufthansa",
"Gulf Air",
"EVA Air",
"Turkish Airlines",
"British Airways",
"Turkish Airlines",
"Pegasus Airlines",
"Pegasus Airlines",
"Singapore Airlines",
"Air India",
"British Airways",
"Pegasus Airlines",
"Vueling Airlines",
"Pegasus Airlines",
"Pegasus Airlines",
"Monarch",
"Oman Air",
"Turkish Airlines",
"British Airways",
"United",
"WOW air",
"Air Transat",
"WestJet",
"Pegasus Airlines",
"Cobalt",
"Royal Brunei ",
"Turkish Airlines",
"Pegasus Airlines",
"Pegasus Airlines",
"easyJet",
"KLM",
"Austrian Airlines",
"British Airways",
"Air India",
"Alitalia",
"British Airways",
"Avianca",
"Pegasus Airlines",
"SriLankan Airlines",
"Qatar Airways",
"Pegasus Airlines",
"Wizz Air",
"Norwegian",
"eurowings",
"eurowings",
"Vueling Airlines",
"Monarch",
"Blue Air",
"Norwegian",
"Norwegian",
"Virgin Atlantic",
"Ethiopian Airlines",
"Iberia",
"Thomson Airways",
"Wizz Air",
"Pegasus Airlines",
"Flybe",
"Wizz Air",
"Monarch",
"Air Malta",
"easyJet",
"Pegasus Airlines",
"Aegean Airlines",
"Aer Lingus",
"Vueling Airlines",
"Norwegian",
"Wizz Air",
"eurowings",
"Wizz Air",
"SWISS",
"Ukraine International",
"Vueling Airlines",
"Wizz Air",
"Flybe",
"Flybe",
"Flybe",
"British Airways",
"Pegasus Airlines",
"Qatar Airways",
"Royal Brunei ",
"Wizz Air",
"Wizz Air",
"Monarch",
"Singapore Airlines"
],
"inbound": {
"DepartureDate": "2017-05-22T00:00:00",
"Carrier": "Norwegian",
"OriginId": "JFK",
"DestinationId": "LGW"
}
},
{
"misc": {
"QuoteDateTime": "2017-04-19T06:33:00",
"MinPrice": 392,
"Direct": true,
"QuoteId": 2,
"skyLink": "http://partners.api.skyscanner.net/apiservices/referral/v1.0/AU/AUD/en-US/anywhere/amd/2017-05-18/2017-05-22?apiKey=wc161029621991497683276175998396"
},
"outbound": {
"DepartureDate": "2017-05-18T00:00:00",
"Carrier": "Lufthansa",
"DestinationId": "EWR",
"OriginId": "LHR"
},
"inbound": {
"DepartureDate": "2017-05-22T00:00:00",
"Carrier": "Austrian Airlines",
"OriginId": "EWR",
"DestinationId": "LHR"
}
}
]
You have gotten some good answers here, but all are manipulating your response and changing the build of it, instead of treating it as is. There is some other data in your response and want to retain the data, so here's a solution using Pipe instead.
You seem to have two objects in your array, but only one contains routes. Will this always be the case? If not, you might want to iterate the response and show all routes (if exists) for all objects, so I'd iterate the array first, and then iterate the routes:
<!-- Iterate array -->
<div *ngFor="let obj of jsonData">
<!-- iterate routes for each object using pipe -->
<div *ngFor="let route of obj.routes | keys">
{{route.toCity}}
</div>
</div>
And then the keys pipe:
#Pipe({ name: 'keys', pure: false })
export class KeysPipe implements PipeTransform {
transform(value: any, args?: any[]): any[] {
// check if "routes" exists
if(value) {
// create instance vars to store keys and final output
let keyArr: any[] = Object.keys(value),
dataArr = [];
// loop through the object,
// pushing values to the return array
keyArr.forEach((key: any) => {
dataArr.push(value[key]);
});
// return the resulting array
return dataArr;
}
}
}
This way you have not manipulated your response, and you have access to all other data that is coming with the response.
Demo
You will want to convert your object to an iterable array.
this.http.get('data.json')
.subscribe((res) => {
let keyArr: any[] = Object.keys(res.json()[0].routes);
keyArr.forEach((key: any) => {
this.data.push(res.json()[0].routes[key]);
});
});
Here's a Plunker
Your routes should be defined as an json array however you have done a workaround to make routes as an array where you have explicitly given indexs (0, 1 ..) which is not correct.
Solution is attached below:
JSON should look like below:
[
{
"routes": [{
"budget": 326,
"toCity": "United States",
"QuoteIds": [
1,
2
],
"options": 2
}, {
"budget": 374,
"toCity": "Thailand",
"QuoteIds": [
3,
4
],
"options": 2
}
]
}
]
Way to assign it
this.routes=resonseJson[0]['routes']
Way to iterate it in html
<div *ngFor="let route of routes">
{{route.toCity}}
</div>
You have to iterate over object keys and not array elements. Hence either use
Object.keys(responseJson[0].routes)
that will return ["0","1", ..."178"]. Then use below:
in component .ts :
routes : any = responseJson[0].routes;
in template :
<div *ngFor="let key of Object.keys(routes)">
{{routes.key.budget}}
</div>