Problem with JSON lines parsing

Highlighted
New Member
14 0 0

The bulkOperationRunQuery returns a link to download a JSON lines file.

 

So far so good. I have downloaded the file, but have problems understanding how to parse it.

 

The querry looks like this:

 

mutation {
                    bulkOperationRunQuery(
                      query:"""
                      {
                        productVariants(query: "updated_at:>='2020-02-23T20:24:51Z'") {
                           edges {
                              node {
                                  id 
                                    sku 
                                    title
                                    inventoryQuantity
                                    updatedAt
                                    createdAt
                                    barcode
                                    price
                                   	weight
                                    product { 
                                		   id
                                       title
                                       vendor
                                       publishedAt
                                       productType
                                    }
                                    inventoryItem { 
                                       id
                                       inventoryLevels (first:1) {
                                          edges {
                                             node {
                                                id
                                                available
                                                location {
                                                   id
                                                }
                                             }
                                          }
                                       }
                                    }
                                    

                              }
                           }
                        }
                     }
                     """
                     )  {
                      bulkOperation {
                        id
                        status
                      }
                      userErrors {
                        field
                        message
                      }
                    }
                  }

 

The first two lines lines will look like this:

 

{"id":"gid:\/\/shopify\/ProductVariant\/7528541571","sku":"03-0723","title":"11mg\/1.1%","inventoryQuantity":560,"updatedAt":"2020-02-23T19:44:25Z","createdAt":"2015-09-14T18:39:00Z","barcode":"3700859403751","price":"35.00","weight":20.0,"inventoryItem":{"id":"gid:\/\/shopify\/InventoryItem\/5104529734"},"product":{"id":"gid:\/\/shopify\/Product\/2578937091","title":"TestTitle","vendor":"Yepp","publishedAt":"2015-10-01T08:32:00Z","productType":"Yepp"}}
{"id":"gid:\/\/shopify\/InventoryLevel\/1626508?inventory_item_id=5104529734","available":560,"location":{"id":"gid:\/\/shopify\/Location\/4544323"},"__parentId":"gid:\/\/shopify\/ProductVariant\/7528541571"}

The first line will easily be parsed, and I understand that the parentid of the second line points to the record in the first line.

But how will I understand where in the structure the second line will be ?

 

In this case I guessed that it's actually:

 

                                       inventoryLevels (first:1) {
                                          edges {
                                             node {
                                                id
                                                available
                                                location {
                                                   id

But then I need to hardcode this into every function, and cannot create a more general method to read this file.

0 Likes
Highlighted
Shopify Staff (Retired)
Shopify Staff (Retired)
624 103 124

Hi @David_De_Bono ,

 

With the JSONL output from the BulkOperations API, the order of each connection type is preserved and all nested connections appear after their parents in the file. However, child nodes might not appear directly after their parent. So there is no way you can guarantee where in the out the child node will be, but you will always know that it will come after the parent node. 

 

So with this in mind, when parsing through the JSONL output you can read the file one line at a time (this also has the added benefit of avoiding reading the entire file into memory). When reading a line for a Product Variant / any line that does not have the parentID field, you should save their IDs to somewhere in  memory (for example in either an array, or hash table, or in a relational database store). Then when you read a line that is for an Inventory Item / has the parentID field, you can do a lookup in your memory (e.g. array, hash table, database) for that parentID to find out which parent Product Variant this child belongs to.

 

Hassain | Developer Support Specialist @ Shopify
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Click Accept as Solution 

0 Likes