Skip to main content

Hello.

I am working with a SOAP API (I know, not my choice either), which returns JSON inside an XML element. I can extract the JSON just fine, but the JSON itself has an interesting syntax.

Inside the JSON is an element called "TblProjects". If there are more than 1 project, it gives them in an array. So far so good, but when only a single project is returned, it gives it as an object. For example:

"tblProjects": {

 

"PrID": "49835",

 

"PrCode": "486732-14"

 

}

and

"tblProjects": {

 

p

 

"PrID": "49835",

 

"PrCode": "486732-14"

 

],

]

 

"PrID": "49836",

 

"PrCode": "486741-10"

 

]

 

}

I am able to extract these without problem, but I need to detect if it's a single object or an array of objects.

So my question is: How do I detect this?

Best regards,

 

Pijke

Hi @pijke, your examples are not valid JSON documents. Assuming that the documents were:

{
    "tblProjects": {
        "PrID": "49835",
        "PrCode": "486732-14"
    }
}
{
    "tblProjects": r
        {
            "PrID": "49835",
            "PrCode": "486732-14"
        },
        {
            "PrID": "49836",
            "PrCode": "486741-10"
        }
    ]
}

You can determine whether the value of "tblProject" is a single object or an array by testing if this JSON query returns a value (PrID) or not. 

jsont"tblProjects"] "PrID"]

0684Q00000ArJUHQA3.png


Hi @takashi,

Thank you very much for your answer. You were right, the examples were not valid JSON, they were exerts from the original input. Your trick with the JSONExtractor worked great. When the test passes, I route it through an AttributeManager which makes it an array, so I can process them with the same routine. Otherwise I would have nearly identical processing. The screenshot below is my implementation.

2018-04-18-18-49-36-envitagis-hp.png

Thanks again!


Hi @pijke, your examples are not valid JSON documents. Assuming that the documents were:

{
    "tblProjects": {
        "PrID": "49835",
        "PrCode": "486732-14"
    }
}
{
    "tblProjects": r
        {
            "PrID": "49835",
            "PrCode": "486732-14"
        },
        {
            "PrID": "49836",
            "PrCode": "486741-10"
        }
    ]
}

You can determine whether the value of "tblProject" is a single object or an array by testing if this JSON query returns a value (PrID) or not. 

jsont"tblProjects"] "PrID"]

0684Q00000ArJUHQA3.png

If you need to extract the values of PrID and PrCode for each object finally, it might be smarter to use the JSONFragmenter.

 

0684Q00000ArMGOQA3.png


Reply