Skip to main content
Solved

Conditional JSON extraction

  • April 18, 2018
  • 3 replies
  • 52 views

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

This post is closed to further activity.
It may be an old question, an answered question, an implemented idea, or a notification-only post.
Please check post dates before relying on any information in a question or answer.
For follow-up or related questions, please post a new question or idea.
If there is a genuine update to be made, please contact us and request that the post is reopened.

3 replies

takashi
Celebrity
  • 7843 replies
  • 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
  • 8 replies
  • 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
Celebrity
  • 7843 replies
  • April 19, 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

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