JSON reader mapping for Twitter JSON

Hi, could you please help me too.

I have json:

{"response":[806,{"id":1214,"from_id":52567435,"to_id":7867089,"date":1400874292,"post_type":"post","text":"Some text","can_delete":1,"media":{"type":"audio","owner_id":2000326132,"item_id":282629055},"attachment":{"type":"audio","audio":{"aid":282629055,"owner_id":2000326132,"artist":"Hans Zimmer","title":"Time ","duration":275,"url":"http:\/\/cs9-4v4.vk.me\/p11\/d785b1e4be11de.mp3?extra=SelQ0ToCB1aYSdeaXrJjF83dqLTkYnXSsJGTj11ZTgyaGjn2L-LbFCz_37qfadNp4bJDH8x1aGNsGUb7c8eygROJ1TzEWjCJXsM","performer":"Hans Zimmer","lyrics_id":"109774370","album":"2","genre":18}},"attachments":[{"type":"audio","audio":{"aid":282629055,"owner_id":2000326132,"artist":"Hans Zimmer","title":"Time ","duration":275,"url":"http:\/\/cs9-4v4.vk.me\/p11\/d785b1e4be11de.mp3?extra=SelQ0ToCB1aYSdeaXrJjF83dqLTkYnXSsJGTj11ZTgyaGjn2L-LbFCz_37qfadNp4bJDH8x1aGNsGUb7c8eygROJ1TzEWjCJXsM","performer":"Hans Zimmer","lyrics_id":"109774370","album":"2","genre":18}}],"post_source":{"type":"vk"},"comments":{"count":5,"can_post":1},"likes":{"count":1,"user_likes":1,"can_like":0,"can_publish":0},"reposts":{"count":0,"user_reposted":0},"online":0,"reply_count":5}]}

How can I get two things:

  1. The number 806 at the begining to the port 1 to the field count.
  2. The information from “text” tag to the port 0 to the field text.

Please help!

Hi Markovich,

If your task is to map these fields to two different output ports, the task is pretty easy. In JSONExtract, there is a graphical mapping interface in which you will map these elements to particular fields and ports. Please refer to attached example for more information.
However, if you would like to get these data on one record, the situation would be slightly more complicated. Basically you would have two options, where the second one is based on the aforementioned approach.

  • Using JSONReader
    You will prepare XPATH mapping for your data. Where the first element of „response“ array is the number you want to extract („806“ value) and map to the first field of output record; and the second element of the same array is an array that contains „text“ element whose value you want to map to the second output field. The XPATH may look like this:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>  
<Context xpath="/root/object" outPort="0">  
  <Mapping cloverField="number" xpath="//response[1]"/>  
  <Mapping cloverField="text" xpath="//response[2]/text"/>  
</Context>  
  • Using JSONExtract
    The second option is to use above mentioned mapping dialog and map data to two different output ports. You will see that record counts on the both edges are different. However, if the format of the JSON is always the same, you can be sure that the first records on these edges are those you want to merge into one record. For this purpose, there is a component called Combined (needs to be configured based on your needs – e.g. incomplete tuples allowed, and make sure only complete tuple is sent to output port).

See the attached example to see aforementioned approaches.
ExtractJSON.grf
Hope this helps.