0

I've to retrieve products and it's details and save it together in a json file. Unfortunately the name and details cannot be retrieved from the same API, I gotta use 2 different API. So I run a code inside a for loop to retrieve the list of product names as well as its details as follows:

For($i=0; $i -lt $cart.total; $i++)
{
$query = 'dummy value with the cart number'
$productName = Invoke-RestMethod -Uri 'https://abc' -Body $query -Method Post
$details = Invoke-RestMethod -Uri 'https://xyz' -Body $query -Method Post
}

All this works, and below is sample data in $productname and $details.

  • $productName

"iPhone"

  • $details

    "data":[ {"Moduleid": "1", "propertyName": "camera", "value": "12MP"}, {"Moduleid" : "43", "propertyName":"battery", "value": "4000MAj"} ]

However what I am trying to do is to save the $productName and $details into a json as follows:

{
 "iPhone 7":[
{"Moduleid": "1", "propertyName": "camera", "value": "12MP"}, 
{"Moduleid" : "423", "propertyName":"battery", "value": "3000MAh"}
],
 "S10+": [
{"Moduleid": "21", "propertyName": "camera", "value": "12MP"}, 
{"Moduleid" : "43", "propertyName":"battery", "value": "4000MAh"}
]
}

I've tried several ways but couldn't reach any conclusion. Please let me know how can these to variable datas be put into a json format as shown above.

Thank you

3
  • Is there any link between the resulting $productName and the $details to match them together? Commented Sep 6, 2020 at 12:52
  • @Theo Just the order :( When you call the cart number in the query, it'll give list of products and its details in the order in which it is stored. Commented Sep 7, 2020 at 8:56
  • Please can you tell us if Steven's answer works? It looks to me he has it correct. If not, please show us examples of the json results from both queries so we know what we're dealing with here. Commented Sep 8, 2020 at 10:03

2 Answers 2

1

The question doesn't make clear what each query is returning. Generally speaking, you need to convert the return JSON from both queries into objects then merge the objects into the objects you really want. The run those objects through ConvertTo-Json which should do the trick. The source object structure is what results in the JSON text etc...

It would look something like below, but again, I have to guess at the starting point, for example I'm sure that $productName isn't a flat array, but is itself JSON.

# For test
$productName = "iPhone","S10+"

# For test
$details = 
@"
{
    "Data": [{    
        "Moduleid": "1", 
        "propertyName": "camera", 
        "value": "12MP"
    },
    {
        "Moduleid" : "43", 
        "propertyName": "battery", 
        "value": "4000MAj"
    }]
    }
"@ | 
ConvertFrom-Json 

$Results =
For( $i = 0; $i -lt $productName.count; ++$i )
{
    [PSCustomObject]@{
        $productName[$i] = $details.data[$i]
    }
}

$Results | ConvertTo-Json

This would give back:

[
    {
        "iPhone":  {
                       "Moduleid":  "1",
                       "propertyName":  "camera",
                       "value":  "12MP"
                   }
    },
    {
        "S10+":  {
                     "Moduleid":  "43",
                     "propertyName":  "battery",
                     "value":  "4000MAj"
                 }
    }
]

Not quite what you're looking for, but should at least demonstrate the point.

Note: Normally I would look for a common property between the 2 returns to relate the data. Absent that I used the array index. Again part of the reason this doesn't look right.

If you can please edit the question to include the return JSON from both queries, then we can refine the above.

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

1 Comment

I created a hashtable.
0

I created a hashtable like:

$hashTable = [HashTable]::New(0, [StringComparer]::Ordinal)

This is so that the keys are case sensitive. Normally if you create it as @{}, it won't be case sensitive.

Then i mapped the key and value to the hash table as follows,

$hashTable.Add($productName.data.name,$details.data)   

Now, I convert it to Json as I wanted it using ConvertTo-Json

$hashTable| ConvertTo-Json | Out-File $outputPath

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.