Related
So, lets consider the following data fetched from db:
[
{
"#id": "http://example.com/1",
"http://example.com/label": "Parent",
"http://example.com/status": "Active",
"http://example.com/children": [
{
"#id": "http://example.com/2"
}
]
},
{
"#id": "http://example.com/2",
"http://example.com/label": "Child",
"http://example.com/status": "Active"
}
]
And the frame:
{
"#context": {
"#base": "http://example.com/",
"#vocab": "http://example.com/"
},
"#graph": {
"status":{}
}
}
The result will look like:
{
"#context": {
"#base": "http://example.com/",
"#vocab": "http://example.com/"
},
"#graph": [
{
"#id": "1",
"children": {
"#id": "2",
"label": "Child",
"status": "Active"
},
"label": "Parent",
"status": "Active"
},
{
"#id": "2",
"label": "Child",
"status": "Active"
}
]
}
As you can see in the first object, in the children section I get some extra parameters in addition to id.
Is there a way I could simplify the children list to just contain ids:
"children": [
"2"
]
I tried adding this to my frame:
"children": {
"#id": "http://example.com/children",
"#type": "#id"
}
But it doesn't work as I expect.
Use framing flags: "#embed": "#never" or "#explicit": true.
{
"#context": {
"#base": "http://example.com/",
"#vocab": "http://example.com/"
},
"#graph": {
"status": {},
"#embed": "#never"
}
}
or
{
"#context": {
"#base": "http://example.com/",
"#vocab": "http://example.com/"
},
"#graph": {
"status": {},
"children": {"#explicit": true, "#omitDefault": true}
}
}
But perhaps all you need is compaction.
If you don't want to compact arrays, toggle the respective option. In JSONLD-Java:
final JsonLdOptions options = new JsonLdOptions();
options.setCompactArrays(false);
Playground: 1, 2, 3.
So, this is my json-ld data:
[
{
"#id": "http://example.com/id2",
"http://www.w3.org/2008/05/skos-xl#literalForm": [
{
"#value": "Secondary entity"
}
]
},
{
"#id": "http://example.com/id1",
"http://example.com/describedBy": [
{
"#id": "http://example.com/id2"
}
],
"http://www.w3.org/2000/01/rdf-schema#label": [
{
"#value": "Main entity"
}
]
}
]
And this is the frame I'm using:
{
"#context" :
{
"label": {
"#id":"http://www.w3.org/2000/01/rdf-schema#label"
},
"describedBy": {
"#id":"http://example.com/describedBy"
},
"id": "#id",
"#vocab" : "http://example.com/",
"#base" : "http://example.com/"
},
"#graph" : {
"describedBy": {}
}
}
The result I'm getting looks like this:
{
"#context": {
"label": {
"#id": "http://www.w3.org/2000/01/rdf-schema#label"
},
"describedBy": {
"#id": "http://example.com/describedBy"
},
"id": "#id",
"#vocab": "http://example.com/",
"#base": "http://example.com/"
},
"id": "id1",
"describedBy": {
"id": "id2",
"http://www.w3.org/2008/05/skos-xl#literalForm": "Secondary entity"
},
"label": "Main entity"
}
Is it possible to frame that data to look like this:
{
"#context": {
"label": {
"#id": "http://www.w3.org/2000/01/rdf-schema#label"
},
"describedBy": {
"#id": "http://example.com/describedBy"
},
"id": "#id",
"#vocab": "http://example.com/",
"#base": "http://example.com/"
},
"id": "id1",
"describedById": "id2",
"describedByLabel": "Secondary entity",
"label": "Main entity"
}
Which is basically creating two new properties: describedById and describedByLabel based on previous describedBy one.
Can't really do this with framing, as "Secondary entity" is a property of the node for "id2", and this would require promoting it to be a property of the "id1" node.
You could do this with a SPARQL CONSTRUCT, but JSON-LD doesn't allow you to really create new data.
So I have been using this logic apps template to hit the Google Analytics API and the response is in this format
{
"reports": [
{
"columnHeader": {
"dimensions": [
"ga:date",
"ga:campaign",
"ga:country",
"ga:browser",
"ga:deviceCategory",
"ga:sourceMedium",
"ga:socialNetwork",
"ga:region"
],
"metricHeader": {
"metricHeaderEntries": [
{
"name": "ga:users",
"type": "INTEGER"
},
{
"name": "ga:sessions",
"type": "INTEGER"
},
{
"name": "ga:newUsers",
"type": "INTEGER"
},
{
"name": "ga:bounces",
"type": "INTEGER"
},
{
"name": "ga:pageviews",
"type": "INTEGER"
},
{
"name": "ga:sessionDuration",
"type": "TIME"
},
{
"name": "ga:hits",
"type": "INTEGER"
},
{
"name": "ga:goalCompletionsAll",
"type": "INTEGER"
},
{
"name": "ga:goalConversionRateAll",
"type": "PERCENT"
}
]
}
},
"data": {
"rows": [
{
"dimensions": [
"20200312",
"(not set)",
"India",
"Chrome",
"desktop",
"(direct) / (none)",
"(not set)",
"Tamil Nadu"
],
"metrics": [
{
"values": [
"4",
"4",
"4",
"0",
"111",
"5100.0",
"111",
"0",
"0.0"
]
}
]
},
{
"dimensions": [
"20200316",
"(not set)",
"India",
"Chrome",
"desktop",
"(direct) / (none)",
"(not set)",
"Tamil Nadu"
],
"metrics": [
{
"values": [
"1",
"1",
"0",
"0",
"6",
"266.0",
"6",
"0",
"0.0"
]
}
]
},
{
"dimensions": [
"20200318",
"(not set)",
"India",
"Chrome",
"desktop",
"(direct) / (none)",
"(not set)",
"Tamil Nadu"
],
"metrics": [
{
"values": [
"1",
"2",
"0",
"0",
"20",
"135.0",
"20",
"0",
"0.0"
]
}
]
}
],
"totals": [
{
"values": [
"6",
"7",
"4",
"0",
"137",
"5501.0",
"137",
"0",
"0.0"
]
}
],
"rowCount": 3,
"minimums": [
{
"values": [
"1",
"1",
"0",
"0",
"6",
"135.0",
"6",
"0",
"0.0"
]
}
],
"maximums": [
{
"values": [
"4",
"4",
"4",
"0",
"111",
"5100.0",
"111",
"0",
"0.0"
]
}
],
"isDataGolden": true
}
}
]
}
I Want to convert it and bring it in a form that the column header:dimensions and metric header entries name will become column names and their values,ie data.rows.dimensions and metrics.values become corresponding values
ga:date ga:campaign ga:country ga:browser ga:deviceCategory ga:sourceMedium ga:socialNetwork ga:region ga:users ga:sessions ga:newUsers : (column names)
20200316 (not set) India Chrome desktop (direct) / (none) (not set) Tamil Nadu 1 1 1 :(values)
If you can use an Integration account, I suggest to create a flat file schema with the desired structure, and in the logic app you can convert in xml and then apply the Flat File Encoding.
Otherwise a function app should resolve your issue
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>
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