JSON path parent object, or equivalent MongoDB query

前端 未结 1 618
天涯浪人
天涯浪人 2021-01-14 15:17

I am selecting nodes in a JSON input but can\'t find a way to include parent object detail for each array entry that I am querying. I am using pentaho data integration to qu

相关标签:
1条回答
  • 2021-01-14 15:31

    Shortly after I added my own bounty, I figured out the solution. My problem has the same basic structure, which is a parent identifier, and some number N child key/value pairs for ratings (quality, value, etc...).

    First, you'll need a JSON Input step that gets the SKU, Name, and size_break_costs array, all as Strings. The important part is that size_break_costs is a String, and is basically just a stringified JSON array. Make sure that under the Content tab of the JSON Input, that "Ignore missing path" is checked, in case you get one with an empty array or the field is missing for some reason.

    For your fields, use:

    Name           | Path               | Type
    ProductSKU     | $.sku              | String
    ProductName    | $.name             | String
    SizeBreakCosts | $.size_break_costs | String
    

    I added a "Filter rows" block after this step, with the condition "SizeBreakCosts IS NOT NULL", which is then passed to a second JSON Input block. This second JSON block, you'll need to check "Source is defined in a field?", and set the value of "Get source from field" to "SizeBreakCosts", or whatever you named it in the first JSON Input block.

    Again, make sure "Ignore missing path" is checked, as well as "Ignore empty file". From this block, we'll want to get two fields. We'll already have ProductSKU and ProductName with each row that's passed in, and this second JSON Input step will further split it into however many rows are in the SizeBreakCosts input JSON. For fields, use:

    Name     | Path           | Type
    Quantity | $.[*].quantity | Integer
    Size     | $.[*].size     | String
    

    As you can see, these paths use "$.[*].FieldName", because the JSON string we passed in has an array as the root item, so we're getting every item in that array, and parsing out its quantity and size.

    Now every row should have the SKU and name from the parent object, and the quantity and size from each child object. Dumping this example to a text file, I got:

    ProductSKU;ProductName;Size;Quantity
    SK3579;Authority;S; 80
    SK3579;Authority;M; 14
    SK3579;Authority;L; 55
    
    0 讨论(0)
提交回复
热议问题