JSON Parsing

Nandan Hegde 36,146 Reputation points MVP Volunteer Moderator
2022-12-13T15:19:41.933+00:00

Hello All,
I would like to understand what exactly is happening with the below queries :
basically I am trying to hit a REST API and getting the below output
1)
270090-image.png

2) I want to get the dataset details within additional properties, so do the below:
270098-image.png

3) since the dataset details are present in last reference:
270058-image.png

270181-image.png

But when I do Tostring :
270191-image.png

It provides me the value.

Can someone explain what his happening on this tostring aspect ?

@Andreas Baumgarten @Rich Matheisen

Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Michael Taylor 60,161 Reputation points
    2022-12-13T16:03:05.663+00:00

    First, ordering is undefined. You should never make an assumption about the order in which JSON data is returned to you. Just because it is the "last" value now doesn't mean it might be the next time you call it. To get specific fields you should query directly for the fields.

    In your example typeProperties is wrapped in a JContainer which is just a wrapper around the underlying JSON data. This allows it to handle arbitrary JSON without having to parse it, until you need it. When you then call ToString it is simply returning the JSON it is wrapping. When you use things like typeProperties[0].dataset (just as an example) it is parsing through the JSON to find what you are looking for.

    Without running the code itself, I'm guessing the additional properties is going to be a hashset so perhaps something like this gives you the "dataset".

       $AllLinkedService.Activities.AdditionalProperties["typeProperties"].dataset  
    

  2. Rich Matheisen 47,901 Reputation points
    2022-12-13T19:35:26.097+00:00

    I think some of the confusion is due to your treating "Activities" as if it contains only a single object. It may be either an array or a hash (I'm guessing that it's an array, though).

    When you address $AllLinkedService.Activities.AdditionalProperties you'll get the AdditionalProperties element from each activity in Activities. That's why you see two "typeProperties". I'm guessing you probably want to select only the Activity "Name" you're interested in (e.g., "Web1") from the "Activities". Use a ForEach-Object loop and Where-Object to work only with the activity name you want.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.