Reputation: 1360
Need to get multiple elements value from json data using Xidel. Single element query like:
xidel - -e 'jn:members(json($raw))("client_name")'
and
xidel - -e 'jn:members(json($raw))("amount")'
work fine but googling for long time, unable to find how to contruct expression for multiple elements extraction. Following tries failed:
xidel - -e 'jn:members(json($raw))("client_name","amount")'
xidel - -e 'jn:members(json($raw))("client_name,amount")'
xidel - -e 'jn:members(json($raw))("client_name")("amount")'
xidel - -e 'jn:members(json($raw))[("client_name")("amount")]'
Upvotes: 3
Views: 3022
Reputation: 167706
We need to see the JSON you have and some explanation of the output you want, whether you want to output JSON or XML or strings.
Here is an example that works for me with the online DEMO of Xidel:
let $data := [
{ "client_name": "Foo", "order": 1, "amount": 20 },
{ "client_name": "Bar", "order": 2, "amount": 30 }]
for $order in jn:members($data)
return $order("client_name") || ": " || $order("amount")
and returns (as I think) a sequence of strings
Foo: 20
Bar: 30
If you want to return three properties of a JSON object then you should be able to use
let $obj := json($raw) return $obj("id") || ', ' || $obj("your_name") || ', ' || $obj("total")
If you don't want to use the pipe then try
let $obj := json($raw) return concat($obj("id"), ", ", $obj("your_name"), ", ", $obj("total"))
Based on the comment with the command line
xidel - -e 'let $obj := json($raw) return concat($obj("id"), ", ", $obj("your_name"), ", ", $obj("total"))'
should do.
Or even
xidel - -e "let $obj := json($raw) return $obj('id') || ', ' || $obj('your_name') || ', ' || $obj('total')"
Upvotes: 0
Reputation: 16917
Some Xidel specific stuff (of the >=0.8 version):
You do not need json($raw)
anymore, $json
is sufficient
It has its own JSON reading syntax, which is more like XPath than JSONiq:
xidel - -e 'jn:members($json) / (client_name, amount)'
or like asked in the other comments:
xidel - -e 'string-join($json / (id, your_name, total), ",")'
And in normal JSONiq, the library functions can be used:
declare namespace libjn= "http://jsoniq.org/function-library";
string-join(libjn:values(libjn:project($json, ("id", "your_name", "total"))), ",")
Upvotes: 2