2

I'm pulling JSON from a REST endpoint using curl and then parsing it using the node package json to get at some of the JSON attributes, which I use later in some app configurations.

curl -s http://52.71.126.196/arcgis/rest/services/WaterNetwork/MapServer/0?f=pjson | json -a fields | json -a name

..which gives:

OBJECTID
SHAPE
FACILITYID
ACCOUNTID
METSERVICE
SERVICETYPE
INSTALLDATE
LOCDESC
ROTATION
LOCATIONID
CRITICAL
ENABLED
ACTIVEFLAG
OWNEDBY
MAINTBY
LASTUPDATE
LASTEDITOR
BILLINGNAME
SERVICECODE
CYCLECODE
RATETABLE
SERVICESIZE
REMSERIALNUMBER
METERMULTIPLIER
LONGITUDE
LATITUDE
METERPULL

Perfect. But, what I really need is these put into an array, such as:

["OBJECTID", "SHAPE", "FACILITYID", "ACCOUNTID", "METSERVICE", 
 "SERVICETYPE", "INSTALLDATE", "LOCDESC", "ROTATION", "LOCATIONID", 
 "CRITICAL", "ENABLED", "ACTIVEFLAG", "OWNEDBY", "MAINTBY", "LASTUPDATE", 
 "LASTEDITOR", "BILLINGNAME", "SERVICECODE", "CYCLECODE", "RATETABLE",
 "SERVICESIZE", "REMSERIALNUMBER", "METERMULTIPLIER", "LONGITUDE", 
 "LATITUDE", "METERPULL"]

I found a command using xargs and sed and hacked it to partially get me there:

curl -s http://52.71.126.196/arcgis/rest/services/WaterNetwork/MapServer/0?f=pjson | json -a fields | json -a name | xargs echo | sed 's/ /", "/g'

...which gives:

OBJECTID", "SHAPE", "FACILITYID", "ACCOUNTID", "METSERVICE", "SERVICETYPE", 
"INSTALLDATE", "LOCDESC", "ROTATION", "LOCATIONID", "CRITICAL", "ENABLED", 
"ACTIVEFLAG", "OWNEDBY", "MAINTBY", "LASTUPDATE", "LASTEDITOR", 
"BILLINGNAME", "SERVICECODE", "CYCLECODE", "RATETABLE", "SERVICESIZE", 
"REMSERIALNUMBER", "METERMULTIPLIER", "LONGITUDE", "LATITUDE", "METERPULL

Also, I really need to be able to get the alias data as well:

curl -s http://52.71.126.196/arcgis/rest/services/WaterNetwork/MapServer/0?f=pjson | json -a fields | json -a alias

OBJECTID
SHAPE
Facility Identifier
Account Number
Metered Service
Service Type
Install Date
Location Description
Rotation
Location Identifier
CriticalCustomer
Enabled
Active Flag
Owned By
Managed By
Last Update Date
Last Editor
BILLINGNAME
SERVICECODE
CYCLECODE
RATETABLE
SERVICESIZE
REMSERIALNUMBER
METERMULTIPLIER
LONGITUDE
LATITUDE
METERPULL

My xargs/sed command breaks down here as there are spaces in the aliases:

curl -s http://52.71.126.196/arcgis/rest/services/WaterNetwork/MapServer/0?f=pjson | json -a fields | json -a alias | xargs echo | sed 's/ /", "/g'

As you can see, it separates each word by spaces, not what I need.

OBJECTID", "SHAPE", "Facility", "Identifier", "Account", "Number", 
"Metered", "Service", "Service", "Type", "Install", "Date", "Location", 
"Description", "Rotation", "Location", "Identifier", "CriticalCustomer", 
"Enabled", "Active", "Flag", "Owned", "By", "Managed", "By", "Last", 
"Update", "Date", "Last", "Editor", "BILLINGNAME", "SERVICECODE", 
"CYCLECODE", "RATETABLE", "SERVICESIZE", "REMSERIALNUMBER", 
"METERMULTIPLIER", "LONGITUDE", "LATITUDE", "METERPULL

...and here is where my xargs/sed foo fails me. I'm not sure if this is a xargs thing or a sed thing, and if I need to pass a regex to sed to deal with the possible spaces in the aliases. Is there a xargs/sed command that could give me the results for names and aliases in the array form above?

1

If you have python, a straightforward solution is:

curl -s http://52.71.126.196/arcgis/rest/services/WaterNetwork/MapServer/0?f=pjson | json -a fields | json -a alias | python -c 'import sys;print([line.strip() for line in sys.stdin])'

which gives:

['OBJECTID', 'SHAPE', 'Facility Identifier', 'Account Number', 'Metered Service', 'Service Type', 'Install Date', 'Location Description', 'Rotation', 'Location Identifier', 'CriticalCustomer', 'Enabled', 'Active Flag', 'Owned By', 'Managed By', 'Last Update Date', 'Last Editor', 'BILLINGNAME', 'SERVICECODE', 'CYCLECODE', 'RATETABLE', 'SERVICESIZE', 'REMSERIALNUMBER', 'METERMULTIPLIER', 'LONGITUDE', 'LATITUDE', 'METERPULL']

If you need double-quotes:

curl -s http://52.71.126.196/arcgis/rest/services/WaterNetwork/MapServer/0?f=pjson | json -a fields | json -a alias | python -c 'import sys;print([line.strip() for line in sys.stdin])' | sed 's/'"'"'/"/g'

which gives:

["OBJECTID", "SHAPE", "Facility Identifier", "Account Number", "Metered Service", "Service Type", "Install Date", "Location Description", "Rotation", "Location Identifier", "CriticalCustomer", "Enabled", "Active Flag", "Owned By", "Managed By", "Last Update Date", "Last Editor", "BILLINGNAME", "SERVICECODE", "CYCLECODE", "RATETABLE", "SERVICESIZE", "REMSERIALNUMBER", "METERMULTIPLIER", "LONGITUDE", "LATITUDE", "METERPULL"]
1
  • This ultimately inspired me to use Python for this, something along the lines of layer_name = subprocess.check_output("curl -s {0}/{1}?f=pjson | json -a name".format(service_url, layer_number), shell=True). – Chad Cooper Mar 31 '16 at 0:27
2
$ curl -s http://52.71.126.196/arcgis/rest/services/WaterNetwork/MapServer/0?f=pjson | json -a fields | json -a alias | sed 's/\(.*\)/["\1"]/g' | json -g

["\1"] is the regex capture group, wrapped in quotes and square brackets--so you get a list of arrays, each containing a single alias element, like this:

["foo bar"] ["baz quux"]

Then the json -g will group all those mini-arrays into a single one:

["foo bar", "baz quux"]

1
  • A bit of explanation would improve your answer – Dave M Mar 9 '16 at 12:14
0

Try using perl:

curl -s http://52.71.126.196/arcgis/rest/services/WaterNetwork/MapServer/0?f=pjson | json -a fields | json -a alias | perl -pe 's/\n/", "/g'

Gives:

OBJECTID", "SHAPE", "Facility Identifier", "Account Number", "Metered Service", "Service Type", "Install Date", "Location Description", "Rotation", "Location Identifier", "CriticalCustomer", "Enabled", "Active Flag", "Owned By", "Managed By", "Last Update Date", "Last Editor", "BILLINGNAME", "SERVICECODE", "CYCLECODE", "RATETABLE", "SERVICESIZE", "REMSERIALNUMBER", "METERMULTIPLIER", "LONGITUDE", "LATITUDE", "METERPULL

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.