Converting XML to CSV


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Converting XML to CSV
# 8  
Old 09-16-2019
Hi SDohmen,

nice to hear that you worked your way through and thanks for giving feedback.

Some comments:

Quote:
I was not aware that you could "backtrack" the tree and get the main info added to the product info.
That's why I pointed out XPath for you twice and presented you links for a good and short tutorial. Read the great manuals out there! Read it and you will be lesser dependant on others.

Quote:
cheated a bit as i got json data before the xml and i transformed it to the xml to actually get the proper data for it.
I would suggest to avoid that. With this step, the complexity of the task had risen for no good reason. I understand why you did it(just to get the task done - did that myself in the past), but I recommend to better invest the time to get the data out of the original format. That's most likely possible with jq - even if it needs the work to get familiar with jq. Otherwise you carry the increased complexity, increased error possibility, increased resource footprint with that task from now on.

An alternative to jq may be a small php script, that generates the csv

(...and sometimes a quick and dirty hack is just fine too)

That's the thing with jq:

Code:
jq -r <data.json ' .searchresults.data[] |
 . as $parent |
 .products[]  |
 ( $parent.idpurchaseorder+";"+.idproduct)'

Note: Those Pipes are part of the jq-filter - not shell pipes - carefully look at the quotes!

It's more like the approach that didn't work with xml. ;-)

Last edited by stomp; 09-16-2019 at 05:15 PM..
# 9  
Old 09-16-2019
Quote:
Originally Posted by stomp
Hi SDohmen,

nice to hear that you worked your way through and thanks for giving feedback.

Some comments:



That's why I pointed out XPath for you twice and presented you links for a good and short tutorial. Read the great manuals out there! Read it and you will be lesser dependant on others.



I would suggest to avoid that. With this step, the complexity of the task had risen for no good reason. I understand why you did it(just to get the task done - did that myself in the past), but I recommend to better invest the time to get the data out of the original format. That's most likely possible with jq - even if it needs the work to get familiar with jq. Otherwise you carry the increased complexity, increased error possibility, increased resource footprint with that task from now on.

An alternative to jq may be a small php script, that generates the csv.

If you like, show the original json...

(and sometimes a quick and dirty hack is just fine too)

That's the thing with jq:

Code:
jq -r <data.json ' .searchresults.data[] |
 . as $parent |
 .products[]  |
 ( $parent.idpurchaseorder+";"+.idproduct)'

It's more like the approach that didn't work with xml. ;-)



I did find those links you gave me but they where still a bit confusing and i would say limited in what i needed. Too be honest i did not have that much time either as i spend already almost a week on this with xmlstarlet alone.


As you mention this was a cheat but transforming the json to xml but this was also because i am severly limited in time and knowledge of jq. I actually did try a couple days and then i gave up because of the time constraints. Seeing your code however makes it look quite easy and even less fault sensitive. When i have some time i will actually try it out and let you know here about the (hopefully) success of the jq way.




I just tested jq but something seems not to work. I edited your piece of code to work with the json string:
Code:
jq -r <purchaseorders.json ' .data[] | 
 . as $parent |
 .products[]  |
 ( $parent.idpurchaseorder+";"+.idproduct)'


but this gave me an error:
Code:
jq: error (at <stdin>:0): number (376648) and string (";") cannot be added


Last edited by SDohmen; 09-17-2019 at 05:15 AM.. Reason: Tested jq
# 10  
Old 09-17-2019
Your json is different from mine. As the error message states numbers and strings cannot be added. You have to convert the number to a string before concatenating both:

Code:
... (number_var|tostring) + string_var ...

This User Gave Thanks to stomp For This Post:
# 11  
Old 09-19-2019
Quote:
Originally Posted by stomp
Your json is different from mine. As the error message states numbers and strings cannot be added. You have to convert the number to a string before concatenating both:

Code:
... (number_var|tostring) + string_var ...




Do you mean like:


Code:
jq -r <data.json ' .data[] |
 . as $parent |
 .products[]  |
 ( ($parent.idpurchaseorder|tostring)+";"+(.idproduct|tostring))'

I tested the above and this works indeed. When i have some time i will make the full line and see if that works also.



Again thank you for the help and pushing me towards the better solution instead of the workaround with xmlstarlet Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using awk for converting xml to txt

Hi, I have a xml script, I converted it to .txt with values comma seperated using awk function. But I want the output values should be inside double quotes My xml script (Workorders.xml) is shown like below: <?xml version="1.0" encoding="utf-8" ?> <scbm-extract version="3.3">... (8 Replies)
Discussion started by: Viswanatheee55
8 Replies

2. UNIX for Dummies Questions & Answers

Need help converting txt to XML

I have a table as following Archive id Line Author Time Text 1fjj34 3 75jk5l 03:20 this is an evidence regarding ... 1fjj34 4 gjhhtrd 03:21 we have seen those documents before 1fjj34 10 645jmdvvb 04:00 Will you consider such an offer?... (0 Replies)
Discussion started by: A-V
0 Replies

3. Shell Programming and Scripting

Help with converting XML to Flat file

Hi Friends, I want to convert a XML file to flat file. Sample I/p: <?xml version='1.0' encoding='UTF-8' ?> <DataFile xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' contactCount='4999' date='2012-04-14' time='22:00:14' xsi:noNamespaceSchemaLocation='gen .xsd'> <Contact... (3 Replies)
Discussion started by: karumudi7
3 Replies

4. Shell Programming and Scripting

Converting CSV to ascii format

Hej All, I have a file like this which is a comma dilimited: input: 6000318,-263.011520678,-59.05869872409,587.67924868792 6000319,-265.6996842902,-50.24902479999,590.65693082607 6000320,-238.1333898366,-288.801232595,633.75332173496... (5 Replies)
Discussion started by: Johanni
5 Replies

5. Shell Programming and Scripting

converting specific XML file to CSV

Hi, i would convert the following XML file : <?xml version="1.0" encoding="UTF-8" ?> <files xmlns="http://www.lotus.com/dxl/console"> <filedata notesversion="6" odsversion="43" logged="yes" backup="no" id="C12577E6:004B0DA3" iid="C12577E6:004B0DA8" link="1" dboptions="0,524288,0,0"> ... (24 Replies)
Discussion started by: Nicol
24 Replies

6. Shell Programming and Scripting

Converting a flat file in XML

Hello Friends, I am new to UNIX shell scripting. Using bash....Could you please help me in converting a flat file into an XML style output file. Flat file: (Input File entries looks like this) John Miller: 617-569-7996:15 Bunting lane, staten Island, NY: 10/21/79: 60600 The... (4 Replies)
Discussion started by: humkhn
4 Replies

7. UNIX for Dummies Questions & Answers

Converting spaces to csv

I'm new to shell scripting and I have a file with positional columns. The rows look like this: 222 3333 44444 55 55 55 55 5555 59999 222 3333 44444 55 55 55 55 5555 59999 Not every row has the same number of spaces between the seven columns. They can range anywhere... (11 Replies)
Discussion started by: jkandel
11 Replies

8. UNIX for Dummies Questions & Answers

Converting HTML to CSV

Hi, I need to convert a relatively large html file (1.5megs) into CSV under Unix. How would I be able to do this? Much thanks. (3 Replies)
Discussion started by: Jexel
3 Replies

9. Shell Programming and Scripting

converting text to csv format

I am trying to check each line and based on first two digits, the comma needs to be place. I checked in the earlier post where the text is converted to csv with a tab delimited. Here is the test file that needs to be changed to csv 11 051701 22 051701 330123405170105170112345... (13 Replies)
Discussion started by: gthokala
13 Replies

10. Shell Programming and Scripting

Converting csv to xls

Hi, Can anyone tell the option to change the file type in unix. i.e. if a file is in csv(Comma Separating Values) format, it should be changed to xls(ordinary MS-Excel) format. But renaming command is not changing to correct file format. Thanks in advance, Milton. (1 Reply)
Discussion started by: miltony
1 Replies
Login or Register to Ask a Question