3

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")]'
user2956477
  • 1,208
  • 9
  • 17

2 Answers2

2

Some Xidel specific stuff (of the >=0.8 version):

  1. You do not need json($raw) anymore, $json is sufficient

  2. 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"))), ",")
BeniBela
  • 16,412
  • 4
  • 45
  • 52
  • First line give me only "client_name" values, second line give me this error: "err:XPTY0020: Context item is not a node" Xidel 0.8.4 – user2956477 Aug 24 '14 at 21:16
  • Then you have another input file than the one posted on pastebin? Is there even a `amount` property (there is none on pastebin)? Perhaps add jn:members`when there are multiple objects after all? Example with input: `xidel -e 'let $json := {"id":1,"your_name":2,"total":3} return string-join($json / (id, your_name, total), ",")'` – BeniBela Aug 24 '14 at 22:06
0

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')"
Martin Honnen
  • 160,499
  • 6
  • 90
  • 110
  • JSON here (privat data replaced with 'xxx'): http://pastebin.com/DHEGBx5z I am scripting some operations on the top of this json in Windows shell, want to plain text of pair values: e.g. for extraction "id,your_name and total" want "349698","xxx","6222.0" but what is more important, as I am non-programmer, want some simple explanation of expression construction. Studied few Jsoniq guides but still no understood. There are too few examples in the net :-( – user2956477 Aug 24 '14 at 11:43
  • The sample seems like a single object literal, I have edited my answer. – Martin Honnen Aug 24 '14 at 12:49
  • Does not make sense for me... Is this jsoniq expression or xidel cmdline parameters? Look like unix script file instead. I am tried it as xidel expression but pipe is special character in win cmdline so this not work. – user2956477 Aug 24 '14 at 13:47
  • The code I posted and have now provided in a slightly different edit is XQuery 3.0 plus JSONiq. As for integrating that with any command line shell, if the latest suggestion does not work then hopefully someone else can help with command line shell syntax problems. – Martin Honnen Aug 24 '14 at 14:02
  • The last one should work if you surround it with `'` quotes for the `-e` parameter. And the one with `||` might work, if you enclose it with `"` quotes (to hide | from the Windows shell) and replace the `"` in the snippet with `'` (so they do not get confused with the outer `"`) – BeniBela Aug 24 '14 at 16:41
  • Both of your Xidel examples gave me single string inside doublequotes ", ," (comma space comma) – user2956477 Aug 24 '14 at 21:19