Skip to main content
Solved

Conditional JSON extraction


Forum|alt.badge.img

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": {

 

[

 

"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

Best answer by takashi

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

{
    "tblProjects": {
        "PrID": "49835",
        "PrCode": "486732-14"
    }
}
{
    "tblProjects": [
        {
            "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. 

json["tblProjects"]["PrID"]

0684Q00000ArJUHQA3.png

View original
Did this help you find an answer to your question?

3 replies

takashi
Influencer
  • Best Answer
  • April 18, 2018

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

{
    "tblProjects": {
        "PrID": "49835",
        "PrCode": "486732-14"
    }
}
{
    "tblProjects": [
        {
            "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. 

json["tblProjects"]["PrID"]

0684Q00000ArJUHQA3.png


Forum|alt.badge.img
  • Author
  • April 18, 2018

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!


takashi
Influencer
  • April 19, 2018
takashi wrote:

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

{
    "tblProjects": {
        "PrID": "49835",
        "PrCode": "486732-14"
    }
}
{
    "tblProjects": [
        {
            "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. 

json["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


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings