1

I have a json file, i converted that by using ConvertFrom-json but the output i am getting below.

Get-Content ".\example1.JSON" | ConvertFrom-Json

Output i am getting as below

---------------------------------
Result                                                                                                                                                                               
------                                                                                                                                                                               
{@{_id=5f0bdeec01c99848bcbbba07; index=0; guid=a1c59de9-94c9-4a53-9a18-61a35457b7a2; isActive=False; balance=$3,782.46; picture=http://placehold.it/32x32; age=28; eyeColor=blue; ...

But i want this should be in below format because of "{"Result":" it is not coming in proper format can someone suggest how to overcome ?

_id           : 5f0bdeec01c99848bcbbba07
index         : 0
guid          : a1c59de9-94c9-4a53-9a18-61a35457b7a2
isActive      : False
balance       : $3,782.46
picture       : http://placehold.it/32x32
age           : 28
eyeColor      : blue
name          : Tran Rodriquez
gender        : male
company       : VIRVA
email         : [email protected]
phone         : +1 (908) 426-2103
address       : 222 Crosby Avenue, Frierson, Louisiana, 613

Here's sample content of the JSON file:

{
  "Result": [
    {
      "id": 10,
      "name": "shirt",
      "color": "red",
      "_id": "5f0bdeec01c99848bcbbba07",
      "host": "tester1"
    },
    {
      "id": 11,
      "name": "coat",
      "color": "black",
      "price": "$2300"
    }
  ]
}
6
  • without seeing the JSON, no one can really tell. can you post a sanitized, simplified sample that demos your problem? Commented Jul 13, 2020 at 19:27
  • {"Result": [ { "id": 10, "name": "shirt", "color": "red", "_id": "5f0bdeec01c99848bcbbba07", "index": 0, "guid": "a1c59de9-94c9-4a53-9a18-61a35457b7a2", "isActive": false, "balance": "$3,782.46", "picture": "placehold.it/32x32", "age": 28, "eyeColor": "blue", "name": "Tran Rodriquez", "gender": "male", "company": "VIRVA", "price": "$123", "host" : "tester1" } ] } Commented Jul 13, 2020 at 19:39
  • save the convert output to $InStuff and then address the object via $InStuff.Result [grin] Commented Jul 13, 2020 at 19:48
  • Please show us the sanitizes JSON file. Use the edit link under your question and insert it there as Formatted text. NOT in a comment, because that becomes unreadable. Commented Jul 13, 2020 at 19:49
  • please see this image i.sstatic.net/EsQfo.jpg Commented Jul 14, 2020 at 2:44

1 Answer 1

1

You need to access the .Result property in order to have the (nested) object it contains format properly:

(Get-Content -Raw .\example1.JSON | ConvertFrom-Json).Result

Note the use of -Raw, which makes Get-Content read the file as a whole, as a single string - while not strictly necessary, this speeds up processing, given that ConvertFrom-Json needs to collect all input first anyway.


An object that is nested inside another object as a property value is formatted using a single-line, hash-table literal-like representation, as you've experienced.

A simple example:

PS> [pscustomobject] @{ foo = 1; bar = [pscustomobject] @{ baz = 2 } }

foo bar
--- ---
  1 @{baz=2}

Note the representation of the nested custom object stored in the .bar property.

This hash-table literal-like representation is the stringification of [pscustomobject] instances, as (also) used in expandable strings; e.g., "$([pscustomobject] @{ baz = 2 })" yields '@{baz=2}' - see this answer for details.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.