0

I have this JSON template

{ 
    "shipToAddress":{ 
        "country":"SE", 
        "emailAddress":"[email protected]", 
        "zipCode":"${zipcode}"
        }, 
    "itemLines":{ 
        "itemLine":[ 
            { 
                "bundleParentId":"", 
                "id":"IL1", 
                "itemType":"ART", 
                "itemNo":"${Article}", 
                "requiredQty":10, 
                "unitOfMeasure":"Piece" 
            }
        ] 
    }, 
    "checkCapacity":"true", 
    "checkInventory":"true", 
    "businessUnit":{ "type":"STO", "code":"856" } 
}

In the above example I have to add the complete element "Itemlines" again the template, could some help me pls. The final template should be like the below,

{ 
    "shipToAddress":{ 
        "country":"SE", 
        "emailAddress":"[email protected]", 
        "zipCode":"${zipcode}"
    }, 
    "itemLines":{ 
        "itemLine":[ 
            { 
                "bundleParentId":"", 
                "id":"IL1", 
                "itemType":"ART", 
                "itemNo":"${Article}", 
                "requiredQty":10, 
                "unitOfMeasure":"Piece" 
            }
        ] 
    "itemLines":{ 
        "itemLine":[ 
            { 
                "bundleParentId":"", 
                "id":"IL1", 
                "itemType":"ART", 
                "itemNo":"${Article}", 
                "requiredQty":10, 
                "unitOfMeasure":"Piece" 
            }
        ] 
    }, 
    "checkCapacity":"true", 
    "checkInventory":"true", 
    "businessUnit":{ "type":"STO", "code":"856" } 
}
2
  • Ok, so the first thing is that this is JSON, not XML. Could you provide the code sample that produced this data structure? I think it could use some work. Commented Mar 1, 2019 at 11:35
  • This json request is an Input, so it is defined by developers, whatever I provided the request for single article now I need to duplicate the part i showed again for multiple articles Commented Mar 1, 2019 at 12:07

1 Answer 1

1

To start with, your expected addition to the template is incorrect and would error out as invalid JSON. Unless the system allows multiple itemLines objects, you really want to be adding a second itemLine to the to the itemLines array.

Some basic JSON terminology:

  • An element is in the form "something" - think of this as the string equivalent in other language
  • An object is in the form {"thing1":"value","thing2":value"} - this is made up of key:value pairs, similar to a map or dict in other languages
  • An array is in the form [{"a":1, "b":2}, "element"] - is an array that can hold various items, seperate by commas

In your template you have:

  • a shipToAddress object;
  • an itemLines object which contains an
    • itemLine array of objects implied to be item lines;
  • a checkCapacity element;
  • a checkInventory element; and
  • a businessUnit object.

To add extra itemLines your JSON should look more like this:

{ 
    "shipToAddress":{ 
        "country":"SE", 
        "emailAddress":"[email protected]", 
        "zipCode":"${zipcode}"
    }, 
    "itemLines":{ 
        "itemLine":[ 
            { 
                "bundleParentId":"", 
                "id":"IL1", 
                "itemType":"ART", 
                "itemNo":"${Article}", 
                "requiredQty":10, 
                "unitOfMeasure":"Piece" 
            },
            { 
                "bundleParentId":"", 
                "id":"IL1", 
                "itemType":"ART", 
                "itemNo":"${Article}", 
                "requiredQty":10, 
                "unitOfMeasure":"Piece" 
            }
        ] 
    }, 
    "checkCapacity":"true", 
    "checkInventory":"true", 
    "businessUnit":{ "type":"STO", "code":"856" } 
}

You should also consider passing in ID as a variable, or you could be violating a constraint elsewhere.

2
  • Nice answer - I might clarify a little that a a JSON element is a quoted string, e.g "element", object is {"key1":"value1"} and an array is ["item1","item2"]. The current examples in the second paragraph are in "something":<type> pairs, which is a little misleading
    – ernie
    Commented Mar 1, 2019 at 17:20
  • Good point - I'm a bit short of time right now, but if someone wants to clean up my examples a little, they're more than welcome.
    – Kate Paulk
    Commented Mar 1, 2019 at 19:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.