Neues Thema starten

XML Parsing

Dear Team,


Can you please help me understand how do i parse a data in following format?


{" Success ":  true ," Data ":  {" Items ":  [{" MainImage ":  { " Filename ":  " f1.jpg " , " Link ":  " http://www.abc.de/itempictures/f1.jpg " , " Created ":  " 2017-04-07 08:20 : 01 " } } ,{" Id2 ":  { " Filename ":  " f2.jpg " , " Link ":  " http://www.abc.de/itempictures/f2.jpg " , " Created ":  " 2016-04-15 16:35 : 07 " } } ,{" 0R002M0FN ":  { " Filename ":  " 0R002M_0FN_french_navy.jpg " , " Link ":  " http://www.maprom.de/itempictures/0R002M_0FN_french_navy.jpg " , " Created ":  " 2016-04-15 16:35 : 07 " } }]}}

Also, if there is an existing solution, please redirect me to it.
Thank you in advance.


Sorry, i forgot to ask the question.

With above mentioned json file i want output to look something like this


ID        - filename    - link

Mainfile- f1.jpg        - Http://www.abc.de/itempictures/f1.jpg

Id2 - f2.jpg - Http://www.abc.de/itempictures/f2.jpg


And so on.


Thanks in advance.

This was a very tricky one ;) We will try to document this case, as it is a bit of pain. Especially the use of the entrySet() method is not obvious and we will try to make that simpler in the future.


But for now, the following parsing code of JSON2Spreadsheet Step should result in your desired structure:


  

<#assign row = target.addRow()>
<#list json[" Data "][" Items "] as j >

  <#list j.entrySet() as e>
    <#assign row = target.addRow()>
    ${addColumns(row, e.key,"ID")}
    ${addColumns(row, e.value)}
  </#list>
</#list>

  

image



1 Person gefällt dies

entry set was what we needed. thank you, this helped  :)


one more quick question, is there any easy way to access each items of the value? for example if i want to choose few items from the value?


best regards

Gaurav

Option 1: use another SpreadsheetMapper as the next step and remove the columns you don't need.

The ${addColumns()} function is a short-cut which does a lot of magic under the hood to help you to keep your parsing code as short and simple as possible. A lot of things are possible, but sometimes you need to do the post-processing in another SpreadsheetMapper. 


Option 2: Use the columsArray and mode parameter of ${addColumns} function

e.g. 

${addColumns(row, order['shipping_address'], "shipto_address_", {"columns":["zip", "city"]})}


or longer more explicit:

${addColumns(row, order['shipping_address'], "shipto_address_", {"columns":["zip", "city"], "mode":"include"})}


This adds only the fields zip and city of the shipping_address (which has many other fields).


Btw: We will start working on an improvement of ${addColumns()} so that you do not need the entrySet() anymore, so that this kind of JSON-structure will work out of the box with a single call to ${addColumns()}



1 Person gefällt dies
Anmelden um einen Kommentar zu veröffentlichen