I'm getting json error in my first reactjs work. It is very important for me and work which I am a candidate. How can I fix this? Thank u very much.
related class:
constructor() {
super();
this.state = {
data:[],
}
}
componentDidMount(){
fetch('../JsonFiles/routes.json').
then(response => response.json()).
then(findresponse => this.setState ({ data: findresponse.data }));
}
render() {
return (
<div>
fasfasfa
{
this.state.data.map((dynamicData, key) =>
<div>
<span> {dynamicData.routes.origin.cityName} </span>
<span> {dynamicData.routes.destinations[{}].cityName} </span>
</div>
)
}
</div>
);
}
}
json file:
{"routes": [
{
"origin": {
"name": "Aalborg Airport",
"country": "DK",
"cityName": "Aalborg",
"cityCode": "AAL"
},
"destinations": [
{
"name": "Bornholm Rønne",
"country": "DK",
"cityName": "Bornholm",
"cityCode": "RNN"
}
]
},
{
"origin": {
"name": "Barca",
"country": "ES",
"cityName": "Barcelona",
"cityCode": "BCN"
},
"destinations": [
{
"name": "Borispol",
"country": "UA",
"cityName": "Kyiv",
"cityCode": "KBP"
}
]
},
{
"origin": {
"name": "Billund",
"country": "DK",
"cityName": "Billund",
"cityCode": "BLL"
},
"destinations": [
{
"name": "Bornholm Rønne",
"country": "DK",
"cityName": "Bornholm",
"cityCode": "RNN"
},
{
"name": "København Kastrup",
"country": "DK",
"cityName": "Copenhagen",
"cityCode": "CPH"
},
{
"name": "Stavanger Sola",
"country": "NO",
"cityName": "Stavanger",
"cityCode": "SVG"
}
]
},
{
"origin": {
"name": "Bornholm Rønne",
"country": "DK",
"cityName": "Bornholm",
"cityCode": "RNN"
},
"destinations": [
{
"name": "Aalborg Airport",
"country": "DK",
"cityName": "Aalborg",
"cityCode": "AAL"
},
{
"name": "Billund",
"country": "DK",
"cityName": "Billund",
"cityCode": "BLL"
},
{
"name": "København Kastrup",
"country": "DK",
"cityName": "Copenhagen",
"cityCode": "CPH"
},
{
"name": "Midtjyllands Lufthavn (Karup)",
"country": "DK",
"cityName": "Midtjyllands Lufthavn (Karup)",
"cityCode": "KRP"
},
{
"name": "Stavanger Sola",
"country": "NO",
"cityName": "Stavanger",
"cityCode": "SVG"
}
]
},
{
"origin": {
"name": "Charles de Gaulle",
"country": "FR",
"cityName": "Charles de Gaulle",
"cityCode": "CDG"
},
"destinations": [
{
"name": "Billund",
"country": "DK",
"cityName": "Billund",
"cityCode": "BLL"
}
]
},
{
"origin": {
"name": "København Kastrup",
"country": "DK",
"cityName": "Copenhagen",
"cityCode": "CPH"
},
"destinations": [
{
"name": "Billund",
"country": "DK",
"cityName": "Billund",
"cityCode": "BLL"
},
{
"name": "Bornholm Rønne",
"country": "DK",
"cityName": "Bornholm",
"cityCode": "RNN"
},
{
"name": "Midtjyllands Lufthavn (Karup)",
"country": "DK",
"cityName": "Midtjyllands Lufthavn (Karup)",
"cityCode": "KRP"
}
]
},
{
"origin": {
"name": "Esbjerg",
"country": "DK",
"cityName": "Esbjerg",
"cityCode": "EBJ"
},
"destinations": [
{
"name": "Stavanger Sola",
"country": "NO",
"cityName": "Stavanger",
"cityCode": "SVG"
}
]
},
{
"origin": {
"name": "Borispol",
"country": "UA",
"cityName": "Kyiv",
"cityCode": "KBP"
},
"destinations": [
{
"name": "Barca",
"country": "ES",
"cityName": "Barcelona",
"cityCode": "BCN"
}
]
},
{
"origin": {
"name": "Midtjyllands Lufthavn (Karup)",
"country": "DK",
"cityName": "Midtjyllands Lufthavn (Karup)",
"cityCode": "KRP"
},
"destinations": [
{
"name": "Bornholm Rønne",
"country": "DK",
"cityName": "Bornholm",
"cityCode": "RNN"
},
{
"name": "København Kastrup",
"country": "DK",
"cityName": "Copenhagen",
"cityCode": "CPH"
}
]
},
{
"origin": {
"name": "Oslo Gardermoen",
"country": "NO",
"cityName": "Oslo",
"cityCode": "OSL"
},
"destinations": [
{
"name": "Stord lufthavn, Sørstokken",
"country": "NO",
"cityName": "Stord",
"cityCode": "SRP"
},
{
"name": "Ørland",
"country": "NO",
"cityName": "Ørland",
"cityCode": "OLA"
}
]
},
{
"origin": {
"name": "Rhodos",
"country": "GR",
"cityName": "Rhodos",
"cityCode": "RHO"
},
"destinations": [
{
"name": "Billund",
"country": "DK",
"cityName": "Billund",
"cityCode": "BLL"
}
]
},
{
"origin": {
"name": "Stavanger Sola",
"country": "NO",
"cityName": "Stavanger",
"cityCode": "SVG"
},
"destinations": [
{
"name": "Billund",
"country": "DK",
"cityName": "Billund",
"cityCode": "BLL"
},
{
"name": "Bornholm Rønne",
"country": "DK",
"cityName": "Bornholm",
"cityCode": "RNN"
},
{
"name": "Esbjerg",
"country": "DK",
"cityName": "Esbjerg",
"cityCode": "EBJ"
}
]
},
{
"origin": {
"name": "Stord lufthavn, Sørstokken",
"country": "NO",
"cityName": "Stord",
"cityCode": "SRP"
},
"destinations": [
{
"name": "Oslo Gardermoen",
"country": "NO",
"cityName": "Oslo",
"cityCode": "OSL"
}
]
},
{
"origin": {
"name": "Ørland",
"country": "NO",
"cityName": "Ørland",
"cityCode": "OLA"
},
"destinations": [
{
"name": "Oslo Gardermoen",
"country": "NO",
"cityName": "Oslo",
"cityCode": "OSL"
}
]
}
]}
AND I AM GETTING THIS ERROR:
I AM GETTING THIS ERROR
1
Unhandled Rejection (SyntaxError): Unexpected token < in JSON at position 0
(anonymous function)
D:/development/js-projects/material-tutorial/src/Components/TrialForm/FlightSearch.js:13
10 |
11 | componentDidMount(){
12 | fetch('../JsonFiles/routes.json').
13 | then(response => response.json()).
14 | then(findresponse => this.setState ({ data: findresponse.data }));
15 |
16 |
You can import JSON file.
import * as jsonFile from '../JsonFiles/routes.json';
Define it on state in constructor function:
constructor() {
super();
this.state = {
data: jsonFile
}
}
es2015 version:
import jsonFile from '../JsonFiles/routes.json';
Now you have all json data available in "jsonFile" and you can use it any where.
Related
Original JSON message:
[
{
"correlationId": "12345",
"payloadFormat": "Money",
"payload": {
"stateName": "TX",
"location": "south",
"name": "Dallas",
"pop": "2M"
}
},
{
"correlationId": "ed1e3",
"payloadFormat": "Cash",
"payload": {
"stateName": "CA",
"location": "west",
"name": "LosAngeles",
"pop": "4M"
}
}
]
Output should be in below format:
[
{
"correlationId": "12345",
"payloadFormat": "Money",
"payload": {
"California": "TX",
"MontGomery": "south",
"City": "Dallas",
"ID": "2M"
}
},
{
"correlationId": "ed1e3",
"payloadFormat": "Cash",
"payload": {
"California": "CA",
"MontGomery": "west",
"City": "LosAngeles",
"ID": "4M"
}
}
]
Check this spec,
[
{
"operation": "shift",
"spec": {
"*": {
"correlationId": "[&1].correlationId",
"payloadFormat": "[&1].payloadFormat",
"payload": {
"stateName": "[&2].payload.California",
"location": "[&2].payload.MontGomery",
"name": "[&2].payload.City",
"pop": "[&2].payload.ID"
}
}
}
}
]
I am trying to search for flights between two places in Sabre using BargainFindermax API. The API provides me with a list of all flights. However, there is no direct relation to which of the flights are linked.
For example, I searched for flights between BLR > LCY. The response I got included flights for AMS > LCY, BLR > DEL, BLR > DEL, DEL > AMS, AMS > LCY.
From this, I can construct the following route BLR > DEL > AMS > LCY. But there are two flights for BLR > DEL and AMS > LCY.
How can I identify which of these flights are related?
Here is the complete JSON response,
{
"groupedItineraryResponse": {
"version": "5.2.0",
"messages": [
{
"severity": "Info",
"type": "WORKERTHREAD",
"code": "TRANSACTIONID",
"text": "2074398531853061390"
},
{
"severity": "Info",
"type": "SERVER",
"code": "ASE032LPSCIL9UM.IDM.SGDCCERT.SABRE.COM",
"text": "27036"
},
{
"severity": "Info",
"type": "DRE",
"code": "RULEID",
"text": "17203"
},
{
"severity": "Info",
"type": "DEFAULT",
"code": "RULEID",
"text": "25238"
}
],
"statistics": {
"itineraryCount": 2
},
"scheduleDescs": [
{
"id": 1,
"frequency": "*MTWTFS",
"stopCount": 0,
"eTicketable": true,
"totalMilesFlown": 257,
"departure": {
"airport": "AMS",
"city": "AMS",
"country": "NL",
"time": "08:45:00+02:00"
},
"arrival": {
"airport": "LCY",
"city": "LON",
"country": "GB",
"time": "08:50:00+01:00"
},
"carrier": {
"marketing": "KL",
"marketingFlightNumber": 985,
"operating": "KL",
"operatingFlightNumber": 985,
"codeShared": "/KLM CITYHOPPER",
"equipment": {
"code": "E90",
"typeForFirstLeg": "N",
"typeForLastLeg": "N"
}
}
},
{
"id": 2,
"trafficRestriction": "G",
"frequency": "SMTWTFS",
"stopCount": 0,
"eTicketable": true,
"totalMilesFlown": 1058,
"departure": {
"airport": "BLR",
"city": "BLR",
"country": "IN",
"time": "19:50:00+05:30"
},
"arrival": {
"airport": "DEL",
"city": "DEL",
"country": "IN",
"time": "22:50:00+05:30",
"terminal": "3"
},
"carrier": {
"marketing": "KL",
"marketingFlightNumber": 3798,
"operating": "9W",
"operatingFlightNumber": 812,
"disclosure": "9W",
"equipment": {
"code": "73H",
"typeForFirstLeg": "N",
"typeForLastLeg": "N"
}
}
},
{
"id": 3,
"trafficRestriction": "G",
"frequency": "SMTWTFS",
"stopCount": 0,
"eTicketable": true,
"totalMilesFlown": 1058,
"departure": {
"airport": "BLR",
"city": "BLR",
"country": "IN",
"time": "18:55:00+05:30"
},
"arrival": {
"airport": "DEL",
"city": "DEL",
"country": "IN",
"time": "22:00:00+05:30",
"terminal": "3"
},
"carrier": {
"marketing": "KL",
"marketingFlightNumber": 3703,
"operating": "9W",
"operatingFlightNumber": 833,
"disclosure": "9W",
"equipment": {
"code": "738",
"typeForFirstLeg": "N",
"typeForLastLeg": "N"
}
}
},
{
"id": 4,
"frequency": "*M**T**",
"stopCount": 0,
"eTicketable": true,
"totalMilesFlown": 3961,
"departure": {
"airport": "DEL",
"city": "DEL",
"country": "IN",
"time": "02:10:00+05:30",
"terminal": "3"
},
"arrival": {
"airport": "AMS",
"city": "AMS",
"country": "NL",
"time": "07:40:00+02:00"
},
"carrier": {
"marketing": "KL",
"marketingFlightNumber": 3817,
"operating": "9W",
"operatingFlightNumber": 234,
"disclosure": "9W",
"equipment": {
"code": "333",
"typeForFirstLeg": "W",
"typeForLastLeg": "W"
}
}
},
{
"id": 5,
"frequency": "*MTWTFS",
"stopCount": 0,
"eTicketable": true,
"totalMilesFlown": 257,
"departure": {
"airport": "AMS",
"city": "AMS",
"country": "NL",
"time": "09:25:00+02:00"
},
"arrival": {
"airport": "LCY",
"city": "LON",
"country": "GB",
"time": "09:30:00+01:00"
},
"carrier": {
"marketing": "KL",
"marketingFlightNumber": 987,
"operating": "KL",
"operatingFlightNumber": 987,
"codeShared": "/KLM CITYHOPPER",
"equipment": {
"code": "E90",
"typeForFirstLeg": "N",
"typeForLastLeg": "N"
}
}
}
],
"taxDescs": [
{
"id": 1,
"code": "YRI",
"amount": 160,
"currency": "USD",
"description": "SERVICE FEE - CARRIER-IMPOSED MISC",
"publishedAmount": 160,
"publishedCurrency": "USD",
"station": "DEL"
},
{
"id": 2,
"code": "YQI",
"amount": 13.3,
"currency": "USD",
"description": "SERVICE FEE - INSURANCE",
"publishedAmount": 925,
"publishedCurrency": "INR",
"station": "BLR"
},
{
"id": 3,
"code": "WO",
"amount": 3.8,
"currency": "USD",
"description": "PASSENGER SERVICE FEE",
"publishedAmount": 3.8,
"publishedCurrency": "USD",
"station": "BLR",
"country": "IN"
},
{
"id": 4,
"code": "K38",
"amount": 11.5,
"currency": "USD",
"description": "GOODS AND SERVICE TAX INTERIM DOMESTIC AND INTERNATIONAL",
"publishedAmount": 799,
"publishedCurrency": "INR",
"station": "BLR",
"country": "IN"
},
{
"id": 5,
"code": "CJ",
"amount": 7.4,
"currency": "USD",
"description": "SECURITY SERVICE CHARGE",
"publishedAmount": 6.5,
"publishedCurrency": "EUR",
"station": "AMS",
"country": "NL"
},
{
"id": 6,
"code": "RN",
"amount": 7.3,
"currency": "USD",
"description": "PASSENGER SERVICE CHARGE",
"publishedAmount": 6.4,
"publishedCurrency": "EUR",
"station": "AMS",
"country": "NL"
},
{
"id": 7,
"code": "IN",
"amount": 5.2,
"currency": "USD",
"description": "USER DEVELOPMENT FEE DEPARTURES",
"publishedAmount": 362,
"publishedCurrency": "INR",
"station": "BLR",
"country": "IN"
}
],
"taxSummaryDescs": [
{
"id": 1,
"code": "YRI",
"amount": 160,
"currency": "USD",
"description": "SERVICE FEE - CARRIER-IMPOSED MISC",
"publishedAmount": 160,
"publishedCurrency": "USD",
"station": "DEL"
},
{
"id": 2,
"code": "YQI",
"amount": 13.3,
"currency": "USD",
"description": "SERVICE FEE - INSURANCE",
"publishedAmount": 925,
"publishedCurrency": "INR",
"station": "BLR"
},
{
"id": 3,
"code": "WO",
"amount": 3.8,
"currency": "USD",
"description": "PASSENGER SERVICE FEE",
"publishedAmount": 3.8,
"publishedCurrency": "USD",
"station": "BLR",
"country": "IN"
},
{
"id": 4,
"code": "K38",
"amount": 11.5,
"currency": "USD",
"description": "GOODS AND SERVICE TAX INTERIM DOMESTIC AND INTERNATIONAL",
"publishedAmount": 0.05,
"publishedCurrency": "INR",
"station": "BLR",
"country": "IN"
},
{
"id": 5,
"code": "CJ",
"amount": 7.4,
"currency": "USD",
"description": "SECURITY SERVICE CHARGE",
"publishedAmount": 6.5,
"publishedCurrency": "EUR",
"station": "AMS",
"country": "NL"
},
{
"id": 6,
"code": "RN",
"amount": 7.3,
"currency": "USD",
"description": "PASSENGER SERVICE CHARGE",
"publishedAmount": 6.4,
"publishedCurrency": "EUR",
"station": "AMS",
"country": "NL"
},
{
"id": 7,
"code": "IN",
"amount": 5.2,
"currency": "USD",
"description": "USER DEVELOPMENT FEE DEPARTURES",
"publishedAmount": 362,
"publishedCurrency": "INR",
"station": "BLR",
"country": "IN"
}
],
"fareComponentDescs": [
{
"id": 1,
"governingCarrier": "KL",
"fareAmount": 24.65,
"fareCurrency": "INR",
"fareBasisCode": "XPLWIA",
"farePassengerType": "ADT",
"publishedFareAmount": 1717,
"oneWayFare": true,
"directionality": "FROM",
"direction": "EH",
"notValidBefore": "2019-07-21",
"notValidAfter": "2019-07-21",
"applicablePricingCategories": "4 5 6 7 8 10 12 16 18",
"vendorCode": "ATP",
"fareTypeBitmap": "00",
"fareType": "PRU",
"fareTariff": "4",
"fareRule": "INPL",
"segments": [
{
"segment": {
"surcharges": [
{
"amount": 28.71,
"currency": "NUC",
"description": "MISCELLANEOUS/OTHER",
"type": "Q"
}
]
}
},
{
"segment": {}
},
{
"segment": {}
}
]
}
],
"validatingCarrierDescs": [
{
"id": 1,
"settlementMethod": "ARC",
"newVcxProcess": true,
"default": {
"code": "DL"
}
}
],
"baggageAllowanceDescs": [
{
"id": 1,
"pieceCount": 1
}
],
"legDescs": [
{
"id": 1,
"schedules": [
{
"ref": 2
},
{
"ref": 4,
"departureDateAdjustment": 1
},
{
"ref": 5,
"departureDateAdjustment": 1
}
]
},
{
"id": 2,
"schedules": [
{
"ref": 3
},
{
"ref": 4,
"departureDateAdjustment": 1
},
{
"ref": 1,
"departureDateAdjustment": 1
}
]
}
],
"itineraryGroups": [
{
"groupDescription": {
"legDescriptions": [
{
"departureDate": "2019-07-21",
"departureLocation": "BLR",
"arrivalLocation": "LCY"
}
]
},
"itineraries": [
{
"id": 1,
"pricingSource": "ADVJR1",
"legs": [
{
"ref": 2
}
],
"pricingInformation": [
{
"pricingSubsource": "MIP",
"fare": {
"validatingCarrierCode": "DL",
"vita": true,
"eTicketable": true,
"lastTicketDate": "2019-07-05",
"governingCarriers": "KL",
"passengerInfoList": [
{
"passengerInfo": {
"passengerType": "ADT",
"passengerNumber": 1,
"nonRefundable": true,
"fareComponents": [
{
"ref": 1,
"segments": [
{
"segment": {
"bookingCode": "X",
"cabinCode": "Y",
"mealCode": "D",
"seatsAvailable": 9
}
},
{
"segment": {
"bookingCode": "X",
"cabinCode": "Y",
"mealCode": "RB",
"seatsAvailable": 9
}
},
{
"segment": {
"bookingCode": "L",
"cabinCode": "Y",
"mealCode": "M",
"seatsAvailable": 9,
"availabilityBreak": true
}
}
]
}
],
"taxes": [
{
"ref": 2
},
{
"ref": 1
},
{
"ref": 7
},
{
"ref": 4
},
{
"ref": 3
},
{
"ref": 5
},
{
"ref": 6
}
],
"taxSummaries": [
{
"ref": 2
},
{
"ref": 1
},
{
"ref": 3
},
{
"ref": 4
},
{
"ref": 7
},
{
"ref": 5
},
{
"ref": 6
}
],
"currencyConversion": {
"from": "INR",
"to": "USD",
"exchangeRateUsed": 0.01442679
},
"fareMessages": [
{
"type": "N",
"code": "0",
"carrier": "KL",
"info": "FARE RESTRICTIONS APPLY/CARRIER RESTRICTIONS APPLY"
},
{
"type": "W",
"code": "0",
"info": "VALIDATING CARRIER - DL PER GSA AGREEMENT WITH KL"
}
],
"passengerTotalFare": {
"totalFare": 264.5,
"totalTaxAmount": 208.5,
"currency": "USD",
"baseFareAmount": 3875,
"baseFareCurrency": "INR",
"equivalentAmount": 56,
"equivalentCurrency": "USD",
"constructionAmount": 55.59,
"constructionCurrency": "NUC",
"commissionPercentage": 0,
"commissionAmount": 0,
"exchangeRateOne": 69.651352
},
"baggageInformation": [
{
"provisionType": "A",
"airlineCode": "KL",
"segments": [
{
"id": 0
},
{
"id": 1
},
{
"id": 2
}
],
"allowance": {
"ref": 1
}
}
]
}
}
],
"totalFare": {
"totalPrice": 264.5,
"totalTaxAmount": 208.5,
"currency": "USD",
"baseFareAmount": 3875,
"baseFareCurrency": "INR",
"constructionAmount": 55.59,
"constructionCurrency": "NUC",
"equivalentAmount": 56,
"equivalentCurrency": "USD"
},
"validatingCarriers": [
{
"ref": 1
}
]
}
}
],
"diversitySwapper": {
"weighedPrice": 409.855
}
},
{
"id": 2,
"pricingSource": "ADVJR1",
"legs": [
{
"ref": 1
}
],
"pricingInformation": [
{
"pricingSubsource": "MIP",
"fare": {
"validatingCarrierCode": "DL",
"vita": true,
"eTicketable": true,
"lastTicketDate": "2019-07-05",
"governingCarriers": "KL",
"passengerInfoList": [
{
"passengerInfo": {
"passengerType": "ADT",
"passengerNumber": 1,
"nonRefundable": true,
"fareComponents": [
{
"ref": 1,
"segments": [
{
"segment": {
"bookingCode": "X",
"cabinCode": "Y",
"mealCode": "D",
"seatsAvailable": 9
}
},
{
"segment": {
"bookingCode": "X",
"cabinCode": "Y",
"mealCode": "RB",
"seatsAvailable": 9
}
},
{
"segment": {
"bookingCode": "L",
"cabinCode": "Y",
"mealCode": "M",
"seatsAvailable": 9,
"availabilityBreak": true
}
}
]
}
],
"taxes": [
{
"ref": 2
},
{
"ref": 1
},
{
"ref": 7
},
{
"ref": 4
},
{
"ref": 3
},
{
"ref": 5
},
{
"ref": 6
}
],
"taxSummaries": [
{
"ref": 2
},
{
"ref": 1
},
{
"ref": 3
},
{
"ref": 4
},
{
"ref": 7
},
{
"ref": 5
},
{
"ref": 6
}
],
"currencyConversion": {
"from": "INR",
"to": "USD",
"exchangeRateUsed": 0.01442679
},
"fareMessages": [
{
"type": "N",
"code": "0",
"carrier": "KL",
"info": "FARE RESTRICTIONS APPLY/CARRIER RESTRICTIONS APPLY"
},
{
"type": "W",
"code": "0",
"info": "VALIDATING CARRIER - DL PER GSA AGREEMENT WITH KL"
}
],
"passengerTotalFare": {
"totalFare": 264.5,
"totalTaxAmount": 208.5,
"currency": "USD",
"baseFareAmount": 3875,
"baseFareCurrency": "INR",
"equivalentAmount": 56,
"equivalentCurrency": "USD",
"constructionAmount": 55.59,
"constructionCurrency": "NUC",
"commissionPercentage": 0,
"commissionAmount": 0,
"exchangeRateOne": 69.651352
},
"baggageInformation": [
{
"provisionType": "A",
"airlineCode": "KL",
"segments": [
{
"id": 0
},
{
"id": 1
},
{
"id": 2
}
],
"allowance": {
"ref": 1
}
}
]
}
}
],
"totalFare": {
"totalPrice": 264.5,
"totalTaxAmount": 208.5,
"currency": "USD",
"baseFareAmount": 3875,
"baseFareCurrency": "INR",
"constructionAmount": 55.59,
"constructionCurrency": "NUC",
"equivalentAmount": 56,
"equivalentCurrency": "USD"
},
"validatingCarriers": [
{
"ref": 1
}
]
}
}
],
"diversitySwapper": {
"weighedPrice": 367.631
}
}
]
}
]
}
}
You have to use legDescs list to connect your segments, and use those "ref" values.
For example:
{
"id": 2,
"schedules": [
{
"ref": 3
},
{
"departureDateAdjustment": 1,
"ref": 4
},
{
"departureDateAdjustment": 1,
"ref": 1
}
]
}
means, your flight with ID = 2 contains 3 segments, with id 3 - 4 and 1. When you search numbered segments in "scheduleDescs" object, you can see routes are:
BLR - DEL
DEL - AMS
AMS - LCY
and as you can see you got the BLR - LCY flight.
I need to convert a json(got after multiple calls and then merged it) into a specific structure.
here is my input json payload
[{
"shops": [{
"shop": {
"code": "AU5",
"streetName": "a",
"city": "a",
"district": "a",
"state": "a",
"postalCode": "a",
"country": "a"
}
}, {
"shop": {
"code": "b",
"streetName": "b",
"city": "b",
"district": "b",
"state": "b",
"postalCode": "b",
"country": "b"
}
}]
},
[
[{
"salesOffice": {
"shop": {
"code": "AU5"
},
"office": "MEL",
"branch": "MEL",
"district": "SPR",
"subRegion": "SPR",
"region": "AP"
}
}],
[{
"salesOffice": {
"shop": {
"code": "b"
},
"office": "999",
"branch": "999",
"district": "999",
"subRegion": "999",
"region": "999"
}
}
]
]]
below is the expected output json
{
"shops": [
{
"shop": {
"code": "AU5",
"streetName": "a",
"city": "a",
"district": "a",
"state": "a",
"postalCode": "a",
"country": "a",
"salesOffice": {
"office": "MEL",
"branch": "MEL",
"district": "SPR",
"subRegion": "SPR",
"region": "AP"
}
}
},
{
"shop": {
"code": "b",
"streetName": "b",
"city": "b",
"district": "b",
"state": "b",
"postalCode": "b",
"country": "b",
"salesOffice": {
"office": "999",
"branch": "999",
"district": "999",
"subRegion": "999",
"region": "999"
}
}
}
]}
while transformation, 'code' inside shop should match with 'code' inside salesOffice>>shop>>'code'
below is Json Schema for the output payload(should validate against the output)
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"shops": {
"type": "array",
"items": {
"type": "object",
"properties": {
"shop": {
"type": "object",
"properties": {
"code": {
"type": "string"
},
"streetName": {
"type": "string"
},
"city": {
"type": "string"
},
"district": {
"type": "string"
},
"state": {
"type": "string"
},
"postalCode": {
"type": "string"
},
"country": {
"type": "string"
}
},
"salesOffice": {
"type": "object",
"properties": {
"office": {
"type": "string"
},
"branch": {
"type": "string"
},
"district": {
"type": "string"
},
"subRegion": {
"type": "string"
},
"region": {
"type": "string"
}
}
},
"required": [
"code",
"streetName",
"city",
"district",
"state",
"postalCode",
"country",
"salesOffice"
]
}
},
"required": [
"shop"
]
}
}
},
"required": [
"shops"
]}
Any solution or any pointer would be a great help
Refer below dataweave:
%dw 1.0
%output application/json
---
{
shops: using (salesOffice = payload[1]..salesOffice)( payload[0].shops map {
shop :
{
code: $.shop.code,
streetName:$.shop.streetName,
city:$.shop.city,
district:$.shop.district,
state:$.shop.state,
postalCode:$.shop.postalCode,
country:$.shop.country,
salesOffice: using (code= $.shop.code) ( salesOffice[?(code == $.shop.code)] map {
office:$.office,
branch:$.branch,
district:$.district,
subRegion:$.subRegion,
region:$.region
})[0]
}
})
}
You can use Validate JSON Schema mule components.
I have a small JSON file which fails to convert a data value properly. The value in question is the "id" field and the conversion produces 2016-05-06. I have a much larger file (same format) and it works properly. Any help?
{
"results": [
{
"seq": "10457-g1AAAAPreJy90lFqAjEQBuCgFj2B4EOpUgRfXNyscWef9CbtTMZFZF3Brs96E3uT9ib1IMIaE2XpmynUlwkE8n_8TDIhRGtRZzFg0uvNfMakAloNGbPhR4E544ZloLP1ljEvgnxeZOZFDQW9lGW5XNRJiKfnlblrYiRZqtQj6rcr73CpayZNK3pg6bGOmZE80vwr0-xCv1X0q6XViDGJ_t46vId-v9C7iu5YegIEmIw90vxb5w0zxd4cRj9c-Z7lSUEEIP-1ueM_Hf_l-MbJ8iGmxDE8oP2343-u7fvuy6WaRqQf0P7o-Nvu25ZPATRA6JG4PAMPEEFw",
"id": "20160506175330-3168-01",
"changes": [
{
"rev": "1-8bda66017d962508e51ac5061557635b"
}
],
"doc": {
"_id": "20160506175330-3168-01",
"_rev": "1-8bda66017d962508e51ac5061557635b",
"year": "2016",
"month": "05",
"day": "06",
"hour": "17",
"minute": "53",
"second": "30",
"imagebox": "18x94+428+180",
"alchemy": {
"text": "person",
"score": "0.845535"
},
"visual": {
"image": "20160506175330-3168-01.jpg",
"scores": [
{
"classifier_id": "Mixed_Color",
"name": "Mixed_Color",
"score": 0.964822
},
{
"classifier_id": "Stove",
"name": "Stove",
"score": 0.923713
},
{
"classifier_id": "Archery",
"name": "Archery",
"score": 0.836994
},
{
"classifier_id": "Dish_Washer",
"name": "Dish_Washer",
"score": 0.835787
},
{
"classifier_id": "Barber_Shop",
"name": "Barber_Shop",
"score": 0.825716
},
{
"classifier_id": "Full_Body",
"name": "Full_Body",
"score": 0.824954
},
{
"classifier_id": "Musical_Instrument",
"name": "Musical_Instrument",
"score": 0.817304
},
{
"classifier_id": "Beauty_Salon",
"name": "Beauty_Salon",
"score": 0.808518
},
{
"classifier_id": "Clothing_Store",
"name": "Clothing_Store",
"score": 0.784035
},
{
"classifier_id": "Distillery",
"name": "Distillery",
"score": 0.67477
},
{
"classifier_id": "Sweet_Treat",
"name": "Sweet_Treat",
"score": 0.588916
},
{
"classifier_id": "Beer",
"name": "Beer",
"score": 0.538915
},
{
"classifier_id": "Store",
"name": "Store",
"score": 0.536244
},
{
"classifier_id": "Yellow",
"name": "Yellow",
"score": 0.526932
}
]
}
}
}
],
"last_seq": "10457-g1AAAAPreJy90lFqAjEQBuCgFj2B4EOpUgRfXNyscWef9CbtTMZFZF3Brs96E3uT9ib1IMIaE2XpmynUlwkE8n_8TDIhRGtRZzFg0uvNfMakAloNGbPhR4E544ZloLP1ljEvgnxeZOZFDQW9lGW5XNRJiKfnlblrYiRZqtQj6rcr73CpayZNK3pg6bGOmZE80vwr0-xCv1X0q6XViDGJ_t46vId-v9C7iu5YegIEmIw90vxb5w0zxd4cRj9c-Z7lSUEEIP-1ueM_Hf_l-MbJ8iGmxDE8oP2343-u7fvuy6WaRqQf0P7o-Nvu25ZPATRA6JG4PAMPEEFw",
"pending": 0
}
I needed to use --no-inference.
I am new to this website and came here because I am really struggling with a problem of extracting information from a JSON file. The tricky part is that there are variable number of fields, so I can't get away with simple syntax.
Here's a sample code:
{
"addresses": {
"#count": "1",
"address_name": {
"address_spec": {
"#addr_no": "1",
"full_address": "Tel Aviv Univ, Eitan Berglas Sch Econ, IL-69978 Tel Aviv, Israel",
"organizations": {
"#count": "2",
"organization": [
"Tel Aviv Univ",
{
"#pref": "Y",
"#text": "Tel Aviv University"
}
]
},
"suborganizations": {
"#count": "1",
"suborganization": "Eitan Berglas Sch Econ"
},
"city": "Tel Aviv",
"country": "Israel",
"zip": {
"#location": "BC",
"#text": "IL-69978"
}
}
}
},
"category_info": {
"headings": {
"#count": "1",
"heading": "Social Sciences"
},
"subjects": {
"#count": "3",
"subject": [
{
"#ascatype": "traditional",
"#text": "Economics"
},
{
"#ascatype": "extended",
"#text": "Business & Economics"
},
{
"#ascatype": "traditional",
"#text": "ECONOMICS"
}
]
}
}
}
{
"addresses": {
"#count": "1",
"address_name": {
"address_spec": {
"#addr_no": "1",
"full_address": "MIT, Cambridge, MA 02139 USA",
"organizations": {
"#count": "2",
"organization": [
"MIT",
{
"#pref": "Y",
"#text": "Massachusetts Institute of Technology (MIT)"
}
]
},
"city": "Cambridge",
"state": "MA",
"country": "USA",
"zip": {
"#location": "AP",
"#text": "02139"
}
}
}
},
"category_info": {
"headings": {
"#count": "1",
"heading": "Social Sciences"
},
"subjects": {
"#count": "3",
"subject": [
{
"#ascatype": "traditional",
"#text": "Economics"
},
{
"#ascatype": "extended",
"#text": "Business & Economics"
},
{
"#ascatype": "traditional",
"#text": "ECONOMICS"
}
]
}
}
}
{
"addresses": {
"#count": "2",
"address_name": [
{
"address_spec": {
"#addr_no": "1",
"full_address": "Univ Kentucky, Lexington, KY 40506 USA",
"organizations": {
"#count": "2",
"organization": [
"Univ Kentucky",
{
"#pref": "Y",
"#text": "University of Kentucky"
}
]
},
"city": "Lexington",
"state": "KY",
"country": "USA",
"zip": {
"#location": "AP",
"#text": "40506"
}
}
},
{
"address_spec": {
"#addr_no": "2",
"full_address": "Univ Bonn, ZEI, D-5300 Bonn, Germany",
"organizations": {
"#count": "2",
"organization": [
"Univ Bonn",
{
"#pref": "Y",
"#text": "University of Bonn"
}
]
},
"suborganizations": {
"#count": "1",
"suborganization": "ZEI"
},
"city": "Bonn",
"country": "Germany",
"zip": {
"#location": "BC",
"#text": "D-5300"
}
}
}
]
},
"category_info": {
"headings": {
"#count": "1",
"heading": "Social Sciences"
},
"subjects": {
"#count": "3",
"subject": [
{
"#ascatype": "traditional",
"#text": "Economics"
},
{
"#ascatype": "extended",
"#text": "Business & Economics"
},
{
"#ascatype": "traditional",
"#text": "ECONOMICS"
}
]
}
}
}
{
"addresses": {
"#count": "1",
"address_name": {
"address_spec": {
"#addr_no": "1",
"full_address": "Harvard Univ, Cambridge, MA 02138 USA",
"organizations": {
"#count": "2",
"organization": [
"Harvard Univ",
{
"#pref": "Y",
"#text": "Harvard University"
}
]
},
"city": "Cambridge",
"state": "MA",
"country": "USA",
"zip": {
"#location": "AP",
"#text": "02138"
}
}
}
},
"category_info": {
"headings": {
"#count": "1",
"heading": "Social Sciences"
},
"subjects": {
"#count": "3",
"subject": [
{
"#ascatype": "traditional",
"#text": "Economics"
},
{
"#ascatype": "extended",
"#text": "Business & Economics"
},
{
"#ascatype": "traditional",
"#text": "ECONOMICS"
}
]
}
}
}
{
"addresses": {
"#count": "3",
"address_name": [
{
"address_spec": {
"#addr_no": "1",
"full_address": "Columbia Univ, New York, NY 10027 USA",
"organizations": {
"#count": "2",
"organization": [
"Columbia Univ",
{
"#pref": "Y",
"#text": "Columbia University"
}
]
},
"city": "New York",
"state": "NY",
"country": "USA",
"zip": {
"#location": "AP",
"#text": "10027"
}
}
},
{
"address_spec": {
"#addr_no": "2",
"full_address": "NYU, New York, NY USA",
"organizations": {
"#count": "2",
"organization": [
"NYU",
{
"#pref": "Y",
"#text": "New York University"
}
]
},
"city": "New York",
"state": "NY",
"country": "USA"
}
},
{
"address_spec": {
"#addr_no": "3",
"full_address": "Univ Pompeu Fabra, Barcelona, Spain",
"organizations": {
"#count": "2",
"organization": [
"Univ Pompeu Fabra",
{
"#pref": "Y",
"#text": "Pompeu Fabra University"
}
]
},
"city": "Barcelona",
"country": "Spain"
}
}
]
},
"category_info": {
"headings": {
"#count": "1",
"heading": "Social Sciences"
},
"subjects": {
"#count": "3",
"subject": [
{
"#ascatype": "traditional",
"#text": "Economics"
},
{
"#ascatype": "extended",
"#text": "Business & Economics"
},
{
"#ascatype": "traditional",
"#text": "ECONOMICS"
}
]
}
}
}
{
"addresses": {
"#count": "2",
"address_name": [
{
"address_spec": {
"#addr_no": "1",
"full_address": "Univ Chicago, Chicago, IL 60637 USA",
"organizations": {
"#count": "2",
"organization": [
"Univ Chicago",
{
"#pref": "Y",
"#text": "University of Chicago"
}
]
},
"city": "Chicago",
"state": "IL",
"country": "USA",
"zip": {
"#location": "AP",
"#text": "60637"
}
}
},
{
"address_spec": {
"#addr_no": "2",
"full_address": "Amer Bar Fdn, Chicago, IL 60611 USA",
"organizations": {
"#count": "1",
"organization": "Amer Bar Fdn"
},
"city": "Chicago",
"state": "IL",
"country": "USA",
"zip": {
"#location": "AP",
"#text": "60611"
}
}
}
]
},
"category_info": {
"headings": {
"#count": "1",
"heading": "Social Sciences"
},
"subjects": {
"#count": "3",
"subject": [
{
"#ascatype": "traditional",
"#text": "Economics"
},
{
"#ascatype": "extended",
"#text": "Business & Economics"
},
{
"#ascatype": "traditional",
"#text": "ECONOMICS"
}
]
}
}
}
{
"addresses": {
"#count": "2",
"address_name": [
{
"address_spec": {
"#addr_no": "1",
"full_address": "Ohio State Univ, Columbus, OH 43210 USA",
"organizations": {
"#count": "2",
"organization": [
"Ohio State Univ",
{
"#pref": "Y",
"#text": "Ohio State University"
}
]
},
"city": "Columbus",
"state": "OH",
"country": "USA",
"zip": {
"#location": "AP",
"#text": "43210"
}
}
},
{
"address_spec": {
"#addr_no": "2",
"full_address": "Harvard Univ, Cambridge, MA 02138 USA",
"organizations": {
"#count": "2",
"organization": [
"Harvard Univ",
{
"#pref": "Y",
"#text": "Harvard University"
}
]
},
"city": "Cambridge",
"state": "MA",
"country": "USA",
"zip": {
"#location": "AP",
"#text": "02138"
}
}
}
]
},
"category_info": {
"headings": {
"#count": "1",
"heading": "Social Sciences"
},
"subjects": {
"#count": "3",
"subject": [
{
"#ascatype": "traditional",
"#text": "Economics"
},
{
"#ascatype": "extended",
"#text": "Business & Economics"
},
{
"#ascatype": "traditional",
"#text": "ECONOMICS"
}
]
}
}
}
{
"addresses": {
"#count": "1",
"address_name": {
"address_spec": {
"#addr_no": "1",
"full_address": "Univ Chicago, Chicago, IL 60637 USA",
"organizations": {
"#count": "2",
"organization": [
"Univ Chicago",
{
"#pref": "Y",
"#text": "University of Chicago"
}
]
},
"city": "Chicago",
"state": "IL",
"country": "USA",
"zip": {
"#location": "AP",
"#text": "60637"
}
}
}
},
"category_info": {
"headings": {
"#count": "1",
"heading": "Social Sciences"
},
"subjects": {
"#count": "3",
"subject": [
{
"#ascatype": "traditional",
"#text": "Economics"
},
{
"#ascatype": "extended",
"#text": "Business & Economics"
},
{
"#ascatype": "traditional",
"#text": "ECONOMICS"
}
]
}
}
}
{
"addresses": {
"#count": "2",
"address_name": [
{
"address_spec": {
"#addr_no": "1",
"full_address": "Wissensch Zentrum Berlin Sozialforsch, D-1000 Berlin, Germany",
"organizations": {
"#count": "1",
"organization": "Wissensch Zentrum Berlin Sozialforsch"
},
"city": "Berlin",
"country": "Germany",
"zip": {
"#location": "BC",
"#text": "D-1000"
}
}
},
{
"address_spec": {
"#addr_no": "2",
"full_address": "Harvard Univ, Dept Govt, Cambridge, MA 02138 USA",
"organizations": {
"#count": "2",
"organization": [
"Harvard Univ",
{
"#pref": "Y",
"#text": "Harvard University"
}
]
},
"suborganizations": {
"#count": "1",
"suborganization": "Dept Govt"
},
"city": "Cambridge",
"state": "MA",
"country": "USA",
"zip": {
"#location": "AP",
"#text": "02138"
}
}
}
]
},
"category_info": {
"headings": {
"#count": "1",
"heading": "Social Sciences"
},
"subjects": {
"#count": "3",
"subject": [
{
"#ascatype": "traditional",
"#text": "Economics"
},
{
"#ascatype": "extended",
"#text": "Business & Economics"
},
{
"#ascatype": "traditional",
"#text": "ECONOMICS"
}
]
}
}
}
{
"addresses": {
"#count": "2",
"address_name": [
{
"address_spec": {
"#addr_no": "1",
"full_address": "NYU, CV Starr Ctr Appl Econ, New York, NY 10003 USA",
"organizations": {
"#count": "2",
"organization": [
"NYU",
{
"#pref": "Y",
"#text": "New York University"
}
]
},
"suborganizations": {
"#count": "1",
"suborganization": "CV Starr Ctr Appl Econ"
},
"city": "New York",
"state": "NY",
"country": "USA",
"zip": {
"#location": "AP",
"#text": "10003"
}
}
},
{
"address_spec": {
"#addr_no": "2",
"full_address": "Princeton Univ, Princeton, NJ 08544 USA",
"organizations": {
"#count": "2",
"organization": [
"Princeton Univ",
{
"#pref": "Y",
"#text": "Princeton University"
}
]
},
"city": "Princeton",
"state": "NJ",
"country": "USA",
"zip": {
"#location": "AP",
"#text": "08544"
}
}
}
]
},
"category_info": {
"headings": {
"#count": "1",
"heading": "Social Sciences"
},
"subjects": {
"#count": "3",
"subject": [
{
"#ascatype": "traditional",
"#text": "Economics"
},
{
"#ascatype": "extended",
"#text": "Business & Economics"
},
{
"#ascatype": "traditional",
"#text": "ECONOMICS"
}
]
}
}
}
What I was hoping to extract is a country for each of the records (some records have more than one country, which seems to be causing the problem). So my naive approach was to say:
.static_data."fullrecord_metadata".addresses.address_name.country
This however gives me several errors (null has no keys, and cannot index array with string). Checking using the keys command:
.static_data."fullrecord_metadata".addresses.address_name | keys
I can see that it's seems there's a problem with the way the data is structured...
So, could you suggest if I can actually extract the list of countries for each entry using jq? Thank you!
For each input top-level JSON entity, the following filter will recursively examine all the objects to see if they have a "country" key, and it will then report the distinct "country" values for that top-level entity:
jq -c '[.. | if type == "object" and has("country")
then .country
else empty end] | unique'
["Israel"]
["USA"]
["Germany","USA"]
["USA"]
["Spain","USA"]
["USA"]
["USA"]
["USA"]
["Germany","USA"]
["USA"]
Here's a filter that will produce the same results in your example, though it is not exactly equivalent:
[.. | .country? // empty] | unique
[Exercise for the interested reader: what is the difference? :-) ]
Here is a solution which uses a function to handle the variation in .address_name
def address_specs:
if type == "array" then .[].address_spec else .address_spec end
;
.addresses | .address_name | [address_specs | .country] | unique