Extracting variable number of fields using jq - json

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

Related

How to extract tables and images from website's json object?

"table-wrap": {
"#id": "ams2322-tbl-0001",
"#xml:lang": "en",
"#orientation": "portrait",
"#position": "float",
"label": "Table 1",
"caption": {
"p": "Strength of recomendations"
},
"table": {
"#frame": "hsides",
"#rules": "groups",
"col": [
{
"#style": "border-right:solid 1px #000000",
"#span": "1"
},
{
"#style": "border-right:solid 1px #000000",
"#span": "1"
},
{
"#style": "border-right:solid 1px #000000",
"#span": "1"
},
{
"#style": "border-right:solid 1px #000000",
"#span": "1"
},
{
"#style": "border-right:solid 1px #000000",
"#span": "1"
}
],
"thead": {
"#valign": "top",
"tr": {
"#style": "border-bottom:solid 1px #000000",
"th": [
{
"#align": "left",
"#valign": "top",
"#rowspan": "1",
"#colspan": "1",
"#text": "Strength of Recommendation"
},
{
"#align": "left",
"#valign": "top",
"#rowspan": "1",
"#colspan": "1",
"#text": "Recommend (1)"
},
{
"#align": "left",
"#valign": "top",
"#rowspan": "1",
"#colspan": "1",
"#text": "Suggest (2)"
},
{
"#align": "left",
"#valign": "top",
"#rowspan": "1",
"#colspan": "1",
"#text": "Suggest against (2)"
},
{
"#align": "left",
"#valign": "top",
"#rowspan": "1",
"#colspan": "1",
"#text": "Recommend against (1)"
}
]
}
},
"tbody": {
"tr": [
{
"td": [
{
"#align": "left",
"#rowspan": "1",
"#colspan": "1",
"#text": "Content of recommendation"
},
{
"#align": "left",
"#rowspan": "1",
"#colspan": "1",
"#text": "Strong recommendation in support of an intervention"
},
{
"#align": "left",
"#rowspan": "1",
"#colspan": "1",
"#text": "(Weak) Suggestion in support of an intervention under certain conditions"
},
{
"#align": "left",
"#rowspan": "1",
"#colspan": "1",
"#text": "(Weak) Suggestion against an intervention under certain conditions"
},
{
"#align": "left",
"#rowspan": "1",
"#colspan": "1",
"#text": "Strong recommendation against an intervention"
}
]
},
{
"td": [
{
"#align": "left",
"#rowspan": "1",
"#colspan": "1",
"#text": "Wording of recommendation"
},
{
"#align": "left",
"#rowspan": "1",
"#colspan": "1",
"#text": "We recommend\u2014[intervention]"
},
{
"#align": "left",
"#rowspan": "1",
"#colspan": "1",
"#text": "We suggest\u2014[intervention]"
},
{
"#align": "left",
"#rowspan": "1",
"#colspan": "1",
"#text": "We suggest against\u2014[intervention]"
},
{
"#align": "left",
"#rowspan": "1",
"#colspan": "1",
"#text": "\u2014We recommend against [intervention]"
}
]
}
]
}
},
"permissions": {
"copyright-holder": "John Wiley & Sons, Ltd"
}
}
Hi, I want to extract tables from json object and format them dataframe.
For example this table wrap is one of the table of the article and I want to make it as real table for printing. But I don't know how can I do that. Do you have any idea have can I do that?
And also same thing for figures and images..
Thank you

How to group flight segments/legs in Bargain Finder Max?

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.

Getting local json data with reactjs

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.

Mocked operation performance in Azure API Management

I have an Azure API Management API composed of only mocked operations. Most, like the bottom of the attached image, run in the tenths of a ms. However, one that is test/plain coming in and returns application/json typically runs in tens of seconds (top of image). I find this using developer console or calling from Logic Apps. My case has 1.7K coming in, the returned JSON is large-ish at 26K.
Still this great amount of response time seems excessive. Might there be an explanation for it?
My Representation example returned by the mock
{
"FunctionalGroup": {
"TransactionSet": {
"#controlNumber": "270001",
"Area": [
{
"Segment": [
{
"#id": "BEG",
"Element": [
{
"#pos": "1",
"#text": "00"
},
{
"#pos": "2",
"#text": "SA"
},
{
"#pos": "3",
"#text": "86816853"
},
{
"#pos": "5",
"#date": "2015-04-09",
"#text": "20150409"
}
]
},
{
"#id": "REF",
"Element": [
{
"#pos": "1",
"#text": "DP"
},
{
"#pos": "2",
"#text": "16"
},
{
"#pos": "3",
"#text": "DEPARTMENTA"
}
]
}
],
"SegmentLoop": [
{
"#id": "N9",
"#name": "N9",
"Segment": [
{
"#id": "N9",
"Element": [
{
"#pos": "1",
"#text": "ST"
},
{
"#pos": "2",
"#text": "001"
}
]
},
{
"#id": "DTM",
"Element": [
{
"#pos": "1",
"#text": "010"
},
{
"#pos": "2",
"#date": "2015-04-12",
"#text": "20150412"
},
{
"#pos": "3",
"#time": "00:00:00",
"#text": "0000"
}
]
},
{
"#id": "DTM",
"Element": [
{
"#pos": "1",
"#text": "002"
},
{
"#pos": "2",
"#date": "2015-04-12",
"#text": "20150412"
},
{
"#pos": "3",
"#time": "12:34:34",
"#text": "123456"
}
]
},
{
"#id": "DTM",
"Element": [
{
"#pos": "1",
"#text": "001"
},
{
"#pos": "2",
"#date": "2015-04-30",
"#text": "20150430"
},
{
"#pos": "3",
"#time": "12:34:34.789",
"#text": "123456789"
}
]
}
]
},
{
"#id": "N1",
"#name": "N1",
"Segment": [
{
"#id": "N1",
"Element": [
{
"#pos": "1",
"#text": "ST"
},
{
"#pos": "2",
"#text": "COMPANY INC."
},
{
"#pos": "3",
"#text": "92"
},
{
"#pos": "4",
"#text": "001"
}
]
},
{
"#id": "N3",
"Element": [
{
"#pos": "1",
"#text": "123 main street"
},
{
"#pos": "2",
"#text": "PH:(644)123-4567"
}
]
},
{
"#id": "N4",
"Element": [
{
"#pos": "1",
"#text": "Elk Grove Village"
},
{
"#pos": "2",
"#text": "IL"
},
{
"#pos": "3",
"#text": "6007"
},
{
"#pos": "4",
"#text": "US"
}
]
}
]
},
{
"#id": "N1",
"#name": "N1",
"Segment": [
{
"#id": "N1",
"Element": [
{
"#pos": "1",
"#text": "SF"
},
{
"#pos": "2",
"#text": "EDGE ENTERPRISES"
},
{
"#pos": "3",
"#text": "92"
},
{
"#pos": "4",
"#text": "99999"
}
]
},
{
"#id": "N3",
"Element": [
{
"#pos": "1",
"#text": "2645 W. Coast Highway"
}
]
},
{
"#id": "N4",
"Element": [
{
"#pos": "1",
"#text": "Silicon Valley"
},
{
"#pos": "2",
"#text": "CA"
},
{
"#pos": "3",
"#text": "56874"
},
{
"#pos": "4",
"#text": "US"
}
]
}
]
}
]
},
{
"SegmentLoop": [
{
"#id": "PO1",
"#name": "PO1",
"Segment": [
{
"#id": "PO1",
"Element": [
{
"#pos": "1",
"#text": "1"
},
{
"#pos": "2",
"#text": "96"
},
{
"#pos": "3",
"#text": "EA"
},
{
"#pos": "4",
"#text": "2.05"
},
{
"#pos": "6",
"#text": "BP"
},
{
"#pos": "7",
"#text": "HB-Natural Adult Bat"
}
]
}
],
"SegmentLoop": [
{
"#id": "PID",
"#name": "PID",
"Segment": [
{
"#id": "PID",
"Element": [
{
"#pos": "1",
"#text": "F"
},
{
"#pos": "5",
"#text": "DESCRIPTION"
}
]
}
]
}
]
}
]
},
{
"SegmentLoop": [
{
"#id": "CTT",
"#name": "CTT",
"Segment": [
{
"#id": "CTT",
"Element": [
{
"#pos": "1",
"#text": "6"
}
]
},
{
"#id": "AMT",
"Element": [
{
"#pos": "1",
"#text": "TT"
},
{
"#pos": "2",
"#number": "1000.00",
"#text": "100000"
}
]
}
]
}
]
}
]
}
}
}
My Policy in effect for the relevant API op
<policies>
<inbound>
<!--base: Begin Global scope-->
<!-- PC-1404 -->
<set-query-parameter name="subscription-key" exists-action="delete" />
<!--base: End Global scope-->
</inbound>
<backend>
<!--base: Begin Global scope-->
<forward-request />
<!--base: End Global scope-->
</backend>
<outbound>
<mock-response status-code="200" content-type="application/json" />
</outbound>
<on-error>
<!--base: Begin Global scope-->
<!-- By placing policy statements in the on-error section you can review the error by
using the context.LastError property, inspect and customize the error response using the
set-body policy, and configure what happens if an error occurs.
There are error codes for built-in steps and for errors that may occur during the processing of
policy statements.
For more information, see https://msdn.microsoft.com/en-us/library/azure/mt629506.aspx
statements to be applied if there is an error condition go here -->
<!-- for CORS support to Office Add-In to detect subscription key errors, etc. PLEX-JIRA: PC-725 TSCH 8/16/16 -->
<set-header name="Access-Control-Allow-Origin" exists-action="override">
<value>*</value>
</set-header>
<!--base: End Global scope-->
</on-error>
I found I needed to move the mock call up to the inbound section. It was the forward to back end (I actually don't have a backend figured that was taking the time). The new op policy is
<policies>
<inbound>
<mock-response status-code="200" content-type="application/json" />
<base />
</inbound>
<backend>
<base />
</backend>
<outbound>
<base />
</outbound>
<on-error>
<base />
</on-error>

JSON string into Object Model using Newtonsoft library

I have the following JSON string :
{
"screen": {
"#id": "12345",
"#name": "My Screening Questionnaire",
"#successful": "True",
"question": [
{
"#id": "123",
"#type": "MultipleChoiceOneAnswer",
"#text": "What is your current work status?",
"answer": [
{
"#id": "1",
"#selected": "True",
"#text": "Australian citizen"
},
{
"#id": "2",
"#text": "NZ citizen"
},
{
"#id": "3",
"#text": "Australian permanent resident"
},
{
"#id": "4",
"#text": "NZ permanent resident"
},
{
"#id": "5",
"#text": "Australian work visa"
},
{
"#id": "6",
"#text": "NZ work visa"
},
{
"#id": "7",
"#text": "Holiday working visa"
},
{
"#id": "8",
"#text": "Other"
}
]
},
{
"#id": "124",
"#type": "MultipleChoiceOneAnswer",
"#text": "When are you available to begin employment?",
"answer": [
{
"#id": "1",
"#selected": "True",
"#text": "Now"
},
{
"#id": "2",
"#text": "2 weeks"
},
{
"#id": "3",
"#text": "1 month"
},
{
"#id": "4",
"#text": "1 –2 months"
},
{
"#id": "5",
"#text": "3 or more months"
}
]
},
{
"#id": "125",
"#type": "Text",
"#text": "Why do you consider yourself to be a suitable candidate for this position?",
"answer": {
"#id": "1",
"#selected": "True",
"#text": "I’m great"
}
}
]
}
}
I want to convert it into an Object model using Newtonsoft
JsonConvert.DeserializeObject:T:(JsonStringText)
What will be the structure of the object Model ?
Thanks.
You can refer to the JSON model returned here :
http://developer.seek.com.au/docs/partner-api/api-methods/applicationexport/get-screen