Service Virtualization

  • 1.  JSON Filters for repeating complex structures

    Posted Jun 22, 2017 07:24 AM

    # monika mehta , # json filter 

     

    The below is repeating json structure . When i tried with index shown as below i am getting values .

     

    $.productOfferings[0].variants[0].sku

    $.productOfferings[0].products[0].model

      

    Since it’s a repeating structure  , for second variants element when I gave like

     

    $.productOfferings[0].variants[1].sku , its thoriwng error . Can you please help on this ?

    Can you please tell how do i reference the repeating json structure , the second variants elements value 


    "productOfferings": [
    {
    "variants": [
    {
    "sku": "6536451"
    }
    ],
    "products": [
    {
    "manufacturer": "ARJ",
    "serviceProvider": "T-Mobile",
    "model": "TEST DEVICE"
    }
    ],
    "prices": [
    {
    "oneTimeCharges": [
    {
    "amount": 100,
    "chargeType": "tradeIn"
    }
    ]
    }
    ]
    },
    {
    "variants": [
    {
    "sku": "610214629210"
    }
    ],
    "products": [
    {
    "manufacturer": "Alcatel",
    "serviceProvider": "T-Mobile",
    "model": "665 One Touch"
    }
    ],
    "prices": [
    {
    "oneTimeCharges": [
    {
    "amount": 12,
    "chargeType": "tradeIn"
    }
    ]
    }
    ]
    },
    {
    "variants": [
    {
    "sku": "610214637147"
    }
    ],
    "products": [
    {
    "tacCodes": [
    {
    "tacCode": "01413500"
    }
    ],
    "manufacturer": "Alcatel",
    "serviceProvider": "T-Mobile",
    "model": "P310A OneTouch Pop 7 black"
    }
    ],
    "prices": [
    {
    "oneTimeCharges": [
    {
    "amount": 10,
    "chargeType": "tradeIn"
    }
    ]
    }
    ]
    },



  • 2.  Re: JSON Filters for repeating complex structures

    Broadcom Employee
    Posted Jun 22, 2017 11:31 AM

    Monika,  looks like the Json you provided is not valid.  I am getting an error when I run it through  Json Validator.  JSONLint - The JSON Validator 



  • 3.  Re: JSON Filters for repeating complex structures

    Posted Jun 29, 2017 12:00 PM

    Rajeswari,

     

    Can you verify that the Json is valid?



  • 4.  Re: JSON Filters for repeating complex structures
    Best Answer

    Posted Jun 29, 2017 12:51 PM
      |   view attached

    I believe you are seeing the exception because the second SKU is in the .productOfferings[1] occurrence not the [0] occurrence.

     

    Assuming that a cleaned up version of your JSON looks as follows:

    {
      "productOfferings":

      [{
        "variants": [{ "sku": "6536451" }],
        "products": [{ "manufacturer": "ARJ", "serviceProvider": "T-Mobile", "model": "TEST DEVICE" }],
        "prices": [{ "oneTimeCharges": [{ "amount": 100, "chargeType": "tradeIn" }] }]
      },
      {
        "variants": [{ "sku": "610214629210" }],
        "products": [{ "manufacturer": "Alcatel", "serviceProvider": "T-Mobile", "model": "665 One Touch" }],
        "prices": [{ "oneTimeCharges": [{ "amount": 12, "chargeType": "tradeIn" }] }]
      },
      {
        "variants": [{ "sku": "610214637147" }],
        "products": [{ "tacCodes": [{ "tacCode": "01413500" }], "manufacturer": "Alcatel", "serviceProvider": "T-Mobile", "model": "P310A OneTouch Pop 7 black" }],
        "prices": [{ "oneTimeCharges": [{ "amount": 10, "chargeType": "tradeIn" }] }]
      }
      ]
    }

     

    Assuming the above JSON is what you meant to include in your post, the attached example project has a test case in that reads the JSON and parses SKU 0 and SKU 1.  The filters used in the test case are:

    $.productOfferings[0].variants[0].sku returns 6536451

    $.productOfferings[1].variants[0].sku returns 610214629210

    $.productOfferings[0].products[0].model return TEST DEVICE


    Similarly, if you wanted to access the model in the third occurrence, you would use

    $.productOfferings[2].products[0].model returns P310A OneTouch Pop 7 black

    Attachment(s)

    zip
    JSON_Xpath_Tester (2).zip   8 KB 1 version